Under construction

Still being built. Things may change

Good News and Bad News (Small)

Time limit5sMemory limit512 MB

Summary
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 FF friends to pass news around. You already know which of your friends can talk to which other friends. There are PP such one way relationships, and each one is an ordered pair (Ai,Bi)(A_i, B_i) meaning that friend AiA_i can talk to friend BiB_i. It does not mean that friend BiB_i can talk to friend AiA_i, although another ordered pair may say exactly that.

For every ordered pair (Ai,Bi)(A_i, B_i) in the input, friend AiA_i delivers one piece of news to friend BiB_i. 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 F2F^2, 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 TT. Each test case begins with one line holding two integers FF and PP: the number of friends and the number of ordered pairs. Then PP lines follow, and the ii-th of them holds two different integers AiA_i and BiB_i, meaning that friend AiA_i can talk to friend BiB_i. Friends are numbered from 1 to FF.

Limits

  • 1≤T≤1001 \le T \le 100
  • 2≤F≤42 \le F \le 4
  • 1≤P≤121 \le P \le 12
  • 1≤Ai≤F1 \le A_i \le F, 1≤Bi≤F1 \le B_i \le F and Ai≠BiA_i \ne B_i for all ii (a friend does not talk to themselves)
  • (Ai,Bi)≠(Aj,Bj)(A_i, B_i) \ne (A_j, B_j) for all i≠ji \ne j (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 PP integers separated by single spaces, where the ii-th integer corresponds to the ii-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 [−F2,F2][-F^2, F^2], 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 (y1,y2,…,yP)(y_1, y_2, \dots, y_P) as sequences of integers, position by position, so the sequence with the smaller value at the first differing position is smaller. For example (−4,−4)(-4, -4) is smaller than (−4,−3)(-4, -3), and (−1,5)(-1, 5) is smaller than (1,−5)(1, -5).

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, F2=9F^2 = 9, so the value −10-10 is not allowed.

Examples1

  1. Example 1

    Input
    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
    
    Expected output
    Case #1: -4 -4
    Case #2: IMPOSSIBLE
    Case #3: -16 -16 -16
    Case #4: -9 -8 -8 -1
    Case #5: -9 9 9