10986 | 2024-05-01 21:19:10 | zsombor | Vállalati ügyelet | cpp17 | Accepted 100/100 | 179ms | 74116 KiB |
#include <iostream>
#include <vector>
using namespace std;
int n, mex = 1;
vector <int> p(3e5, 0);
vector <int> a(3e5, 0);
vector <vector <int>> g(3e5);
vector <int> sz(3e5, 1);
vector <int> cnt(3e5, 0);
vector <int> ans(3e5, 0);
void dfs(int x) {
for (int i = 0; i < g[x].size();i++) {
int& y = g[x][i];
dfs(y);
sz[x] += sz[y];
if (sz[y] > sz[g[x][0]]) swap(g[x][0], y);
}
}
void dfs_del(int x) {
cnt[a[x]]--;
for (int i : g[x]) dfs_del(i);
}
void del(int x) {
mex = 1;
dfs_del(x);
}
void add(int x) {
cnt[a[x]]++;
while (cnt[mex]) mex++;
}
void dfs_add(int x) {
add(x);
for (int i : g[x]) dfs_add(i);
}
void solve(int x) {
for (int i = 1; i < g[x].size(); i++) {
int& y = g[x][i];
solve(y);
del(y);
}
if (g[x].size()) solve(g[x][0]);
for (int i = 1; i < g[x].size(); i++) {
int& y = g[x][i];
dfs_add(y);
}
add(x);
ans[x] = mex;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> p[i];
g[p[i]].push_back(i);
}
for (int i = 1; i <= n; i++) cin >> a[i];
dfs(1);
solve(1);
for (int i = 1; i <= n; i++) cout << ans[i] << " ";
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 16ms | 27744 KiB | ||||
2 | Accepted | 107ms | 36868 KiB | ||||
subtask2 | 5/5 | ||||||
3 | Accepted | 13ms | 27916 KiB | ||||
4 | Accepted | 13ms | 28124 KiB | ||||
5 | Accepted | 13ms | 28336 KiB | ||||
6 | Accepted | 13ms | 28548 KiB | ||||
subtask3 | 8/8 | ||||||
7 | Accepted | 13ms | 27916 KiB | ||||
8 | Accepted | 13ms | 28124 KiB | ||||
9 | Accepted | 13ms | 28336 KiB | ||||
10 | Accepted | 13ms | 28548 KiB | ||||
11 | Accepted | 68ms | 31040 KiB | ||||
12 | Accepted | 68ms | 31180 KiB | ||||
13 | Accepted | 67ms | 31168 KiB | ||||
14 | Accepted | 71ms | 31172 KiB | ||||
subtask4 | 12/12 | ||||||
15 | Accepted | 13ms | 27916 KiB | ||||
16 | Accepted | 13ms | 28124 KiB | ||||
17 | Accepted | 13ms | 28336 KiB | ||||
18 | Accepted | 13ms | 28548 KiB | ||||
19 | Accepted | 104ms | 72816 KiB | ||||
20 | Accepted | 107ms | 73020 KiB | ||||
21 | Accepted | 114ms | 73404 KiB | ||||
22 | Accepted | 118ms | 73464 KiB | ||||
23 | Accepted | 108ms | 73268 KiB | ||||
24 | Accepted | 108ms | 73256 KiB | ||||
subtask5 | 17/17 | ||||||
25 | Accepted | 13ms | 27916 KiB | ||||
26 | Accepted | 13ms | 28124 KiB | ||||
27 | Accepted | 13ms | 28336 KiB | ||||
28 | Accepted | 13ms | 28548 KiB | ||||
29 | Accepted | 18ms | 29476 KiB | ||||
30 | Accepted | 14ms | 30000 KiB | ||||
31 | Accepted | 16ms | 30188 KiB | ||||
32 | Accepted | 14ms | 29740 KiB | ||||
33 | Accepted | 16ms | 29776 KiB | ||||
34 | Accepted | 14ms | 29656 KiB | ||||
35 | Accepted | 14ms | 29616 KiB | ||||
36 | Accepted | 13ms | 29620 KiB | ||||
37 | Accepted | 14ms | 30072 KiB | ||||
38 | Accepted | 14ms | 29876 KiB | ||||
39 | Accepted | 14ms | 29924 KiB | ||||
40 | Accepted | 13ms | 29924 KiB | ||||
41 | Accepted | 13ms | 29960 KiB | ||||
subtask6 | 28/28 | ||||||
42 | Accepted | 108ms | 37020 KiB | ||||
43 | Accepted | 104ms | 40468 KiB | ||||
44 | Accepted | 104ms | 47472 KiB | ||||
45 | Accepted | 107ms | 56396 KiB | ||||
46 | Accepted | 108ms | 64368 KiB | ||||
47 | Accepted | 109ms | 70644 KiB | ||||
48 | Accepted | 71ms | 32320 KiB | ||||
49 | Accepted | 116ms | 74116 KiB | ||||
50 | Accepted | 115ms | 74088 KiB | ||||
subtask7 | 30/30 | ||||||
51 | Accepted | 12ms | 30124 KiB | ||||
52 | Accepted | 104ms | 38968 KiB | ||||
53 | Accepted | 13ms | 27916 KiB | ||||
54 | Accepted | 13ms | 28124 KiB | ||||
55 | Accepted | 13ms | 28336 KiB | ||||
56 | Accepted | 13ms | 28548 KiB | ||||
57 | Accepted | 68ms | 31040 KiB | ||||
58 | Accepted | 68ms | 31180 KiB | ||||
59 | Accepted | 67ms | 31168 KiB | ||||
60 | Accepted | 71ms | 31172 KiB | ||||
61 | Accepted | 104ms | 72816 KiB | ||||
62 | Accepted | 107ms | 73020 KiB | ||||
63 | Accepted | 114ms | 73404 KiB | ||||
64 | Accepted | 118ms | 73464 KiB | ||||
65 | Accepted | 108ms | 73268 KiB | ||||
66 | Accepted | 108ms | 73256 KiB | ||||
67 | Accepted | 18ms | 29476 KiB | ||||
68 | Accepted | 14ms | 30000 KiB | ||||
69 | Accepted | 16ms | 30188 KiB | ||||
70 | Accepted | 14ms | 29740 KiB | ||||
71 | Accepted | 16ms | 29776 KiB | ||||
72 | Accepted | 14ms | 29656 KiB | ||||
73 | Accepted | 14ms | 29616 KiB | ||||
74 | Accepted | 13ms | 29620 KiB | ||||
75 | Accepted | 14ms | 30072 KiB | ||||
76 | Accepted | 14ms | 29876 KiB | ||||
77 | Accepted | 14ms | 29924 KiB | ||||
78 | Accepted | 13ms | 29924 KiB | ||||
79 | Accepted | 13ms | 29960 KiB | ||||
80 | Accepted | 108ms | 37020 KiB | ||||
81 | Accepted | 104ms | 40468 KiB | ||||
82 | Accepted | 104ms | 47472 KiB | ||||
83 | Accepted | 107ms | 56396 KiB | ||||
84 | Accepted | 108ms | 64368 KiB | ||||
85 | Accepted | 109ms | 70644 KiB | ||||
86 | Accepted | 71ms | 32320 KiB | ||||
87 | Accepted | 116ms | 74116 KiB | ||||
88 | Accepted | 115ms | 74088 KiB | ||||
89 | Accepted | 163ms | 36748 KiB | ||||
90 | Accepted | 179ms | 36792 KiB | ||||
91 | Accepted | 165ms | 36860 KiB | ||||
92 | Accepted | 179ms | 36868 KiB | ||||
93 | Accepted | 118ms | 47364 KiB | ||||
94 | Accepted | 112ms | 56504 KiB | ||||
95 | Accepted | 116ms | 65408 KiB | ||||
96 | Accepted | 104ms | 40160 KiB | ||||
97 | Accepted | 108ms | 41640 KiB | ||||
98 | Accepted | 108ms | 44888 KiB | ||||
99 | Accepted | 109ms | 39440 KiB | ||||
100 | Accepted | 104ms | 43752 KiB | ||||
101 | Accepted | 112ms | 51884 KiB |