Good News and Bad News (Small)
Time limit5sMemory limit512 MB
Assign a nonzero integer in [-F^2, F^2] to each directed edge so every vertex's outgoing sum equals its incoming sum, choosing the lexicographically smallest solution.
- Level
Medium6 of 10
- Topics
- Graph, Greedy, Math, Brute force
- Solved
- No attempts yet
Problem
You want your friends to pass news around. You already know which of your friends can talk to which other friends. There are such one way relationships, and each one 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 say exactly that.
For every ordered pair in the input, friend delivers one piece of news to friend . Each piece of news is a single integer. The absolute value is the magnitude of the news, and the sign is the type of the news, good or bad. The integer cannot be 0, because then there would be no news, and its absolute value cannot be larger than , because then the news would be too exciting. Different ordered pairs may use different values.
You are considerate of your friends' feelings, so for each friend the sum of the values of all news delivered by that friend must equal the sum of the values of all news delivered to that friend. If a friend delivers no news, that sum is 0. If a friend receives no news, that sum is 0.
Decide whether a set of news values obeying all of these rules exists, and find it when it does.
Input
The first line contains the number of test cases . Each test case begins with one line holding two integers and : the number of friends and the number of ordered pairs. Then lines follow, and the -th of them holds two different integers and , meaning that friend can talk to friend . Friends are numbered from 1 to .
Limits
- , and for all (a friend does not talk to themselves)
- for all (no ordered pair repeats inside one test case)
Output
For each test case, output one line containing Case #x: y, where x is the test case number starting from 1. If no arrangement obeys the rules, y is IMPOSSIBLE. Otherwise y is integers separated by single spaces, where the -th integer corresponds to the -th ordered pair of the input and is the news value that the first friend of that pair delivers to the second friend. Each integer must be nonzero and lie inside , and the whole set of values must satisfy every condition in the statement.
When several arrangements satisfy the conditions, output only the lexicographically smallest one. Compare as sequences of integers, position by position, so the sequence with the smaller value at the first differing position is smaller. For example is smaller than , and is smaller than .
Notes
- If a friend receives news but delivers none, the sum received is nonzero while the sum delivered is 0, so the rules cannot be satisfied.
- A friend who neither delivers nor receives any news breaks no rule.
- Some inputs cannot be solved with positive values only. Those inputs need at least one negative value.
- The bound on the absolute value depends on the number of friends. With 3 friends, , so the value is not allowed.