Under construction

Still being built. Things may change

The Cow Gathering

Time limit2sMemory limit512 MB

Summary
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 NN cows, and N−1N-1 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 MM pairs of cows (ai,bi)(a_i, b_i) such that cow aia_i must leave before cow bib_i. Note that the cows aia_i and bib_i 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 11 contains two space-separated integers NN and MM.

Lines 2≤i≤N2 ≤ i ≤ N each contain two integers xix_i and yiy_i with 1≤xi,yi≤N1 ≤ x_i, y_i ≤ N and xi≠yix_i ≠ y_i indicating that cows xix_i and yiy_i are friends.

Lines N+1≤i≤N+MN+1 ≤ i ≤ N+M each contain two integers aia_i and bib_i with 1≤ai,bi≤N1 ≤ a_i, b_i ≤ N and ai≠bia_i ≠ b_i indicating that cow aia_i must leave the gathering before cow bib_i.

It is guaranteed that 1≤N,M≤1051 ≤ N, M ≤ 10^5. In test cases worth 20%20\% of the points, it is further guaranteed that N,M≤3000N, M ≤ 3000.

Output

The output should consist of NN lines, with one integer did_i on each line such that di=1d_i = 1 if cow ii could be the last to leave, and di=0d_i = 0 otherwise.

Examples1

  1. Example 1

    Input
    5 1
    1 2
    2 3
    3 4
    4 5
    2 4
    
    Expected output
    0
    0
    1
    1
    1