#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <stdlib.h>
#include <map>
#include <random>
#include <cstring>
#include <functional>
#include <iomanip>
#include <cassert>
#include <queue>
#include <unordered_map>
#include <array>
using namespace std;
const int inf = 1e9;
vector<int> bfs(int v, vector<vector<int>> &g) {
const int n = g.size();
vector<int> dist(n, inf);
dist[v] = 0;
deque<int> q{v};
while (!q.empty()) {
int v = q.front();
q.pop_front();
for (int u : g[v]) {
if (dist[u] > dist[v] + 1) {
dist[u] = dist[v] + 1;
q.push_back(u);
}
}
}
return dist;
}
const int maxn = 1 << 18;
int md[maxn];
int d[maxn];
void dfs(int v, vector<vector<int>> &g, int p) {
md[v] = d[v];
for (int u : g[v]) {
if (u != p) {
d[u] = d[v] + 1;
dfs(u, g, v);
md[v] = max(md[v], md[u]);
}
}
}
int ans[maxn];
vector<int> c;
int lst[maxn];
int f[maxn];
int get(int i) {
int ans = 0;
while (i >= 0) {
ans += f[i];
i = (i & (i + 1)) - 1;
}
return ans;
}
void upd(int i, int x) {
while (i < maxn) {
f[i] += x;
i = i | (i + 1);
}
}
struct node {
int x, y, sz;
node*l = nullptr;
node*r = nullptr;
node(int x): x(x) {
y = rand();
sz = 1;
}
};
int s(node* v) {
if (!v) return 0;
return v->sz;
}
void upd(node* v) {
if (!v) return;
v->sz = 1 + s(v->l) + s(v->r);
}
node* merge(node*l, node*r) {
if (!l) return r;
if (!r) return l;
if (l->y > r->y) {
l->r = merge(l->r, r);
upd(l);
return l;
}
r->l = merge(l, r->l);
upd(r);
return r;
}
pair<node*, node*> split(node* v, int k) {
if (!v) return { nullptr, nullptr };
if (v->x <= k) {
pair<node*, node*> q = split(v->r, k);
v->r = q.first;
upd(v);
return { v, q.second };
}
pair<node*, node*> q = split(v->l, k);
v->l = q.second;
upd(v);
return { q.first, v };
}
node* r = nullptr;
void add(int x) {
node* t = new node(x);
r = merge(r, t);
}
void cut(int x) {
pair<node*, node*> q = split(r, x - 1);
r = q.first;
}
void dfs1(int v, vector<vector<int>> &g, int p) {
pair<int, int> mx = { -1, -1 };
for (int u : g[v]) {
if (u != p) {
if (mx.first == -1) {
mx.first = u;
} else {
if (md[mx.first] < md[u]) {
mx.second = mx.first;
mx.first = u;
} else if (mx.second == -1 || md[mx.second] < md[u]) {
mx.second = u;
}
}
}
}
int memlst = lst[c[v]];
pair<node*, node*> q = { nullptr, nullptr };
if (mx.second != -1) {
upd(max(0, d[v] * 2 - md[mx.second]), 1);
upd(d[v], -1);
q = split(r, d[v] * 2 - md[mx.second] - 1);
r = q.first;
if (lst[c[v]] == -1 || get(lst[c[v]])) {
add(d[v]);
lst[c[v]] = d[v];
}
dfs1(mx.first, g, v);
cut(d[v]);
upd(max(0, d[v] * 2 - md[mx.second]), -1);
upd(d[v], 1);
r = merge(r, q.second);
} else if (mx.first != -1) {
if (lst[c[v]] == -1 || get(lst[c[v]])) {
add(d[v]);
lst[c[v]] = d[v];
}
dfs1(mx.first, g, v);
cut(d[v]);
}
lst[c[v]] = memlst;
if (mx.first != -1) {
upd(max(0, d[v] * 2 - md[mx.first]), 1);
upd(d[v], -1);
q = split(r, d[v] * 2 - md[mx.first] - 1);
r = q.first;
ans[v] = s(r);
if (lst[c[v]] == -1 || get(lst[c[v]])) {
add(d[v]);
lst[c[v]] = d[v];
}
} else {
ans[v] = s(r);
}
for (int u : g[v]) {
if (u != p) {
if (u == mx.first) continue;
dfs1(u, g, v);
}
}
if (mx.first != -1) {
upd(max(0, d[v] * 2 - md[mx.first]), -1);
upd(d[v], 1);
cut(d[v]);
r = merge(r, q.second);
}
lst[c[v]] = memlst;
}
void solve() {
int n, m;
cin >> n >> m;
vector<vector<int>> g(n);
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
--u; --v;
g[u].push_back(v);
g[v].push_back(u);
}
c.resize(n);
for (int& i : c) {
cin >> i;
}
vector<int> x = bfs(0, g);
int i = max_element(x.begin(), x.end()) - x.begin();
x = bfs(i, g);
i = max_element(x.begin(), x.end()) - x.begin();
x = bfs(i, g);
int j = max_element(x.begin(), x.end()) - x.begin();
vector<int> answ(n, 0);
if (1) {
d[i] = 0;
dfs(i, g, i);
fill(lst, lst + maxn, -1);
fill(f, f + maxn, 0);
dfs1(i, g, i);
for (int i = 0; i < n; ++i) {
answ[i] = max(answ[i], ans[i]);
}
}
if (1) {
d[j] = 0;
dfs(j, g, j);
fill(f, f + maxn, 0);
dfs1(j, g, j);
for (int i = 0; i < n; ++i) {
answ[i] = max(answ[i], ans[i]);
}
}
for (int i : answ) {
cout << i << ' ';
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
t = 1;
while (t--) {
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2388 KB |
Output is correct |
2 |
Correct |
4 ms |
2652 KB |
Output is correct |
3 |
Correct |
3 ms |
2624 KB |
Output is correct |
4 |
Correct |
4 ms |
2772 KB |
Output is correct |
5 |
Correct |
4 ms |
2772 KB |
Output is correct |
6 |
Correct |
4 ms |
2772 KB |
Output is correct |
7 |
Correct |
4 ms |
2772 KB |
Output is correct |
8 |
Correct |
4 ms |
2644 KB |
Output is correct |
9 |
Correct |
4 ms |
2776 KB |
Output is correct |
10 |
Correct |
3 ms |
2624 KB |
Output is correct |
11 |
Correct |
4 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
2516 KB |
Output is correct |
13 |
Correct |
3 ms |
2772 KB |
Output is correct |
14 |
Correct |
3 ms |
2644 KB |
Output is correct |
15 |
Correct |
3 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2524 KB |
Output is correct |
17 |
Correct |
3 ms |
2772 KB |
Output is correct |
18 |
Correct |
3 ms |
2772 KB |
Output is correct |
19 |
Correct |
3 ms |
2772 KB |
Output is correct |
20 |
Correct |
4 ms |
3036 KB |
Output is correct |
21 |
Correct |
4 ms |
2900 KB |
Output is correct |
22 |
Correct |
3 ms |
2772 KB |
Output is correct |
23 |
Correct |
4 ms |
2760 KB |
Output is correct |
24 |
Correct |
4 ms |
2772 KB |
Output is correct |
25 |
Correct |
4 ms |
2780 KB |
Output is correct |
26 |
Correct |
3 ms |
2644 KB |
Output is correct |
27 |
Correct |
4 ms |
2900 KB |
Output is correct |
28 |
Correct |
4 ms |
2900 KB |
Output is correct |
29 |
Correct |
4 ms |
2908 KB |
Output is correct |
30 |
Correct |
2 ms |
2516 KB |
Output is correct |
31 |
Correct |
4 ms |
2772 KB |
Output is correct |
32 |
Correct |
5 ms |
3028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
162 ms |
18212 KB |
Output is correct |
2 |
Correct |
224 ms |
31200 KB |
Output is correct |
3 |
Correct |
32 ms |
8268 KB |
Output is correct |
4 |
Correct |
344 ms |
27692 KB |
Output is correct |
5 |
Correct |
466 ms |
52628 KB |
Output is correct |
6 |
Correct |
452 ms |
44744 KB |
Output is correct |
7 |
Correct |
327 ms |
26892 KB |
Output is correct |
8 |
Correct |
339 ms |
29408 KB |
Output is correct |
9 |
Correct |
340 ms |
30660 KB |
Output is correct |
10 |
Correct |
334 ms |
27772 KB |
Output is correct |
11 |
Correct |
139 ms |
23276 KB |
Output is correct |
12 |
Correct |
475 ms |
41748 KB |
Output is correct |
13 |
Correct |
367 ms |
37704 KB |
Output is correct |
14 |
Correct |
405 ms |
38092 KB |
Output is correct |
15 |
Correct |
120 ms |
22832 KB |
Output is correct |
16 |
Correct |
383 ms |
42456 KB |
Output is correct |
17 |
Correct |
406 ms |
41760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
294 ms |
35672 KB |
Output is correct |
2 |
Correct |
681 ms |
79104 KB |
Output is correct |
3 |
Correct |
52 ms |
12560 KB |
Output is correct |
4 |
Correct |
380 ms |
44528 KB |
Output is correct |
5 |
Correct |
706 ms |
82564 KB |
Output is correct |
6 |
Correct |
586 ms |
67160 KB |
Output is correct |
7 |
Correct |
365 ms |
42868 KB |
Output is correct |
8 |
Correct |
437 ms |
49488 KB |
Output is correct |
9 |
Correct |
409 ms |
46904 KB |
Output is correct |
10 |
Correct |
387 ms |
46196 KB |
Output is correct |
11 |
Correct |
271 ms |
37028 KB |
Output is correct |
12 |
Correct |
620 ms |
71532 KB |
Output is correct |
13 |
Correct |
459 ms |
53980 KB |
Output is correct |
14 |
Correct |
508 ms |
58280 KB |
Output is correct |
15 |
Correct |
125 ms |
23848 KB |
Output is correct |
16 |
Correct |
524 ms |
62708 KB |
Output is correct |
17 |
Correct |
535 ms |
61824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2388 KB |
Output is correct |
2 |
Correct |
4 ms |
2652 KB |
Output is correct |
3 |
Correct |
3 ms |
2624 KB |
Output is correct |
4 |
Correct |
4 ms |
2772 KB |
Output is correct |
5 |
Correct |
4 ms |
2772 KB |
Output is correct |
6 |
Correct |
4 ms |
2772 KB |
Output is correct |
7 |
Correct |
4 ms |
2772 KB |
Output is correct |
8 |
Correct |
4 ms |
2644 KB |
Output is correct |
9 |
Correct |
4 ms |
2776 KB |
Output is correct |
10 |
Correct |
3 ms |
2624 KB |
Output is correct |
11 |
Correct |
4 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
2516 KB |
Output is correct |
13 |
Correct |
3 ms |
2772 KB |
Output is correct |
14 |
Correct |
3 ms |
2644 KB |
Output is correct |
15 |
Correct |
3 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2524 KB |
Output is correct |
17 |
Correct |
3 ms |
2772 KB |
Output is correct |
18 |
Correct |
3 ms |
2772 KB |
Output is correct |
19 |
Correct |
3 ms |
2772 KB |
Output is correct |
20 |
Correct |
4 ms |
3036 KB |
Output is correct |
21 |
Correct |
4 ms |
2900 KB |
Output is correct |
22 |
Correct |
3 ms |
2772 KB |
Output is correct |
23 |
Correct |
4 ms |
2760 KB |
Output is correct |
24 |
Correct |
4 ms |
2772 KB |
Output is correct |
25 |
Correct |
4 ms |
2780 KB |
Output is correct |
26 |
Correct |
3 ms |
2644 KB |
Output is correct |
27 |
Correct |
4 ms |
2900 KB |
Output is correct |
28 |
Correct |
4 ms |
2900 KB |
Output is correct |
29 |
Correct |
4 ms |
2908 KB |
Output is correct |
30 |
Correct |
2 ms |
2516 KB |
Output is correct |
31 |
Correct |
4 ms |
2772 KB |
Output is correct |
32 |
Correct |
5 ms |
3028 KB |
Output is correct |
33 |
Correct |
162 ms |
18212 KB |
Output is correct |
34 |
Correct |
224 ms |
31200 KB |
Output is correct |
35 |
Correct |
32 ms |
8268 KB |
Output is correct |
36 |
Correct |
344 ms |
27692 KB |
Output is correct |
37 |
Correct |
466 ms |
52628 KB |
Output is correct |
38 |
Correct |
452 ms |
44744 KB |
Output is correct |
39 |
Correct |
327 ms |
26892 KB |
Output is correct |
40 |
Correct |
339 ms |
29408 KB |
Output is correct |
41 |
Correct |
340 ms |
30660 KB |
Output is correct |
42 |
Correct |
334 ms |
27772 KB |
Output is correct |
43 |
Correct |
139 ms |
23276 KB |
Output is correct |
44 |
Correct |
475 ms |
41748 KB |
Output is correct |
45 |
Correct |
367 ms |
37704 KB |
Output is correct |
46 |
Correct |
405 ms |
38092 KB |
Output is correct |
47 |
Correct |
120 ms |
22832 KB |
Output is correct |
48 |
Correct |
383 ms |
42456 KB |
Output is correct |
49 |
Correct |
406 ms |
41760 KB |
Output is correct |
50 |
Correct |
294 ms |
35672 KB |
Output is correct |
51 |
Correct |
681 ms |
79104 KB |
Output is correct |
52 |
Correct |
52 ms |
12560 KB |
Output is correct |
53 |
Correct |
380 ms |
44528 KB |
Output is correct |
54 |
Correct |
706 ms |
82564 KB |
Output is correct |
55 |
Correct |
586 ms |
67160 KB |
Output is correct |
56 |
Correct |
365 ms |
42868 KB |
Output is correct |
57 |
Correct |
437 ms |
49488 KB |
Output is correct |
58 |
Correct |
409 ms |
46904 KB |
Output is correct |
59 |
Correct |
387 ms |
46196 KB |
Output is correct |
60 |
Correct |
271 ms |
37028 KB |
Output is correct |
61 |
Correct |
620 ms |
71532 KB |
Output is correct |
62 |
Correct |
459 ms |
53980 KB |
Output is correct |
63 |
Correct |
508 ms |
58280 KB |
Output is correct |
64 |
Correct |
125 ms |
23848 KB |
Output is correct |
65 |
Correct |
524 ms |
62708 KB |
Output is correct |
66 |
Correct |
535 ms |
61824 KB |
Output is correct |
67 |
Correct |
32 ms |
7972 KB |
Output is correct |
68 |
Correct |
198 ms |
28868 KB |
Output is correct |
69 |
Correct |
289 ms |
36608 KB |
Output is correct |
70 |
Correct |
382 ms |
40384 KB |
Output is correct |
71 |
Correct |
498 ms |
52620 KB |
Output is correct |
72 |
Correct |
500 ms |
46760 KB |
Output is correct |
73 |
Correct |
352 ms |
39472 KB |
Output is correct |
74 |
Correct |
376 ms |
32908 KB |
Output is correct |
75 |
Correct |
347 ms |
34728 KB |
Output is correct |
76 |
Correct |
353 ms |
36740 KB |
Output is correct |
77 |
Correct |
235 ms |
32812 KB |
Output is correct |
78 |
Correct |
451 ms |
45024 KB |
Output is correct |
79 |
Correct |
367 ms |
41456 KB |
Output is correct |
80 |
Correct |
379 ms |
39716 KB |
Output is correct |
81 |
Correct |
113 ms |
22572 KB |
Output is correct |
82 |
Correct |
360 ms |
42392 KB |
Output is correct |
83 |
Correct |
406 ms |
41744 KB |
Output is correct |
84 |
Correct |
376 ms |
43844 KB |
Output is correct |
85 |
Correct |
489 ms |
53256 KB |
Output is correct |
86 |
Correct |
480 ms |
47840 KB |
Output is correct |
87 |
Correct |
360 ms |
42536 KB |
Output is correct |
88 |
Correct |
390 ms |
36460 KB |
Output is correct |
89 |
Correct |
385 ms |
42080 KB |
Output is correct |
90 |
Correct |
369 ms |
42052 KB |
Output is correct |
91 |
Correct |
236 ms |
34956 KB |
Output is correct |
92 |
Correct |
502 ms |
52432 KB |
Output is correct |
93 |
Correct |
400 ms |
39620 KB |
Output is correct |
94 |
Correct |
392 ms |
39008 KB |
Output is correct |
95 |
Correct |
119 ms |
23148 KB |
Output is correct |
96 |
Correct |
384 ms |
43076 KB |
Output is correct |
97 |
Correct |
447 ms |
42532 KB |
Output is correct |
98 |
Correct |
384 ms |
44716 KB |
Output is correct |
99 |
Correct |
530 ms |
54312 KB |
Output is correct |
100 |
Correct |
590 ms |
64184 KB |
Output is correct |
101 |
Correct |
349 ms |
42152 KB |
Output is correct |
102 |
Correct |
423 ms |
47788 KB |
Output is correct |
103 |
Correct |
400 ms |
46028 KB |
Output is correct |
104 |
Correct |
406 ms |
45828 KB |
Output is correct |
105 |
Correct |
253 ms |
33984 KB |
Output is correct |
106 |
Correct |
560 ms |
55860 KB |
Output is correct |
107 |
Correct |
434 ms |
44608 KB |
Output is correct |
108 |
Correct |
476 ms |
51636 KB |
Output is correct |
109 |
Correct |
122 ms |
23812 KB |
Output is correct |
110 |
Correct |
447 ms |
47716 KB |
Output is correct |
111 |
Correct |
539 ms |
57356 KB |
Output is correct |