The Cow Gathering
Time limit2sMemory limit512 MB
Given a friendship tree on N cows and M must-leave-before pairs, decide for each cow whether it can be the last to leave.
- Level
Medium7 of 10
- Topics
- Tree, Graph, Topological sort, Prefix sum
- Solved
- No attempts yet
Problem
There are cows, and pairs of cows who are friends with each other. Every cow knows every other cow through some chain of friendships.
They had great fun, but the time has come for them to leave, one by one. They want to leave in some order such that as long as there are still at least two cows left, every remaining cow has a remaining friend. Furthermore, due to issues with luggage storage, there are pairs of cows such that cow must leave before cow . Note that the cows and may or may not be friends.
Help the cows figure out, for each cow, whether she could be the last cow to leave. It may be that there is no way for the cows to leave satisfying the above constraints.
Input
Line contains two space-separated integers and .
Lines each contain two integers and with and indicating that cows and are friends.
Lines each contain two integers and with and indicating that cow must leave the gathering before cow .
It is guaranteed that . In test cases worth of the points, it is further guaranteed that .
Output
The output should consist of lines, with one integer on each line such that if cow could be the last to leave, and otherwise.