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 [Codeforces 1286B] Numbers on Tree | 技巧构造_dfs

[Codeforces 1286B] Numbers on Tree | 技巧构造_思维_02


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 [Codeforces 1286B] Numbers on Tree | 技巧构造_dfs_03

The next n lines contain descriptions of vertices: the i-th line contains two integers pi and [Codeforces 1286B] Numbers on Tree | 技巧构造_子树_04, 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 [Codeforces 1286B] Numbers on Tree | 技巧构造_dfs_05. 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 [Codeforces 1286B] Numbers on Tree | 技巧构造_子树_06.

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个点的有根树,每个节点有一个值[Codeforces 1286B] Numbers on Tree | 技巧构造_思维_07
给出每个结点的祖先[Codeforces 1286B] Numbers on Tree | 技巧构造_i++_08[Codeforces 1286B] Numbers on Tree | 技巧构造_i++_08为0代表这个点为根节点),以及子节点中[Codeforces 1286B] Numbers on Tree | 技巧构造_结点_10的点的个数[Codeforces 1286B] Numbers on Tree | 技巧构造_dfs_11
需要构造的就是[Codeforces 1286B] Numbers on Tree | 技巧构造_思维_12数组的值

对于需要构造的n个数,我们可以将其设为从1~n里面的数,而且为了消除相同的影响并方便构造,两两之间[Codeforces 1286B] Numbers on Tree | 技巧构造_思维_12值不同
第一步我们可以找到每个节点为根的情况下,他的子树的结点的数量是多少通过dfs即可获得
对于一个点,如果他的[Codeforces 1286B] Numbers on Tree | 技巧构造_dfs_11甚至大于了他的子树中结点的个数,那么说这种就是不可能的情况,应该输出NO
其余的情况,一定是有一种解的
怎样构造出[Codeforces 1286B] Numbers on Tree | 技巧构造_思维_07
首先我们可以用dfs的方式在对一个点[Codeforces 1286B] Numbers on Tree | 技巧构造_i++_16进行操作的时候,我们求出所有没有被访问过的节点中的第[Codeforces 1286B] Numbers on Tree | 技巧构造_子树_17个点[Codeforces 1286B] Numbers on Tree | 技巧构造_dfs_18,那么说就可以想象到[Codeforces 1286B] Numbers on Tree | 技巧构造_思维_19,然后继续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;
}
/**


**/