#P10521. [XJTUPC 2024] 瑟莉姆的宴会
[XJTUPC 2024] 瑟莉姆的宴会
Background
Welcome to Lord Selim’s pleasure banquet!

Problem Description
There are guests at the banquet, numbered from to . To better control the power of the Shadow, Selim requires that exactly guests are each dominated by exactly one other guest, and the remaining one becomes the overall dominator, dominating the other guests. The direct domination relationships among the guests form a rooted tree.
For this tree, if the parent of node is , then dominates , and is called the direct dominator of . Also, domination is transitive: if dominates and dominates , then also dominates .
There are also domination conditions. A domination condition is an ordered pair (, ). If guest dominates , then the power of the Shadow increases by . If dominates , then the power of the Shadow decreases by . If neither dominates the other, then the power of the Shadow does not change. The initial power of the Shadow is .
As Selim’s considerate servant, Songque needs to organize a banquet, so she must arrange the domination relationships for everyone. Selim does not care how large the power of the Shadow can become; she only needs the power of the Shadow to stay non-negative. Can you help her construct the final domination relationships?
If there are multiple solutions, you only need to output any one. It is guaranteed that for any valid input, a solution exists.
Input Format
The first line contains two positive integers ($1 \le n \le 1\times 10^5,\ 1 \le m \le 2\times 10^5$), separated by spaces, representing the number of guests and the number of domination conditions.
The next lines each contain two positive integers (), separated by a space, representing a domination condition . Domination conditions may be repeated, and opposite conditions may also appear, meaning both and can appear. Each domination condition does not affect the others.
Output Format
Output one line with numbers. The -th number represents the index of the direct dominator of guest . The overall dominator’s direct dominator index is .
5 5
3 1
2 3
1 3
2 4
3 5
2 3 0 3 3
Hint
In the sample, the final power of the Shadow is , which satisfies the non-negative requirement.
Translated by ChatGPT 5