Unidirectional Link Network

Time limit1sMemory limit128 MB

Summary
Given a directed graph, partition nodes into disjoint simple rings or paths (following directed edges) to maximize total edge value, essentially a maximum weight matching style flow problem.
Level

Hard8 of 10

Topics
Graph, Greedy, Math
Solved
No attempts yet

Problem

A multicomputer system consists of several nodes, each with its own memory. The nodes are connected to one another by unidirectional communication links, so the interconnection network can be described as a directed graph: vertices are nodes and edges are the unidirectional links.

The two most important structures in an interconnection network are the linear array and the ring.

  • A linear array places nodes in order 0,1,…,k−10, 1, \dots, k-1 such that, for every 0≤i<k−10 \le i < k-1, there is a unidirectional link from node ii to node i+1i+1. A linear array may consist of a single node.
  • A ring is a linear array with one additional unidirectional link from node k−1k-1 back to node 00.

To support parallel applications, the whole system must be decomposed into several subsystems. Each subsystem must be a ring or a linear array, and no two subsystems may share a node.

A ring of kk nodes is worth kk dollars, and a linear array of kk nodes is worth k−1k-1 dollars. Consequently a linear array of a single node is worth 00 dollars.

Given an interconnection network, write a program that decomposes the system into rings and/or linear arrays so that the total value is maximized.

For example, decomposing a network into a ring of 6 nodes and a linear array of 8 nodes gives a total value of 6+7=136 + 7 = 13 dollars, which can be the maximum possible value.

Input

The first line contains the number of test cases TT. For each test case, the first line contains the number of nodes nn and the number of unidirectional links mm (n≤1,000n \le 1{,}000, m≤50,000m \le 50{,}000). Nodes are numbered from 00 to n−1n-1. Each of the next mm lines contains two integers uu and vv, separated by a space, denoting a unidirectional link from node uu to node vv.

Output

For each test case, print a single line containing one integer: the maximum total value obtainable by decomposing the given interconnection network into rings and/or linear arrays.

Examples7

  1. Example 1

    Input
    3
    4 3
    3 2
    1 0
    2 3
    6 6
    0 1
    1 2
    2 3
    3 1
    3 4
    4 5
    14 19
    0 1
    1 2
    2 3
    3 4
    4 5
    5 0
    5 4
    2 1
    2 6
    6 7
    7 8
    8 9
    9 1
    8 7
    7 10
    10 11
    11 12
    12 13
    13 8
    
    Expected output
    3
    5
    13
    
  2. Example 2

    Input
    1
    1 0
    
    Expected output
    0
    
  3. Example 3

    Input
    1
    4 3
    0 1
    1 2
    2 3
    
    Expected output
    3
    
  4. Example 4

    Input
    1
    3 3
    0 1
    1 2
    2 0
    
    Expected output
    3
    
  5. Example 5

    Input
    1
    2 2
    0 1
    1 0
    
    Expected output
    2
    
  6. Example 6

    Input
    1
    3 2
    0 1
    0 2
    
    Expected output
    1
    
  7. Example 7

    Input
    1
    3 2
    0 2
    1 2
    
    Expected output
    1