#P10521. [XJTUPC 2024] 瑟莉姆的宴会

[XJTUPC 2024] 瑟莉姆的宴会

Background

Welcome to Lord Selim’s pleasure banquet!

Problem Description

There are nn guests at the banquet, numbered from 11 to nn. To better control the power of the Shadow, Selim requires that exactly n1n-1 guests are each dominated by exactly one other guest, and the remaining one becomes the overall dominator, dominating the other n1n-1 guests. The direct domination relationships among the guests form a rooted tree.

For this tree, if the parent of node aa is bb, then bb dominates aa, and bb is called the direct dominator of aa. Also, domination is transitive: if aa dominates bb and bb dominates cc, then aa also dominates cc.

There are also mm domination conditions. A domination condition is an ordered pair (x,y)(x,y) (1x,yn1 \le x,y \le n, xyx \neq y). If guest xx dominates yy, then the power of the Shadow increases by 11. If yy dominates xx, then the power of the Shadow decreases by 11. If neither dominates the other, then the power of the Shadow does not change. The initial power of the Shadow is 00.

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 n,mn,m ($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 mm lines each contain two positive integers x,yx,y (1x,yn, xy1 \le x,y \le n,\ x\neq y), separated by a space, representing a domination condition (x,y)(x,y). Domination conditions may be repeated, and opposite conditions may also appear, meaning both (x,y)(x,y) and (y,x)(y,x) can appear. Each domination condition does not affect the others.

Output Format

Output one line with nn numbers. The ii-th number represents the index of the direct dominator of guest ii. The overall dominator’s direct dominator index is 00.

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 111+0+1=01-1-1+0+1=0, which satisfies the non-negative requirement.

Translated by ChatGPT 5