luogu#P5157. [USACO18DEC] The Cow Gathering P

[USACO18DEC] The Cow Gathering P

Problem Description

Cows from all over the world have gathered for a big party. There are a total of NN cows, and N1N-1 pairs of cows are friends. Each cow can get to know every other cow through some chain of friendships.

They have had a great time, but now it is time for them to leave, one by one. They want to leave in some order such that, as long as at least two cows have not left yet, every remaining cow still has at least one friend who has not left. In addition, due to baggage storage constraints, there are MM pairs of cows (ai,bi)(a_i,b_i) that must satisfy that cow aia_i leaves before cow bib_i. Note that cows aia_i and bib_i may or may not be friends.

Help the cows determine, for each cow, whether she can be the last cow to leave. It is possible that there is no leaving order that satisfies the above requirements.

Input Format

The first line of input contains two space-separated integers NN and MM.

The following N1N-1 lines (for 2iN2 \leq i \leq N) each contain two integers xix_i and yiy_i, satisfying 1xi1 \leq x_i, yiN,xiyiy_i \leq N, xi \neq yi, indicating that cows xix_i and yiy_i are friends.

The following MM lines (for N+1iN+MN+1 \leq i \leq N+M) each contain two integers aia_i and bib_i, satisfying 1ai,biN1 \leq a_i,b_i \leq N and aibia_i \neq b_i, indicating that cow aia_i must leave the party before cow bib_i.

The input guarantees 1N,M1051 \leq N,M \leq 10^5. For the testdata worth 20%20\% of the total score, it is guaranteed that N,M3000N,M \leq 3000.

Output Format

Output NN lines. Each line contains an integer did_i. If cow ii can be the last cow to leave, then di=1d_i=1; otherwise, di=0d_i=0.

5 1
1 2
2 3
3 4
4 5
2 4

0
0
1
1
1

Hint

Translated by ChatGPT 5