#P13176. [GCJ 2017 #3] Good News and Bad News
[GCJ 2017 #3] Good News and Bad News
题目描述
You would like to get your friends to share some news. You know your friends well, so you know which of your friends can talk to which of your other friends. There are such one-way relationships, each of which is an ordered pair that means that friend can talk to friend . It does not imply that friend can talk to friend ; however, another of the ordered pairs might make that true.
For every such existing ordered pair , you want friend to deliver some news to friend . In each case, this news will be represented by an integer value; the magnitude of the news is given by the absolute value, and the type of news (good or bad) is given by the sign. The integer cannot be 0 (or else there would be no news!), and its absolute value cannot be larger than (or else the news would be just too exciting!). These integer values may be different for different ordered pairs.
Because you are considerate of your friends' feelings, for each friend, the sum of the values of all news given by that friend must equal the sum of values of all news given to that friend. If no news is given by a friend, that sum is considered to be 0; if no news is given to a friend, that sum is considered to be 0.
Can you find a set of news values for your friends to communicate such that these rules are obeyed, or determine that it is impossible?
输入格式
The first line of the input gives the number of test cases, . test cases follow. Each begins with one line with two integers and : the number of friends, and the number of different ordered pairs of friends. Then, more lines follow; the -th of these lines has two different integers and representing that friend can talk to friend . Friends are numbered from 1 to .
输出格式
For each test case, output one line containing Case #x: y
, where is the test case number (starting from 1) and is either IMPOSSIBLE if there is no arrangement satisfying the rules above, or, if there is such an arrangement, integers, each of which is nonzero and lies inside . The -th of those integers corresponds to the -th ordered pair from the input, and represents the news value that the first friend in the ordered pair will communicate to the second. The full set of values must satisfy the conditions in the problem statement.
If there are multiple possible answers, you may output any of them.
5
2 2
1 2
2 1
2 1
1 2
4 3
1 2
2 3
3 1
3 4
1 2
2 3
3 1
2 1
3 3
1 3
2 3
1 2
Case #1: 1 1
Case #2: IMPOSSIBLE
Case #3: -1 -1 -1
Case #4: 4 -4 -4 8
Case #5: -1 1 1
提示
Sample Explanation
The sample output shows one possible set of valid answers. Other valid answers are possible.
In Sample Case #1, one acceptable arrangement is to have friend deliver news with value to friend , and vice versa.
In Sample Case #2, whatever value of news friend gives to friend , it must be nonzero. So, the sum of news values given to friend is not equal to zero. However, friend cannot give any news and so that value is . Therefore, the sums of given and received news for friend cannot match, and the case is IMPOSSIBLE.
In Sample Case #3, each of friends , and can deliver news with value to the one other friend they can talk to — an unfortunate circle of bad news! Note that there is a friend 4 who does not give or receive any news; this still obeys the rules.
In Sample Case #4, note that would not have been an acceptable answer, because there are friends, and .
In Sample Case #5, note that the case cannot be solved without using at least one negative value.
Limits
- .
- , for all .
- , for all .
- , for all . (A friend does not self-communicate.)
- , for all . (No pair of friends is repeated within a test case in the same order.)
Small dataset (Test Set 1 - Visible)
- Time limit:
205 seconds. - .
- .
Large dataset (Test Set 2 - Hidden)
- Time limit:
4010 seconds. - .
- .