#P3542. [POI2012] PEN-Salaries

    ID: 2600 Type: RemoteJudge 1000ms 125MiB Tried: 0 Accepted: 0 Difficulty: 6 Uploaded By: Tags>贪心2012POI深度优先搜索,DFS

[POI2012] PEN-Salaries

题目描述

The Byteotian Software Corporation (BSC) has nn employees.

In BSC's strict hierarchy, each employee has a direct supervisor, except the CEO, to whom all other BSC employees answer, directly or not.

Each employee has a unique monthly salary, and all their salaries range from 1 to nn bythalers.

Each supervisor earns more than each of their subordinates.

According to Byteotian law, the salaries of employees on certain posts may be publicly disclosed.

Furthermore, if the salary of an employee is disclosed, then the salary of their supervisor is also disclosed.

The Byteotian Internal Revenue Anti-Corruption Service (BIRAS) has decided to investigate BSC.

Before BIRAS enters BSC with a warrant, they intend to learn the salaries of all BSC employees that are not disclosed but can be determined from those that are disclosed.

输入格式

In the first line of the standard input a single integer nn (1n1 000 0001\le n\le 1\ 000\ 000) is given that denotes the number of BSC employees.

The employees have id's that range from 1 to nn.

The nn lines that follow provide information on these employees.

The line no. i+1i+1 describes the employee no. ii by two integers pip_i and ziz_i (1pin1\le p_i\le n, 0zin0\le z_i\le n), separated by a single space.

The number pip_i is the id of the direct supervisor of the employee ii.If pi=ip_i=i, then ii is the CEO of BSC. If zi>0z_i>0,then that is the salary of the employee ii.If, on the other hand,zi=0z_i=0, then the salary of employee ii is not disclosed.Those ziz_i's that are positive are also pairwise different.

The input data will always be such that there is at least one assignment of salaries to employees consistent with them, i.e., with the hierarchy and the partial assignment that they provide.

In tests worth 54% of the total points the condition n10 000n\le 10\ 000 holds in addition.

输出格式

Your program should print nn lines to the standard output, each line holding a single integer.

If the employee ii's salary is disclosed or can be inferred from disclosed salaries, then the ii-th line should hold employee ii's salary.

Otherwise the ii-th line should contain 00.

题目大意

题目描述

译自 POI 2012 Stage 3. Day 1「Salaries

有一个 nn 个点的有根树,每个点的权值分别为 1n1 \ldots n,且大于其儿子的权值。其中一部分点的权值是公开的,且每个权值已知的点的父亲权值也一定已知。求能够根据已知信息推算出来的权值未知的点的权值。

输入格式

第一行一个整数 nn,表示点的个数。

接下来 nn 行每行两个整数 pi,zi(1pin,0zin)p_i, z_i (1 \le p_i \le n,0 \le z_i \le n),其中 pip_i 表示结点 ii 的父亲,ziz_i 表示结点 ii 的权值。如果 zi=0z_i = 0,则该点权值未知,否则该点权值为 ziz_i

输出格式

输出 nn 行,每行一个整数,表示 ii 点的权值。如果该点权值已知或可以推算出来,输出该点权值,否则输出 00

样例解释

数据范围

对于 54%54\% 的数据有 n104n \le 10^4.

对于所有数据有 1n1061 \le n \le 10^6

翻译来自于 LibreOJ

10
2 2
2 10
1 0
2 9
2 5
4 0
6 0
6 0
5 0
5 0
2
10
1
9
5
8
0
0
0
0