CF1286B.Numbers on Tree

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Evlampiy was gifted a rooted tree. The vertices of the tree are numbered from 11 to nn. Each of its vertices also has an integer aia_i written on it. For each vertex ii, Evlampiy calculated cic_i — the number of vertices jj in the subtree of vertex ii, such that aj<aia_j \lt a_i.

Illustration for the second example, the first integer is aia_i and the integer in parentheses is cic_i

After the new year, Evlampiy could not remember what his gift was! He remembers the tree and the values of cic_i, but he completely forgot which integers aia_i were written on the vertices.

Help him to restore initial integers!

埃夫拉姆皮收到了一棵有根树作为礼物。树的顶点编号从 11 到 nn。每个顶点上还写有一个整数 aia_i。对于每个顶点 ii,埃夫拉姆皮计算了 cic_i —— 即顶点 ii 的子树中满足 aj<aia_j \lt a_i 的顶点 jj 的个数。

第二个样例的示意图,其中第一个整数为 aia_i,括号内的整数为 cic_i

新年过后,埃夫拉姆皮完全记不起自己的礼物是什么了!他只记得这棵树以及所有 cic_i 的值,却彻底忘记了各个顶点上写的整数 aia_i 是什么。

请帮助他恢复原始的整数序列!

输入格式

The first line contains an integer nn (1≤n≤2000)(1 \leq n \leq 2000) — the number of vertices in the tree.

The next nn lines contain descriptions of vertices: the ii-th line contains two integers pip_i and cic_i (0≤pi≤n0 \leq p_i \leq n; 0≤ci≤n−10 \leq c_i \leq n-1), where pip_i is the parent of vertex ii or 00 if vertex ii is root, and cic_i is the number of vertices jj in the subtree of vertex ii, such that aj<aia_j \lt a_i.

It is guaranteed that the values of pip_i describe a rooted tree with nn vertices.

第一行包含一个整数 nn(1≤n≤20001 \leq n \leq 2000)——树中顶点的数量。

接下来的 nn 行描述各个顶点:第 ii 行包含两个整数 pip_i 和 cic_i(0≤pi≤n0 \leq p_i \leq n;0≤ci≤n−10 \leq c_i \leq n-1),其中 pip_i 表示顶点 ii 的父节点,若顶点 ii 为根节点,则 pi=0p_i = 0;cic_i 表示顶点 ii 的子树中满足 aj<aia_j \lt a_i 的顶点 jj 的数量。

保证 pip_i 的取值构成一棵含 nn 个顶点的有根树。

输出格式

If a solution exists, in the first line print "YES", and in the second line output nn integers aia_i (1≤ai≤109)(1 \leq a_i \leq {10}^{9}). If there are several solutions, output any of them. One can prove that if there is a solution, then there is also a solution in which all aia_i are between 11 and 10910^9.

If there are no solutions, print "NO".

如果存在解,在第一行输出 "YES",在第二行输出 nn 个整数 aia_i(1≤ai≤1091 \leq a_i \leq {10}^{9})。若存在多个解,输出任意一个即可。可以证明:若存在解,则必存在一个所有 aia_i 均在 11 到 10910^9 范围内的解。

若不存在解,输出 "NO"。

输入输出样例

  • 输入#1

    3
    2 0
    0 2
    2 0

    输出#1

    YES
    1 2 1
  • 输入#2

    5
    0 1
    1 3
    2 1
    3 0
    2 0

    输出#2

    YES
    2 3 2 1 2

输入解题思路,AI测评打分。不知道怎么写?

首页