#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;
};
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 low = 0, high = dep[v] - 1, d = -1;
while (low <= high) {
int mid = (low + high) >> 1;
if (Query(1, 0, n - 1, mid, dep[v] - 1) == 1) {
d = mid;
low = mid + 1;
} else {
high = mid - 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 low = 0, high = dep[v] - 1, d = -1;
while (low <= high) {
int mid = (low + high) >> 1;
if (Query(1, 0, n - 1, mid, dep[v] - 1) == 1) {
d = mid;
low = mid + 1;
} else {
high = mid - 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
9 ms |
1200 KB |
Output is correct |
3 |
Correct |
5 ms |
1116 KB |
Output is correct |
4 |
Correct |
13 ms |
1372 KB |
Output is correct |
5 |
Correct |
8 ms |
1112 KB |
Output is correct |
6 |
Correct |
13 ms |
1628 KB |
Output is correct |
7 |
Correct |
14 ms |
1360 KB |
Output is correct |
8 |
Correct |
7 ms |
1116 KB |
Output is correct |
9 |
Correct |
10 ms |
1120 KB |
Output is correct |
10 |
Correct |
9 ms |
1116 KB |
Output is correct |
11 |
Correct |
9 ms |
1112 KB |
Output is correct |
12 |
Correct |
6 ms |
1112 KB |
Output is correct |
13 |
Correct |
16 ms |
1456 KB |
Output is correct |
14 |
Correct |
13 ms |
1116 KB |
Output is correct |
15 |
Correct |
13 ms |
1272 KB |
Output is correct |
16 |
Correct |
5 ms |
1116 KB |
Output is correct |
17 |
Correct |
14 ms |
1456 KB |
Output is correct |
18 |
Correct |
18 ms |
1368 KB |
Output is correct |
19 |
Correct |
8 ms |
1116 KB |
Output is correct |
20 |
Correct |
14 ms |
1876 KB |
Output is correct |
21 |
Correct |
14 ms |
1368 KB |
Output is correct |
22 |
Correct |
7 ms |
1188 KB |
Output is correct |
23 |
Correct |
10 ms |
1372 KB |
Output is correct |
24 |
Correct |
9 ms |
1300 KB |
Output is correct |
25 |
Correct |
9 ms |
1144 KB |
Output is correct |
26 |
Correct |
6 ms |
1320 KB |
Output is correct |
27 |
Correct |
14 ms |
1628 KB |
Output is correct |
28 |
Correct |
15 ms |
1624 KB |
Output is correct |
29 |
Correct |
14 ms |
1372 KB |
Output is correct |
30 |
Correct |
5 ms |
1336 KB |
Output is correct |
31 |
Correct |
18 ms |
1424 KB |
Output is correct |
32 |
Correct |
14 ms |
1368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
890 ms |
30608 KB |
Output is correct |
2 |
Correct |
1936 ms |
64372 KB |
Output is correct |
3 |
Correct |
376 ms |
12052 KB |
Output is correct |
4 |
Correct |
1780 ms |
52664 KB |
Output is correct |
5 |
Execution timed out |
2045 ms |
111608 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1365 ms |
73964 KB |
Output is correct |
2 |
Execution timed out |
2048 ms |
149720 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
9 ms |
1200 KB |
Output is correct |
3 |
Correct |
5 ms |
1116 KB |
Output is correct |
4 |
Correct |
13 ms |
1372 KB |
Output is correct |
5 |
Correct |
8 ms |
1112 KB |
Output is correct |
6 |
Correct |
13 ms |
1628 KB |
Output is correct |
7 |
Correct |
14 ms |
1360 KB |
Output is correct |
8 |
Correct |
7 ms |
1116 KB |
Output is correct |
9 |
Correct |
10 ms |
1120 KB |
Output is correct |
10 |
Correct |
9 ms |
1116 KB |
Output is correct |
11 |
Correct |
9 ms |
1112 KB |
Output is correct |
12 |
Correct |
6 ms |
1112 KB |
Output is correct |
13 |
Correct |
16 ms |
1456 KB |
Output is correct |
14 |
Correct |
13 ms |
1116 KB |
Output is correct |
15 |
Correct |
13 ms |
1272 KB |
Output is correct |
16 |
Correct |
5 ms |
1116 KB |
Output is correct |
17 |
Correct |
14 ms |
1456 KB |
Output is correct |
18 |
Correct |
18 ms |
1368 KB |
Output is correct |
19 |
Correct |
8 ms |
1116 KB |
Output is correct |
20 |
Correct |
14 ms |
1876 KB |
Output is correct |
21 |
Correct |
14 ms |
1368 KB |
Output is correct |
22 |
Correct |
7 ms |
1188 KB |
Output is correct |
23 |
Correct |
10 ms |
1372 KB |
Output is correct |
24 |
Correct |
9 ms |
1300 KB |
Output is correct |
25 |
Correct |
9 ms |
1144 KB |
Output is correct |
26 |
Correct |
6 ms |
1320 KB |
Output is correct |
27 |
Correct |
14 ms |
1628 KB |
Output is correct |
28 |
Correct |
15 ms |
1624 KB |
Output is correct |
29 |
Correct |
14 ms |
1372 KB |
Output is correct |
30 |
Correct |
5 ms |
1336 KB |
Output is correct |
31 |
Correct |
18 ms |
1424 KB |
Output is correct |
32 |
Correct |
14 ms |
1368 KB |
Output is correct |
33 |
Correct |
890 ms |
30608 KB |
Output is correct |
34 |
Correct |
1936 ms |
64372 KB |
Output is correct |
35 |
Correct |
376 ms |
12052 KB |
Output is correct |
36 |
Correct |
1780 ms |
52664 KB |
Output is correct |
37 |
Execution timed out |
2045 ms |
111608 KB |
Time limit exceeded |
38 |
Halted |
0 ms |
0 KB |
- |