Good News and Bad News (Large)
Time limit5sMemory limit512 MB
Assign a nonzero integer to each directed edge so every vertex's outgoing sum equals its incoming sum, exactly as built by a prescribed DFS cycle-circulation procedure.
- Level
Hard8 of 10
- Topics
- Graph, DFS, Implementation, Simulation
- Solved
- No attempts yet
Problem
You want to get your friends to share some news. You know your friends well, so you know which of them can talk to which. There are one way relationships, and the -th of them is an ordered pair meaning that friend can talk to friend . It does not mean that friend can talk to friend , although another ordered pair may state that separately.
For every given ordered pair , friend has to deliver one piece of news to friend . A piece of news is one integer. Its absolute value is the magnitude of the news, and its sign gives the type of the news. The integer cannot be 0 (there would be no news at all), and its absolute value cannot exceed (the news would be too exciting). Different ordered pairs may get different integers.
Because you care about your friends' feelings, for each friend the sum of the values of all news that friend delivers has to equal the sum of the values of all news that friend receives. If a friend delivers no news, that sum is 0. If a friend receives no news, that sum is 0.
Find a set of news values that obeys every rule, or determine that none exists.
Input
The first line contains the number of test cases . test cases follow. The first line of each test case contains two integers and , the number of friends and the number of ordered pairs of friends. Each of the next lines contains two different integers and , meaning that friend can talk to friend . Friends are numbered from 1 to .
Limits
- , , for all (a friend does not talk to themselves)
- for all (no ordered pair repeats inside one test case)
Output
For each test case, print one line containing Case #x: y, where is the test case number starting from 1 and is one of the following.
If no assignment obeys the rules, is IMPOSSIBLE.
Otherwise is integers separated by single spaces, and the -th of them is the news value that friend delivers to friend . Many assignments obey the rules, so print exactly the one that this procedure builds.
- Build an undirected multigraph whose vertices are friends 1 to and whose -th edge joins and . Edge numbers follow the input order.
- Run a depth first search over that multigraph. Start at the smallest friend that has not been visited yet, and repeat until every friend is visited. At a friend, examine the incident edges in increasing order of edge number, and skip the single edge that was used to enter that friend. An edge that leads to a friend not visited yet becomes a tree edge of the search forest, and every other edge joins some friend to one of its ancestors and is a back edge.
- Let back edge join friend and its ancestor . Send one unit of news around the cycle formed by edge and the forest path that goes down from to . The unit travels from to along edge , and from toward along the forest path.
- Once every back edge is handled, the value of edge is the total amount that travelled along it, counted as positive when it travelled from to and as negative when it travelled from to .
Whenever an assignment that obeys the rules exists, the values this procedure builds are nonzero and lie inside .
Notes
A friend who neither delivers nor receives any news still obeys the rules.
If some friend only receives news and can talk to nobody, the test case is IMPOSSIBLE. A news value cannot be 0, so the sum received is nonzero while the sum delivered is 0.
A value whose absolute value exceeds is not allowed even when every other rule holds.
Some test cases cannot be solved without at least one negative value.