#include <bits/stdc++.h>
using namespace std;
const int MAX = (int) (8e6);
int rt[MAX], ch[MAX][2], cnt[MAX], tsz;
void Change(int& x, int y, int l, int r, int p) {
x = ++tsz;
ch[x][0] = ch[y][0];
ch[x][1] = ch[y][1];
cnt[x] = cnt[y];
if (l == r) {
cnt[x] = 1;
return;
}
int mid = (l + r) >> 1;
if (p <= mid) {
Change(ch[x][0], ch[y][0], l, mid, p);
} else {
Change(ch[x][1], ch[y][1], mid + 1, r, p);
}
cnt[x] = cnt[ch[x][0]] + cnt[ch[x][1]];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<int>> g(n);
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
--x; --y;
g[x].push_back(y);
g[y].push_back(x);
}
vector<int> c(n);
for (int i = 0; i < n; i++) {
cin >> c[i];
--c[i];
}
auto Bfs = [&](int v) {
vector<int> d(n, -1);
vector<int> que(1, v);
d[v] = 0;
for (int b = 0; b < (int) que.size(); b++) {
int i = que[b];
for (int j : g[i]) {
if (d[j] == -1) {
d[j] = d[i] + 1;
que.push_back(j);
}
}
}
return d;
};
int x, y;
{
vector<int> d = Bfs(0);
x = 0;
for (int i = 1; i < n; i++) {
if (d[i] > d[x]) {
x = i;
}
}
}
{
vector<int> d = Bfs(x);
y = 0;
for (int i = 1; i < n; i++) {
if (d[i] > d[y]) {
y = i;
}
}
}
vector<vector<int>> d(2);
d[0] = Bfs(x);
d[1] = Bfs(y);
vector<int> res(n);
for (int foo = 0; foo < 2; foo++) {
int root = (foo == 0 ? x : y);
const int L = 18;
vector<int> mx(n);
vector<vector<int>> pr(n, vector<int>(L));
vector<int> dep(n);
function<void(int, int)> Dfs = [&](int v, int pv) {
pr[v][0] = pv;
for (int i = 1; i < L; i++) {
pr[v][i] = pr[pr[v][i - 1]][i - 1];
}
mx[v] = 1;
for (int u : g[v]) {
if (u == pv) {
continue;
}
dep[u] = dep[v] + 1;
Dfs(u, v);
mx[v] = max(mx[v], mx[u] + 1);
}
};
auto Jump = [&](int v, int k) {
for (int i = L - 1; i >= 0; i--) {
if (k >> i & 1) {
v = pr[v][i];
}
}
return v;
};
Dfs(root, root);
vector<int> st(8 * n, 1);
vector<int> lzy(8 * n);
auto Push = [&](int x) {
st[x * 2] += lzy[x];
st[x * 2 + 1] += lzy[x];
lzy[x * 2] += lzy[x];
lzy[x * 2 + 1] += lzy[x];
lzy[x] = 0;
};
function<void(int, int, int, int, int, int)> Modify = [&](int x, int l, int r, int ll, int rr, int v) {
if (ll <= l && r <= rr) {
st[x] += v;
lzy[x] += v;
Push(x);
return;
}
Push(x);
int mid = (l + r) >> 1;
if (rr <= mid) {
Modify(x * 2, l, mid, ll, rr, v);
} else if (ll > mid) {
Modify(x * 2 + 1, mid + 1, r, ll, rr, v);
} else {
Modify(x * 2, l, mid, ll, rr, v);
Modify(x * 2 + 1, mid + 1, r, ll, rr, v);
}
st[x] = max(st[x * 2], st[x * 2 + 1]);
};
function<int(int, int, int, int, int)> Query = [&](int x, int l, int r, int ll, int rr) {
if (ll <= l && r <= rr) {
return st[x];
}
Push(x);
int mid = (l + r) >> 1;
int res;
if (rr <= mid) {
res = Query(x * 2, l, mid, ll, rr);
} else if (ll > mid) {
res = Query(x * 2 + 1, mid + 1, r, ll, rr);
} else {
res = max(Query(x * 2, l, mid, ll, rr), Query(x * 2 + 1, mid + 1, r, ll, rr));
}
st[x] = max(st[x * 2], st[x * 2 + 1]);
return res;
};
function<int(int, int, int, int)> Walk = [&](int x, int l, int r, int p) {
if (l > p || st[x] < 1) {
return -1;
}
if (l == r) {
return l;
}
Push(x);
int mid = (l + r) >> 1;
int ret = Walk(x * 2 + 1, mid + 1, r, p);
if (ret == -1) {
ret = Walk(x * 2, l, mid, p);
}
st[x] = max(st[x * 2], st[x * 2 + 1]);
return ret;
};
auto Add = [&](int v, int d, int p) {
Modify(1, 0, n - 1, max(0, dep[v] - d + 1), dep[v], p);
};
while (tsz > 0) {
ch[tsz][0] = 0;
ch[tsz][1] = 0;
cnt[tsz] = 0;
tsz--;
}
for (int i = 0; i < n; i++) {
rt[i] = 0;
}
function<void(int, int)> Solve = [&](int v, int pv) {
if (d[foo][v] >= d[foo ^ 1][v]) {
Add(v, mx[v], -1);
int d = (dep[v] == 0 ? -1 : Walk(1, 0, n - 1, dep[v] - 1));
if (d == -1) {
res[v] = 0;
} else {
res[v] = cnt[rt[Jump(v, dep[v] - d)]];
}
Add(v, mx[v], +1);
}
multiset<int> st;
for (int u : g[v]) {
if (u == pv) {
continue;
}
st.insert(mx[u]);
}
for (int u : g[v]) {
if (u == pv) {
continue;
}
st.erase(st.find(mx[u]));
if (!st.empty()) {
Add(pr[v][0], *prev(st.end()), -1);
}
int d = (dep[v] == 0 ? -1 : Walk(1, 0, n - 1, dep[v] - 1));
rt[v] = 0;
if (d != -1) {
rt[v] = rt[Jump(v, dep[v] - d)];
}
Change(rt[v], rt[v], 0, m - 1, c[v]);
Solve(u, v);
if (!st.empty()) {
Add(pr[v][0], *prev(st.end()), +1);
}
st.insert(mx[u]);
}
};
Solve(root, root);
}
for (int i = 0; i < n; i++) {
cout << res[i] << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
4 ms |
1116 KB |
Output is correct |
3 |
Correct |
2 ms |
860 KB |
Output is correct |
4 |
Correct |
4 ms |
1372 KB |
Output is correct |
5 |
Correct |
4 ms |
860 KB |
Output is correct |
6 |
Correct |
5 ms |
1628 KB |
Output is correct |
7 |
Correct |
5 ms |
1116 KB |
Output is correct |
8 |
Correct |
5 ms |
1100 KB |
Output is correct |
9 |
Correct |
5 ms |
1116 KB |
Output is correct |
10 |
Correct |
4 ms |
1116 KB |
Output is correct |
11 |
Correct |
4 ms |
1116 KB |
Output is correct |
12 |
Correct |
5 ms |
1116 KB |
Output is correct |
13 |
Correct |
4 ms |
1372 KB |
Output is correct |
14 |
Correct |
5 ms |
1076 KB |
Output is correct |
15 |
Correct |
5 ms |
1116 KB |
Output is correct |
16 |
Correct |
6 ms |
1116 KB |
Output is correct |
17 |
Correct |
6 ms |
1372 KB |
Output is correct |
18 |
Correct |
4 ms |
1116 KB |
Output is correct |
19 |
Correct |
4 ms |
1116 KB |
Output is correct |
20 |
Correct |
4 ms |
1632 KB |
Output is correct |
21 |
Correct |
5 ms |
1372 KB |
Output is correct |
22 |
Correct |
4 ms |
1116 KB |
Output is correct |
23 |
Correct |
5 ms |
1116 KB |
Output is correct |
24 |
Correct |
4 ms |
1116 KB |
Output is correct |
25 |
Correct |
5 ms |
1116 KB |
Output is correct |
26 |
Correct |
5 ms |
1116 KB |
Output is correct |
27 |
Correct |
5 ms |
1628 KB |
Output is correct |
28 |
Correct |
5 ms |
1628 KB |
Output is correct |
29 |
Correct |
5 ms |
1372 KB |
Output is correct |
30 |
Correct |
5 ms |
1116 KB |
Output is correct |
31 |
Correct |
6 ms |
1372 KB |
Output is correct |
32 |
Correct |
6 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
390 ms |
30608 KB |
Output is correct |
2 |
Correct |
455 ms |
62412 KB |
Output is correct |
3 |
Correct |
81 ms |
11700 KB |
Output is correct |
4 |
Correct |
753 ms |
52604 KB |
Output is correct |
5 |
Correct |
977 ms |
108440 KB |
Output is correct |
6 |
Correct |
983 ms |
83624 KB |
Output is correct |
7 |
Correct |
765 ms |
57116 KB |
Output is correct |
8 |
Correct |
799 ms |
62380 KB |
Output is correct |
9 |
Correct |
747 ms |
60676 KB |
Output is correct |
10 |
Correct |
798 ms |
60220 KB |
Output is correct |
11 |
Correct |
920 ms |
65048 KB |
Output is correct |
12 |
Correct |
1033 ms |
93088 KB |
Output is correct |
13 |
Correct |
984 ms |
86308 KB |
Output is correct |
14 |
Correct |
940 ms |
83676 KB |
Output is correct |
15 |
Correct |
1032 ms |
65312 KB |
Output is correct |
16 |
Correct |
944 ms |
96412 KB |
Output is correct |
17 |
Correct |
1010 ms |
85912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
657 ms |
73976 KB |
Output is correct |
2 |
Correct |
1154 ms |
143256 KB |
Output is correct |
3 |
Correct |
104 ms |
19284 KB |
Output is correct |
4 |
Correct |
874 ms |
97904 KB |
Output is correct |
5 |
Correct |
1209 ms |
154008 KB |
Output is correct |
6 |
Correct |
1095 ms |
126184 KB |
Output is correct |
7 |
Correct |
888 ms |
99488 KB |
Output is correct |
8 |
Correct |
999 ms |
111256 KB |
Output is correct |
9 |
Correct |
942 ms |
107260 KB |
Output is correct |
10 |
Correct |
927 ms |
103504 KB |
Output is correct |
11 |
Correct |
1031 ms |
103384 KB |
Output is correct |
12 |
Correct |
1282 ms |
145064 KB |
Output is correct |
13 |
Correct |
1128 ms |
125216 KB |
Output is correct |
14 |
Correct |
1163 ms |
125480 KB |
Output is correct |
15 |
Correct |
1110 ms |
108712 KB |
Output is correct |
16 |
Correct |
1117 ms |
138736 KB |
Output is correct |
17 |
Correct |
1093 ms |
128868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
4 ms |
1116 KB |
Output is correct |
3 |
Correct |
2 ms |
860 KB |
Output is correct |
4 |
Correct |
4 ms |
1372 KB |
Output is correct |
5 |
Correct |
4 ms |
860 KB |
Output is correct |
6 |
Correct |
5 ms |
1628 KB |
Output is correct |
7 |
Correct |
5 ms |
1116 KB |
Output is correct |
8 |
Correct |
5 ms |
1100 KB |
Output is correct |
9 |
Correct |
5 ms |
1116 KB |
Output is correct |
10 |
Correct |
4 ms |
1116 KB |
Output is correct |
11 |
Correct |
4 ms |
1116 KB |
Output is correct |
12 |
Correct |
5 ms |
1116 KB |
Output is correct |
13 |
Correct |
4 ms |
1372 KB |
Output is correct |
14 |
Correct |
5 ms |
1076 KB |
Output is correct |
15 |
Correct |
5 ms |
1116 KB |
Output is correct |
16 |
Correct |
6 ms |
1116 KB |
Output is correct |
17 |
Correct |
6 ms |
1372 KB |
Output is correct |
18 |
Correct |
4 ms |
1116 KB |
Output is correct |
19 |
Correct |
4 ms |
1116 KB |
Output is correct |
20 |
Correct |
4 ms |
1632 KB |
Output is correct |
21 |
Correct |
5 ms |
1372 KB |
Output is correct |
22 |
Correct |
4 ms |
1116 KB |
Output is correct |
23 |
Correct |
5 ms |
1116 KB |
Output is correct |
24 |
Correct |
4 ms |
1116 KB |
Output is correct |
25 |
Correct |
5 ms |
1116 KB |
Output is correct |
26 |
Correct |
5 ms |
1116 KB |
Output is correct |
27 |
Correct |
5 ms |
1628 KB |
Output is correct |
28 |
Correct |
5 ms |
1628 KB |
Output is correct |
29 |
Correct |
5 ms |
1372 KB |
Output is correct |
30 |
Correct |
5 ms |
1116 KB |
Output is correct |
31 |
Correct |
6 ms |
1372 KB |
Output is correct |
32 |
Correct |
6 ms |
1372 KB |
Output is correct |
33 |
Correct |
390 ms |
30608 KB |
Output is correct |
34 |
Correct |
455 ms |
62412 KB |
Output is correct |
35 |
Correct |
81 ms |
11700 KB |
Output is correct |
36 |
Correct |
753 ms |
52604 KB |
Output is correct |
37 |
Correct |
977 ms |
108440 KB |
Output is correct |
38 |
Correct |
983 ms |
83624 KB |
Output is correct |
39 |
Correct |
765 ms |
57116 KB |
Output is correct |
40 |
Correct |
799 ms |
62380 KB |
Output is correct |
41 |
Correct |
747 ms |
60676 KB |
Output is correct |
42 |
Correct |
798 ms |
60220 KB |
Output is correct |
43 |
Correct |
920 ms |
65048 KB |
Output is correct |
44 |
Correct |
1033 ms |
93088 KB |
Output is correct |
45 |
Correct |
984 ms |
86308 KB |
Output is correct |
46 |
Correct |
940 ms |
83676 KB |
Output is correct |
47 |
Correct |
1032 ms |
65312 KB |
Output is correct |
48 |
Correct |
944 ms |
96412 KB |
Output is correct |
49 |
Correct |
1010 ms |
85912 KB |
Output is correct |
50 |
Correct |
657 ms |
73976 KB |
Output is correct |
51 |
Correct |
1154 ms |
143256 KB |
Output is correct |
52 |
Correct |
104 ms |
19284 KB |
Output is correct |
53 |
Correct |
874 ms |
97904 KB |
Output is correct |
54 |
Correct |
1209 ms |
154008 KB |
Output is correct |
55 |
Correct |
1095 ms |
126184 KB |
Output is correct |
56 |
Correct |
888 ms |
99488 KB |
Output is correct |
57 |
Correct |
999 ms |
111256 KB |
Output is correct |
58 |
Correct |
942 ms |
107260 KB |
Output is correct |
59 |
Correct |
927 ms |
103504 KB |
Output is correct |
60 |
Correct |
1031 ms |
103384 KB |
Output is correct |
61 |
Correct |
1282 ms |
145064 KB |
Output is correct |
62 |
Correct |
1128 ms |
125216 KB |
Output is correct |
63 |
Correct |
1163 ms |
125480 KB |
Output is correct |
64 |
Correct |
1110 ms |
108712 KB |
Output is correct |
65 |
Correct |
1117 ms |
138736 KB |
Output is correct |
66 |
Correct |
1093 ms |
128868 KB |
Output is correct |
67 |
Correct |
76 ms |
12372 KB |
Output is correct |
68 |
Correct |
392 ms |
63960 KB |
Output is correct |
69 |
Correct |
557 ms |
73188 KB |
Output is correct |
70 |
Correct |
729 ms |
63344 KB |
Output is correct |
71 |
Correct |
931 ms |
113640 KB |
Output is correct |
72 |
Correct |
934 ms |
90784 KB |
Output is correct |
73 |
Correct |
740 ms |
62612 KB |
Output is correct |
74 |
Correct |
891 ms |
70816 KB |
Output is correct |
75 |
Correct |
818 ms |
67744 KB |
Output is correct |
76 |
Correct |
835 ms |
69032 KB |
Output is correct |
77 |
Correct |
923 ms |
67684 KB |
Output is correct |
78 |
Correct |
1056 ms |
103336 KB |
Output is correct |
79 |
Correct |
950 ms |
98716 KB |
Output is correct |
80 |
Correct |
1049 ms |
86952 KB |
Output is correct |
81 |
Correct |
983 ms |
72476 KB |
Output is correct |
82 |
Correct |
948 ms |
100916 KB |
Output is correct |
83 |
Correct |
1004 ms |
91968 KB |
Output is correct |
84 |
Correct |
770 ms |
73376 KB |
Output is correct |
85 |
Correct |
999 ms |
129064 KB |
Output is correct |
86 |
Correct |
966 ms |
103064 KB |
Output is correct |
87 |
Correct |
764 ms |
74460 KB |
Output is correct |
88 |
Correct |
889 ms |
86184 KB |
Output is correct |
89 |
Correct |
863 ms |
84120 KB |
Output is correct |
90 |
Correct |
919 ms |
79776 KB |
Output is correct |
91 |
Correct |
828 ms |
80416 KB |
Output is correct |
92 |
Correct |
1063 ms |
131748 KB |
Output is correct |
93 |
Correct |
1037 ms |
98924 KB |
Output is correct |
94 |
Correct |
979 ms |
90968 KB |
Output is correct |
95 |
Correct |
997 ms |
84672 KB |
Output is correct |
96 |
Correct |
984 ms |
113844 KB |
Output is correct |
97 |
Correct |
993 ms |
106528 KB |
Output is correct |
98 |
Correct |
806 ms |
98080 KB |
Output is correct |
99 |
Correct |
1033 ms |
139424 KB |
Output is correct |
100 |
Correct |
1048 ms |
124708 KB |
Output is correct |
101 |
Correct |
956 ms |
92956 KB |
Output is correct |
102 |
Correct |
909 ms |
101544 KB |
Output is correct |
103 |
Correct |
863 ms |
99244 KB |
Output is correct |
104 |
Correct |
861 ms |
96988 KB |
Output is correct |
105 |
Correct |
954 ms |
95008 KB |
Output is correct |
106 |
Correct |
1166 ms |
129064 KB |
Output is correct |
107 |
Correct |
1101 ms |
125228 KB |
Output is correct |
108 |
Correct |
1079 ms |
117056 KB |
Output is correct |
109 |
Correct |
1117 ms |
106328 KB |
Output is correct |
110 |
Correct |
1108 ms |
132556 KB |
Output is correct |
111 |
Correct |
1085 ms |
126240 KB |
Output is correct |