Greedy Farmers
Time limit1sMemory limit128 MB
Assign each node the smallest Grundy number absent from its neighbors, maximizing the count of nodes that get infinity (-1).
- Level
Hard8 of 10
- Topics
- Graph, Game theory, Greedy, DFS
- Solved
- No attempts yet
Problem
The countries of Byteland have decided to unite and form the Union of Byte Countries. One of its first policies is a system of direct agricultural subsidies: every farmer is given a natural number describing the size of the farm they own, and this number determines the subsidy they receive.
To reduce inequality, the parliament decided to pay the most to farmers holding the smallest numbers, counting from . Because each farmer would then simply claim the value , one rule was added: each farmer must be assigned the smallest natural number that differs from every number assigned to the farmers they employ.
It turns out that for some configurations such an assignment cannot be produced uniquely, or cannot be produced at all. For those situations an extra value, infinity, is used. A farmer may be assigned infinity when employs some farmer who is also assigned infinity, and none of 's own employees was assigned the finite number that would otherwise want to take. The government wants the final assignment to contain as many infinities as possible, and the assignment satisfying this is unique.
Write a program that computes this assignment. For a farmer whose value is infinity, output .
Input
The first line contains an integer () — the number of test cases. The test cases follow.
Each test case begins with a line containing two integers and (, ) — the number of farmers and the number of employment relations between them. The farmers are numbered from to .
Each of the next lines contains two integers and (), meaning that farmer employs farmer .
Output
For each test case output lines. The -th of these lines must contain the value assigned to farmer . If that value is infinity, output instead.