-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdforthing.cpp
75 lines (60 loc) · 1.43 KB
/
dforthing.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<ll, ll>;
using vi = vector<ll>;
using vpi = vector<pi>;
using vb = vector<bool>;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin >> n;
vi p(n + 1);
vi c(n + 1);
ll root;
for (ll i = 1; i <= n; i++) {
cin >> p[i] >> c[i];
if (p[i] == 0) root = i;
}
vector<set<ll>> ed(n + 1);
for (ll i = 1; i <= n; i++) {
ed[p[i]].insert(i);
}
bool works = true;
function<vector<ll>(ll)> dfs = [&](ll v) {
vector<ll> cur;
for (ll ch : ed[v]) {
vector<ll> chv = dfs(ch);
cur.insert(cur.begin(), chv.begin(), chv.end());
}
if (c[v] > cur.size()) {
works = false;
return vi{};
} else {
cur.insert(cur.begin() + c[v], v);
//cout << "CUR: ";
//for (int i : cur) cout << i << " ";
//cout << endl;
//cout << endl;
return cur;
}
};
vi last = dfs(root);
if(!works) {
cout << "NO" << endl;
}
else {
cout << "YES" << endl;
vi ans(n);
for (int i = 0; i < last.size(); i++) {
//cout << last[i] << endl;
ans[last[i] - 1] = i + 1;
}
for(int i : ans) {
cout << i << " ";
}
cout << endl;
}
return 0;
}