1380. Monkeys

单点时限: 5.0 sec

内存限制: 256 MB

There are n monkeys on a tree. They are numbered from 1 to n. The monkey number 1 clutches a branch by its tail. Remaining monkeys either are held by other monkeys, hold on to other monkeys or both. Each monkey can use two hands and can hold at most one other monkey in each hand (gripping its tail). Starting from the moment 0, at each second one monkey releases its grip of one hand. This may cause some monkeys fall down onto the ground, where they can continue releasing their grips (the time of falling is negligibly small).

Task

Write a program which:

reads from the standard input the description of how the monkeys hold together and in what order they release their grips,

for each monkey computes the time it falls down onto the ground,

writes the result to the standard output.

输入格式

The first line of the standard input consists of two positive integers n and m, 1 <= n <= 200000, 1 <= m <= 400000. The number n denotes the number of monkeys, and the number m denotes the time (in seconds) we observe the monkeys. Next n lines contains the description of the initial situation. In the (k + 1)-st line (1 <= k <= n) there are two integers denoting the numbers of monkeys that are hold by the monkey number k. The former is the number of the monkey that is hold by the left hand, and the latter - by the right hand. The number -1 denotes that the monkey’s hand is free. The following m lines denote the result of the observation of the monkeys. In the i-th of those lines (1 <= i <= m) there are two integers. The former is the number of the monkey, and the latter is the number of its hand (1 - left, 2 - right) the monkey releases its grip of, in the moment i - 1.

输出格式

Your program should write to the standard output exactly n integers, one per line. The number of the i-th line should denote the moment the i-th monkey fell down onto the ground, or should be equal -1 if the monkey has not fallen down onto the ground during the observation.

样例

Input
3 2
-1 3
3 -1
1 2
1 2
3 1
Output
-1
1
1

0 人解决,3 人已尝试。

0 份提交通过,共有 15 份提交。

9.9 EMB 奖励。

创建: 17 年,3 月前.

修改: 7 年,2 月前.

最后提交: 3 年,12 月前.

来源: POI

题目标签