#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
const int MAXN = 5e5;
int N, K;
int dist[MAXN];
vector<int> adj[MAXN];
int group_of[MAXN];
vector<int> members[MAXN];
struct lcatreeitem {
int i;
lcatreeitem& operator=(int other) {
i = other;
return *this;
}
operator int() const {
return i;
}
bool operator<(const lcatreeitem& other) const {
if (i == -1) return false;
else if (other.i == -1) return true;
return dist[i] < dist[other.i];
}
} lcatree[1<<21];
int first[MAXN];
int lcatree_i = 0;
const int lcatree_shift = 1<<20;
int mergeuntil[MAXN];
bool penible[MAXN];
int uf[MAXN];
int root(int u) {
return uf[u] == u ? u : uf[u] = root(uf[u]);
}
void merge(int u, int v) {
if (dist[u] > dist[v]) {
swap(u, v);
}
uf[root(v)] = root(u);
}
void dfs(int u) {
first[u] = lcatree_i;
lcatree[lcatree_shift+lcatree_i++] = u;
for (int v : adj[u]) {
adj[v].erase(find(adj[v].begin(), adj[v].end(), u));
dist[v] = dist[u]+1;
dfs(v);
lcatree[lcatree_shift+lcatree_i++] = u;
}
}
int dfs2(int u) {
int ret = mergeuntil[u];
for (int v : adj[u]) {
int r = dfs2(v);
if (dist[r] < dist[ret]) ret = r;
if (r != v) {
merge(v, u);
}
}
return ret;
}
int lca(int u, int v) {
if (first[u] > first[v]) swap(u, v);
int l = lcatree_shift+first[u], r = lcatree_shift+first[v]+1;
lcatreeitem ret;
ret = -1;
for (; l < r; l >>= 1, r >>= 1) {
if (l&1) {
ret = min(ret, lcatree[l++]);
}
if (r&1) {
ret = min(ret, lcatree[--r]);
}
}
return ret;
}
int dfs3(int u) {
int ret = 0;
for (int v : adj[u]) {
ret += dfs3(v);
}
ret = max(ret, (int)penible[u]);
return ret;
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> N >> K;
iota(mergeuntil, mergeuntil+N, 0);
iota(uf, uf+N, 0);
int u, v;
for (int i = 0; i < N-1; i++) {
cin >> u >> v, u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 0; i < N; i++) {
cin >> group_of[i], group_of[i]--;
members[group_of[i]].push_back(i);
}
dfs(0);
for (int i = lcatree_shift-1; i > 0; i--) {
lcatree[i] = min(lcatree[2*i], lcatree[2*i+1]);
}
for (int g = 0; g < K; g++) {
int a = members[g][0];
for (auto m : members[g]) {
a = lca(a, m);
}
for (auto m : members[g]) {
mergeuntil[m] = a;
}
}
dfs2(0);
bool plus1 = false;
int lcapenible = -1;
for (int i = 1; i < N; i++) {
if (root(i) == i) {
if (lcapenible == -1) lcapenible = i;
else lcapenible = lca(lcapenible, i);
penible[i] = true;
}
}
for (int i = 1; i < N; i++) {
if (penible[i]) {
if (lca(lcapenible, i) == i) {
plus1 = true;
break;
}
}
}
int ans = dfs3(0)+plus1;
cout << ((ans+1)>>1) << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
27860 KB |
Output is correct |
2 |
Correct |
15 ms |
27860 KB |
Output is correct |
3 |
Correct |
16 ms |
27860 KB |
Output is correct |
4 |
Correct |
15 ms |
27848 KB |
Output is correct |
5 |
Correct |
15 ms |
27856 KB |
Output is correct |
6 |
Correct |
15 ms |
27860 KB |
Output is correct |
7 |
Correct |
15 ms |
27916 KB |
Output is correct |
8 |
Correct |
15 ms |
27916 KB |
Output is correct |
9 |
Correct |
15 ms |
27892 KB |
Output is correct |
10 |
Correct |
14 ms |
27860 KB |
Output is correct |
11 |
Correct |
15 ms |
27912 KB |
Output is correct |
12 |
Correct |
15 ms |
27908 KB |
Output is correct |
13 |
Correct |
15 ms |
27836 KB |
Output is correct |
14 |
Correct |
15 ms |
27860 KB |
Output is correct |
15 |
Correct |
15 ms |
27952 KB |
Output is correct |
16 |
Correct |
14 ms |
27912 KB |
Output is correct |
17 |
Correct |
17 ms |
28072 KB |
Output is correct |
18 |
Correct |
14 ms |
27860 KB |
Output is correct |
19 |
Correct |
14 ms |
27852 KB |
Output is correct |
20 |
Correct |
15 ms |
27860 KB |
Output is correct |
21 |
Correct |
14 ms |
27900 KB |
Output is correct |
22 |
Correct |
15 ms |
27912 KB |
Output is correct |
23 |
Correct |
15 ms |
27860 KB |
Output is correct |
24 |
Correct |
16 ms |
27916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
27860 KB |
Output is correct |
2 |
Correct |
15 ms |
27860 KB |
Output is correct |
3 |
Correct |
16 ms |
27860 KB |
Output is correct |
4 |
Correct |
15 ms |
27848 KB |
Output is correct |
5 |
Correct |
15 ms |
27856 KB |
Output is correct |
6 |
Correct |
15 ms |
27860 KB |
Output is correct |
7 |
Correct |
15 ms |
27916 KB |
Output is correct |
8 |
Correct |
15 ms |
27916 KB |
Output is correct |
9 |
Correct |
15 ms |
27892 KB |
Output is correct |
10 |
Correct |
14 ms |
27860 KB |
Output is correct |
11 |
Correct |
15 ms |
27912 KB |
Output is correct |
12 |
Correct |
15 ms |
27908 KB |
Output is correct |
13 |
Correct |
15 ms |
27836 KB |
Output is correct |
14 |
Correct |
15 ms |
27860 KB |
Output is correct |
15 |
Correct |
15 ms |
27952 KB |
Output is correct |
16 |
Correct |
14 ms |
27912 KB |
Output is correct |
17 |
Correct |
17 ms |
28072 KB |
Output is correct |
18 |
Correct |
14 ms |
27860 KB |
Output is correct |
19 |
Correct |
14 ms |
27852 KB |
Output is correct |
20 |
Correct |
15 ms |
27860 KB |
Output is correct |
21 |
Correct |
14 ms |
27900 KB |
Output is correct |
22 |
Correct |
15 ms |
27912 KB |
Output is correct |
23 |
Correct |
15 ms |
27860 KB |
Output is correct |
24 |
Correct |
16 ms |
27916 KB |
Output is correct |
25 |
Correct |
15 ms |
27912 KB |
Output is correct |
26 |
Correct |
16 ms |
28244 KB |
Output is correct |
27 |
Correct |
15 ms |
28124 KB |
Output is correct |
28 |
Correct |
16 ms |
28372 KB |
Output is correct |
29 |
Correct |
16 ms |
28284 KB |
Output is correct |
30 |
Correct |
16 ms |
28160 KB |
Output is correct |
31 |
Correct |
15 ms |
27860 KB |
Output is correct |
32 |
Correct |
17 ms |
28500 KB |
Output is correct |
33 |
Correct |
14 ms |
27860 KB |
Output is correct |
34 |
Correct |
16 ms |
28180 KB |
Output is correct |
35 |
Correct |
16 ms |
28244 KB |
Output is correct |
36 |
Correct |
15 ms |
28188 KB |
Output is correct |
37 |
Correct |
16 ms |
28180 KB |
Output is correct |
38 |
Correct |
14 ms |
27860 KB |
Output is correct |
39 |
Correct |
16 ms |
28144 KB |
Output is correct |
40 |
Correct |
16 ms |
28208 KB |
Output is correct |
41 |
Correct |
17 ms |
28160 KB |
Output is correct |
42 |
Correct |
16 ms |
28132 KB |
Output is correct |
43 |
Correct |
16 ms |
28384 KB |
Output is correct |
44 |
Correct |
15 ms |
27916 KB |
Output is correct |
45 |
Correct |
16 ms |
28244 KB |
Output is correct |
46 |
Correct |
17 ms |
28232 KB |
Output is correct |
47 |
Correct |
15 ms |
27916 KB |
Output is correct |
48 |
Correct |
15 ms |
28180 KB |
Output is correct |
49 |
Correct |
18 ms |
28244 KB |
Output is correct |
50 |
Correct |
16 ms |
28288 KB |
Output is correct |
51 |
Correct |
16 ms |
28216 KB |
Output is correct |
52 |
Correct |
16 ms |
28116 KB |
Output is correct |
53 |
Correct |
16 ms |
28244 KB |
Output is correct |
54 |
Correct |
16 ms |
28116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
27860 KB |
Output is correct |
2 |
Correct |
15 ms |
27860 KB |
Output is correct |
3 |
Correct |
16 ms |
27860 KB |
Output is correct |
4 |
Correct |
15 ms |
27848 KB |
Output is correct |
5 |
Correct |
15 ms |
27856 KB |
Output is correct |
6 |
Correct |
15 ms |
27860 KB |
Output is correct |
7 |
Correct |
15 ms |
27916 KB |
Output is correct |
8 |
Correct |
15 ms |
27916 KB |
Output is correct |
9 |
Correct |
15 ms |
27892 KB |
Output is correct |
10 |
Correct |
14 ms |
27860 KB |
Output is correct |
11 |
Correct |
15 ms |
27912 KB |
Output is correct |
12 |
Correct |
15 ms |
27908 KB |
Output is correct |
13 |
Correct |
15 ms |
27836 KB |
Output is correct |
14 |
Correct |
15 ms |
27860 KB |
Output is correct |
15 |
Correct |
15 ms |
27952 KB |
Output is correct |
16 |
Correct |
14 ms |
27912 KB |
Output is correct |
17 |
Correct |
17 ms |
28072 KB |
Output is correct |
18 |
Correct |
14 ms |
27860 KB |
Output is correct |
19 |
Correct |
14 ms |
27852 KB |
Output is correct |
20 |
Correct |
15 ms |
27860 KB |
Output is correct |
21 |
Correct |
14 ms |
27900 KB |
Output is correct |
22 |
Correct |
15 ms |
27912 KB |
Output is correct |
23 |
Correct |
15 ms |
27860 KB |
Output is correct |
24 |
Correct |
16 ms |
27916 KB |
Output is correct |
25 |
Correct |
14 ms |
27872 KB |
Output is correct |
26 |
Correct |
53 ms |
36104 KB |
Output is correct |
27 |
Correct |
66 ms |
35724 KB |
Output is correct |
28 |
Correct |
16 ms |
28160 KB |
Output is correct |
29 |
Correct |
16 ms |
27916 KB |
Output is correct |
30 |
Correct |
15 ms |
27860 KB |
Output is correct |
31 |
Correct |
73 ms |
35932 KB |
Output is correct |
32 |
Correct |
18 ms |
28184 KB |
Output is correct |
33 |
Correct |
77 ms |
42940 KB |
Output is correct |
34 |
Correct |
66 ms |
35904 KB |
Output is correct |
35 |
Correct |
16 ms |
28184 KB |
Output is correct |
36 |
Correct |
66 ms |
36572 KB |
Output is correct |
37 |
Correct |
17 ms |
28152 KB |
Output is correct |
38 |
Correct |
16 ms |
28104 KB |
Output is correct |
39 |
Correct |
54 ms |
36128 KB |
Output is correct |
40 |
Correct |
17 ms |
28372 KB |
Output is correct |
41 |
Correct |
63 ms |
35684 KB |
Output is correct |
42 |
Correct |
70 ms |
38192 KB |
Output is correct |
43 |
Correct |
15 ms |
27912 KB |
Output is correct |
44 |
Correct |
74 ms |
43204 KB |
Output is correct |
45 |
Correct |
69 ms |
39536 KB |
Output is correct |
46 |
Correct |
16 ms |
28220 KB |
Output is correct |
47 |
Correct |
17 ms |
28148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
36228 KB |
Output is correct |
2 |
Correct |
72 ms |
38784 KB |
Output is correct |
3 |
Correct |
18 ms |
28208 KB |
Output is correct |
4 |
Correct |
16 ms |
28116 KB |
Output is correct |
5 |
Correct |
15 ms |
27956 KB |
Output is correct |
6 |
Correct |
15 ms |
27916 KB |
Output is correct |
7 |
Correct |
16 ms |
28156 KB |
Output is correct |
8 |
Correct |
72 ms |
36932 KB |
Output is correct |
9 |
Correct |
18 ms |
28132 KB |
Output is correct |
10 |
Correct |
64 ms |
35920 KB |
Output is correct |
11 |
Correct |
15 ms |
27944 KB |
Output is correct |
12 |
Correct |
72 ms |
35848 KB |
Output is correct |
13 |
Correct |
76 ms |
36844 KB |
Output is correct |
14 |
Correct |
87 ms |
38456 KB |
Output is correct |
15 |
Correct |
54 ms |
36108 KB |
Output is correct |
16 |
Correct |
17 ms |
28244 KB |
Output is correct |
17 |
Correct |
16 ms |
27860 KB |
Output is correct |
18 |
Correct |
71 ms |
38540 KB |
Output is correct |
19 |
Correct |
95 ms |
42456 KB |
Output is correct |
20 |
Correct |
17 ms |
28116 KB |
Output is correct |
21 |
Correct |
16 ms |
27860 KB |
Output is correct |
22 |
Correct |
62 ms |
37232 KB |
Output is correct |
23 |
Correct |
17 ms |
28116 KB |
Output is correct |
24 |
Correct |
73 ms |
36240 KB |
Output is correct |
25 |
Correct |
105 ms |
41276 KB |
Output is correct |
26 |
Correct |
16 ms |
28180 KB |
Output is correct |
27 |
Correct |
16 ms |
28312 KB |
Output is correct |
28 |
Correct |
18 ms |
28108 KB |
Output is correct |
29 |
Correct |
16 ms |
28152 KB |
Output is correct |
30 |
Correct |
16 ms |
28172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
27860 KB |
Output is correct |
2 |
Correct |
15 ms |
27860 KB |
Output is correct |
3 |
Correct |
16 ms |
27860 KB |
Output is correct |
4 |
Correct |
15 ms |
27848 KB |
Output is correct |
5 |
Correct |
15 ms |
27856 KB |
Output is correct |
6 |
Correct |
15 ms |
27860 KB |
Output is correct |
7 |
Correct |
15 ms |
27916 KB |
Output is correct |
8 |
Correct |
15 ms |
27916 KB |
Output is correct |
9 |
Correct |
15 ms |
27892 KB |
Output is correct |
10 |
Correct |
14 ms |
27860 KB |
Output is correct |
11 |
Correct |
15 ms |
27912 KB |
Output is correct |
12 |
Correct |
15 ms |
27908 KB |
Output is correct |
13 |
Correct |
15 ms |
27836 KB |
Output is correct |
14 |
Correct |
15 ms |
27860 KB |
Output is correct |
15 |
Correct |
15 ms |
27952 KB |
Output is correct |
16 |
Correct |
14 ms |
27912 KB |
Output is correct |
17 |
Correct |
17 ms |
28072 KB |
Output is correct |
18 |
Correct |
14 ms |
27860 KB |
Output is correct |
19 |
Correct |
14 ms |
27852 KB |
Output is correct |
20 |
Correct |
15 ms |
27860 KB |
Output is correct |
21 |
Correct |
14 ms |
27900 KB |
Output is correct |
22 |
Correct |
15 ms |
27912 KB |
Output is correct |
23 |
Correct |
15 ms |
27860 KB |
Output is correct |
24 |
Correct |
16 ms |
27916 KB |
Output is correct |
25 |
Correct |
15 ms |
27912 KB |
Output is correct |
26 |
Correct |
16 ms |
28244 KB |
Output is correct |
27 |
Correct |
15 ms |
28124 KB |
Output is correct |
28 |
Correct |
16 ms |
28372 KB |
Output is correct |
29 |
Correct |
16 ms |
28284 KB |
Output is correct |
30 |
Correct |
16 ms |
28160 KB |
Output is correct |
31 |
Correct |
15 ms |
27860 KB |
Output is correct |
32 |
Correct |
17 ms |
28500 KB |
Output is correct |
33 |
Correct |
14 ms |
27860 KB |
Output is correct |
34 |
Correct |
16 ms |
28180 KB |
Output is correct |
35 |
Correct |
16 ms |
28244 KB |
Output is correct |
36 |
Correct |
15 ms |
28188 KB |
Output is correct |
37 |
Correct |
16 ms |
28180 KB |
Output is correct |
38 |
Correct |
14 ms |
27860 KB |
Output is correct |
39 |
Correct |
16 ms |
28144 KB |
Output is correct |
40 |
Correct |
16 ms |
28208 KB |
Output is correct |
41 |
Correct |
17 ms |
28160 KB |
Output is correct |
42 |
Correct |
16 ms |
28132 KB |
Output is correct |
43 |
Correct |
16 ms |
28384 KB |
Output is correct |
44 |
Correct |
15 ms |
27916 KB |
Output is correct |
45 |
Correct |
16 ms |
28244 KB |
Output is correct |
46 |
Correct |
17 ms |
28232 KB |
Output is correct |
47 |
Correct |
15 ms |
27916 KB |
Output is correct |
48 |
Correct |
15 ms |
28180 KB |
Output is correct |
49 |
Correct |
18 ms |
28244 KB |
Output is correct |
50 |
Correct |
16 ms |
28288 KB |
Output is correct |
51 |
Correct |
16 ms |
28216 KB |
Output is correct |
52 |
Correct |
16 ms |
28116 KB |
Output is correct |
53 |
Correct |
16 ms |
28244 KB |
Output is correct |
54 |
Correct |
16 ms |
28116 KB |
Output is correct |
55 |
Correct |
14 ms |
27872 KB |
Output is correct |
56 |
Correct |
53 ms |
36104 KB |
Output is correct |
57 |
Correct |
66 ms |
35724 KB |
Output is correct |
58 |
Correct |
16 ms |
28160 KB |
Output is correct |
59 |
Correct |
16 ms |
27916 KB |
Output is correct |
60 |
Correct |
15 ms |
27860 KB |
Output is correct |
61 |
Correct |
73 ms |
35932 KB |
Output is correct |
62 |
Correct |
18 ms |
28184 KB |
Output is correct |
63 |
Correct |
77 ms |
42940 KB |
Output is correct |
64 |
Correct |
66 ms |
35904 KB |
Output is correct |
65 |
Correct |
16 ms |
28184 KB |
Output is correct |
66 |
Correct |
66 ms |
36572 KB |
Output is correct |
67 |
Correct |
17 ms |
28152 KB |
Output is correct |
68 |
Correct |
16 ms |
28104 KB |
Output is correct |
69 |
Correct |
54 ms |
36128 KB |
Output is correct |
70 |
Correct |
17 ms |
28372 KB |
Output is correct |
71 |
Correct |
63 ms |
35684 KB |
Output is correct |
72 |
Correct |
70 ms |
38192 KB |
Output is correct |
73 |
Correct |
15 ms |
27912 KB |
Output is correct |
74 |
Correct |
74 ms |
43204 KB |
Output is correct |
75 |
Correct |
69 ms |
39536 KB |
Output is correct |
76 |
Correct |
16 ms |
28220 KB |
Output is correct |
77 |
Correct |
17 ms |
28148 KB |
Output is correct |
78 |
Correct |
55 ms |
36228 KB |
Output is correct |
79 |
Correct |
72 ms |
38784 KB |
Output is correct |
80 |
Correct |
18 ms |
28208 KB |
Output is correct |
81 |
Correct |
16 ms |
28116 KB |
Output is correct |
82 |
Correct |
15 ms |
27956 KB |
Output is correct |
83 |
Correct |
15 ms |
27916 KB |
Output is correct |
84 |
Correct |
16 ms |
28156 KB |
Output is correct |
85 |
Correct |
72 ms |
36932 KB |
Output is correct |
86 |
Correct |
18 ms |
28132 KB |
Output is correct |
87 |
Correct |
64 ms |
35920 KB |
Output is correct |
88 |
Correct |
15 ms |
27944 KB |
Output is correct |
89 |
Correct |
72 ms |
35848 KB |
Output is correct |
90 |
Correct |
76 ms |
36844 KB |
Output is correct |
91 |
Correct |
87 ms |
38456 KB |
Output is correct |
92 |
Correct |
54 ms |
36108 KB |
Output is correct |
93 |
Correct |
17 ms |
28244 KB |
Output is correct |
94 |
Correct |
16 ms |
27860 KB |
Output is correct |
95 |
Correct |
71 ms |
38540 KB |
Output is correct |
96 |
Correct |
95 ms |
42456 KB |
Output is correct |
97 |
Correct |
17 ms |
28116 KB |
Output is correct |
98 |
Correct |
16 ms |
27860 KB |
Output is correct |
99 |
Correct |
62 ms |
37232 KB |
Output is correct |
100 |
Correct |
17 ms |
28116 KB |
Output is correct |
101 |
Correct |
73 ms |
36240 KB |
Output is correct |
102 |
Correct |
105 ms |
41276 KB |
Output is correct |
103 |
Correct |
16 ms |
28180 KB |
Output is correct |
104 |
Correct |
16 ms |
28312 KB |
Output is correct |
105 |
Correct |
18 ms |
28108 KB |
Output is correct |
106 |
Correct |
16 ms |
28152 KB |
Output is correct |
107 |
Correct |
16 ms |
28172 KB |
Output is correct |
108 |
Correct |
373 ms |
67552 KB |
Output is correct |
109 |
Correct |
655 ms |
85476 KB |
Output is correct |
110 |
Correct |
663 ms |
96648 KB |
Output is correct |
111 |
Correct |
815 ms |
106976 KB |
Output is correct |
112 |
Correct |
711 ms |
97112 KB |
Output is correct |
113 |
Correct |
431 ms |
83732 KB |
Output is correct |
114 |
Correct |
501 ms |
67580 KB |
Output is correct |
115 |
Correct |
488 ms |
67512 KB |
Output is correct |
116 |
Correct |
522 ms |
72668 KB |
Output is correct |
117 |
Correct |
615 ms |
83832 KB |
Output is correct |
118 |
Correct |
456 ms |
69224 KB |
Output is correct |
119 |
Correct |
585 ms |
83792 KB |
Output is correct |
120 |
Correct |
678 ms |
97864 KB |
Output is correct |
121 |
Correct |
626 ms |
83916 KB |
Output is correct |
122 |
Correct |
584 ms |
77660 KB |
Output is correct |
123 |
Correct |
392 ms |
85344 KB |
Output is correct |
124 |
Correct |
721 ms |
82624 KB |
Output is correct |