Evlampiy was gifted a rooted tree. The vertices of the tree are numbered from 1 to n. Each of its vertices also has an integer ai written on it. For each vertex i, Evlampiy calculated ci — the number of vertices j in the subtree of vertex i, such that a j < a i
Illustration for the second example, the first integer is ai and the integer in parentheses is ci
After the new year, Evlampiy could not remember what his gift was! He remembers the tree and the values of ci, but he completely forgot which integers ai were written on the vertices.
Help him to restore initial integers!
Input
The first line contains an integer n ( 1 ≤ n ≤ 2000 ) — the number of vertices in the tree.
The next n lines contain descriptions of vertices: the i-th line contains two integers pi and c i ( 0 ≤ p i ≤ n ; 0 ≤ c i ≤ n − 1 ) , where pi is the parent of vertex i or 0 if vertex i is root, and ci is the number of vertices j in the subtree of vertex i, such that aj<ai.
It is guaranteed that the values of pi describe a rooted tree with n vertices.
Output
If a solution exists, in the first line print “YES”, and in the second line output n integers ai ( 1 ≤ a i ≤ 109 ) . 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 ai are between 1 and 109
If there are no solutions, print “NO”.
Examples
inputCopy
3 2 0 0 2 2 0
outputCopy
YES 1 2 1
inputCopy
5 0 1 1 3 2 1 3 0 2 0
outputCopy
YES 2 3 2 1 2
题意:
一刻n个点的有根树,每个节点有一个值a i
给出每个结点的祖先fa(fa 为0代表这个点为根节点),以及子节点中a j < a i 的点的个数c i
需要构造的就是a 数组的值
对于需要构造的n个数,我们可以将其设为从1~n里面的数,而且为了消除相同的影响并方便构造,两两之间a 值不同
第一步我们可以找到每个节点为根的情况下,他的子树的结点的数量是多少通过dfs即可获得
对于一个点,如果他的c i 甚至大于了他的子树中结点的个数,那么说这种就是不可能的情况,应该输出NO
其余的情况,一定是有一种解的
怎样构造出a i?
首先我们可以用dfs的方式在对一个点u uu进行操作的时候,我们求出所有没有被访问过的节点中的第c [ u ] + 1 个点i ,那么说就可以想象到a [ u ] = i ,然后继续dfs所连边即可
Code:
int n; struct node{ int to,nex; }e[maxn]; int cnt,head[maxn]; void init(){ cnt = 0; for(int i=1;i<=n;i++) head[i] = -1; } void add(int u,int v){ e[cnt].to = v; e[cnt].nex = head[u]; head[u] = cnt ++; } int root; int fa,c[maxn]; int siz[maxn],a[maxn]; bool vis[maxn]; void dfs(int u) { int cnt = 0; for(int i=1;i<=n;i++){ if(vis[i]) continue; cnt ++; if(cnt == c[u] + 1) { vis[i] = 1; a[u] = i; break; } } for(int i=head[u];~i;i=e[i].nex) { int to = e[i].to; dfs(to); } } void get(int u) { siz[u] = 1; for(int i=head[u];~i;i=e[i].nex) { int to = e[i].to; get(to); siz[u] += siz[to]; } } int main() { n = read; init(); for(int i=1;i<=n;i++) { fa = read,c[i] = read; if(fa == 0) { root = i; continue; } add(fa,i); } get(root); for(int i=1;i<=n;i++){ if(c[i] >= siz[i]) { puts("NO"); return 0; } } puts("YES"); dfs(root);/// root vet for(int i=1;i<=n;i++){ printf("%d ",a[i]); } return 0; } /** **/