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 cows, and 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 pairs of cows that must satisfy that cow leaves before cow . Note that cows and 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 and .
The following lines (for ) each contain two integers and , satisfying , , indicating that cows and are friends.
The following lines (for ) each contain two integers and , satisfying and , indicating that cow must leave the party before cow .
The input guarantees . For the testdata worth of the total score, it is guaranteed that .
Output Format
Output lines. Each line contains an integer . If cow can be the last cow to leave, then ; otherwise, .
5 1
1 2
2 3
3 4
4 5
2 4
0
0
1
1
1
Hint
Translated by ChatGPT 5