Under construction

Still being built. Things may change

Squares and Lanterns

Time limit2sMemory limit1024 MB

Summary
Given a tree where each square has a lantern range, find which squares can serve as a center where every leaf-to-center path has the same lit-lantern total while all squares meet their minimums.
Level

Hard8 of 10

Topics
Tree, Dynamic programming, DFS
Solved
No attempts yet

Problem

In 15th-century Barcelona there were nn squares. Some squares are connected by two-way roads. There are n−1n-1 roads in total, and from every square you can reach every other square along these roads, so the squares form a tree with nn vertices.

Square ii has rir_i lanterns. Callum will find it easier to fight the Templars if the city is better lit, so he wants to switch on some lanterns in some squares. Square ii is lit well enough only if at least lil_i lanterns are on there.

Callum calls a square peripheral if it is connected by exactly one road to another square.

Callum calls a square vv bright if there is a way to switch on some lanterns in each square so that every square is lit well enough, and the number of lit lanterns on the path from vv to every peripheral square other than vv is the same.

The number of lit lanterns on a path is the total number of lit lanterns on all squares of that path.

Help Callum determine which squares are bright.

Input

The first line contains a natural number nn (the number of squares), with 2≤n≤1052 \le n \le 10^5.

Each of the next n−1n-1 lines contains two natural numbers aia_i and bib_i (1≤ai,bi≤n1 \le a_i, b_i \le n, ai≠bia_i \ne b_i), the numbers of the squares connected by the road.

Each of the next nn lines contains two non-negative integers lil_i and rir_i (0≤li≤ri≤1040 \le l_i \le r_i \le 10^4), the minimum and maximum number of lanterns that can be switched on in square ii.

It is guaranteed that the squares form a tree.

Output

Print nn numbers on one line. Print 1 if square ii is bright, and 0 otherwise.

Examples2

  1. Example 1

    Input
    6
    1 2
    1 3
    1 4
    4 5
    5 6
    1 3
    0 4
    0 4
    2 3
    2 2
    0 0
    
    Expected output
    1 1 1 1 0 1
    
  2. Example 2

    Input
    4
    1 2
    1 3
    1 4
    1 1
    1 1
    1 1
    1 1
    
    Expected output
    1 1 1 1