-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathB.cpp
58 lines (56 loc) · 865 Bytes
/
B.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "bits/stdc++.h"
using namespace std;
int c[2005];
vector <int> g[2005];
int ans[2005];
int sub[2005];
void add(int x, int val) {
ans[x] += val;
for(auto i : g[x]) {
add(i, val);
}
}
void addGreater(int x, int val) {
if(ans[x] >= val) {
++ans[x];
}
for(auto i : g[x]) {
addGreater(i, val);
}
}
void dfs(int x) {
for(auto i : g[x]) {
dfs(i);
add(i, sub[x]);
addGreater(i, c[x] + 1);
sub[x] += sub[i];
}
sub[x] += 1;
ans[x] = c[x] + 1;
if(sub[x] <= c[x]) {
printf("NO\n");
exit(0);
}
}
int main(int argc, char const *argv[])
{
int n;
scanf("%d", &n);
int root = 1;
for(int i = 1; i <= n; i++) {
int p;
scanf("%d %d", &p, &c[i]);
if(p == 0) {
root = i;
} else {
g[p].push_back(i);
}
}
dfs(root);
printf("YES\n");
for(int i = 1; i <= n; i++) {
printf("%d ", ans[i] + 1);
}
printf("\n");
return 0;
}