-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path1287D.cpp
executable file
·66 lines (57 loc) · 1.26 KB
/
1287D.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
// Problem Code: 1287D
#include <iostream>
#include <string>
#include <vector>
#include <numeric>
#include <functional>
using namespace std;
void numbers_on_tree(int n, vector<int>& p, vector<int>& c) {
int root = -1;
function<int(int)> count;
function<void(int)> construct;
vector< vector<int> > ch(n + 1);
vector<int> cnt(n + 1), val(n + 1), r(n);
// Create adjacency list of tree
for (int i = 1; i <= n; i++) {
if (p[i] == 0)
root = i;
else
ch[p[i]].push_back(i);
}
// Count number of children for each node
count = [&](int node) {
for (int u: ch[node])
cnt[node] += 1 + count(u);
return cnt[node];
};
count(root);
// Check if c[i] is greater than number of children
for (int i = 1; i <= n; i++)
if (c[i] > cnt[i]) {
cout << "NO" << endl;
return;
}
// Construct values for all nodes
iota(r.begin(), r.end(), 1);
construct = [&](int node) {
val[node] = r[c[node]];
r.erase(r.begin() + c[node]);
for (int u: ch[node])
construct(u);
};
construct(root);
// Output solution
cout << "YES" << endl;
for (int i = 1; i <= n; i++)
cout << val[i] << " ";
cout << endl;
}
int main() {
int n;
cin >> n;
vector<int> p(n + 1), c(n + 1);
for (int i = 1; i <= n; i++)
cin >> p[i] >> c[i];
numbers_on_tree(n, p, c);
return 0;
}