Unidirectional Link Network
Time limit1sMemory limit128 MB
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.
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 such that, for every , there is a unidirectional link from node to node . A linear array may consist of a single node.
- A ring is a linear array with one additional unidirectional link from node back to node .
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 nodes is worth dollars, and a linear array of nodes is worth dollars. Consequently a linear array of a single node is worth 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 dollars, which can be the maximum possible value.
Input
The first line contains the number of test cases . For each test case, the first line contains the number of nodes and the number of unidirectional links (, ). Nodes are numbered from to . Each of the next lines contains two integers and , separated by a space, denoting a unidirectional link from node to node .
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.