#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
int S[515151], E[515151], dfsCnt = 0, P[515151][21], depth[515151];
vector<int>edge[515151];
void dfs(int w, int bef, int dep) {
S[w] = ++dfsCnt; P[w][0] = bef; depth[w] = dep;
for (int i = 0; P[w][i] != -1; i++)P[w][i + 1] = P[P[w][i]][i];
for (int nxt : edge[w]) if (nxt != bef)dfs(nxt, w, dep + 1);
E[w] = dfsCnt;
}
void go_up(int &x, int y) { for (int i = 0; i < 20; i++)if (y&(1 << i))x = P[x][i]; }
int get_lca(int a, int b) {
if (depth[a] < depth[b])go_up(b, depth[b] - depth[a]);
if (depth[a] > depth[b])go_up(a, depth[a] - depth[b]);
if (a == b)return a;
for (int i = 19; i >= 0; i--) if(P[a][i] != P[b][i]) { a = P[a][i]; b = P[b][i]; }
return P[a][0];
}
vector<pair<int, int>>L[515151];
int up[515151];
void dfs_uf(int w, int bef) {
for (int nxt : edge[w]) if (nxt != bef) {
dfs_uf(nxt, w);
up[w] = min(up[w], up[nxt]);
}
}
int par[515151];
int find(int x) {
if (par[x] == x)return x;
return par[x] = find(par[x]);
}
int indeg[515151];
int main() {
int n, k, i, j; scanf("%d%d", &n, &k);
for (i = 0; i < n - 1; i++) {
int s, e; scanf("%d%d", &s, &e);
edge[s].push_back(e); edge[e].push_back(s);
}
for (i = 1; i <= n; i++)for (j = 0; j < 20; j++)P[i][j] = -1;
dfs(1, -1, 1);
for (i = 1; i <= n; i++) {
int color; scanf("%d", &color);
L[color].push_back({ S[i], i });
}
for (i = 1; i <= n; i++)up[i] = 1e9;
for (i = 1; i <= k; i++) {
sort(L[i].begin(), L[i].end());
for (j = 0; j < L[i].size() - 1; j++){
int a = L[i][j].second, b = L[i][j + 1].second;
int lca = get_lca(a, b);
up[a] = min(up[a], depth[lca]);
up[b] = min(up[b], depth[lca]);
}
}
dfs_uf(1, -1);
for (i = 1; i <= n; i++)par[i] = i;
for (i = 1; i <= n; i++) if (depth[i] > up[i])par[find(i)] = find(P[i][0]);
for (i = 1; i <= n; i++) {
int s = i;
for (int e : edge[s]) {
int ps = find(s), pe = find(e);
if (ps==pe)continue;
indeg[ps]++; indeg[pe]++;
}
}
int res = 0;
for (i = 1; i <= n; i++)if (par[i] == i) {
if (indeg[i] <= 2)res++;
}
if (res == 1)printf("0");
else printf("%d", (res + 1) / 2);
return 0;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:51:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (j = 0; j < L[i].size() - 1; j++){
~~^~~~~~~~~~~~~~~~~
mergers.cpp:37:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int n, k, i, j; scanf("%d%d", &n, &k);
~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:39:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int s, e; scanf("%d%d", &s, &e);
~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:45:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int color; scanf("%d", &color);
~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
24568 KB |
Output is correct |
2 |
Correct |
20 ms |
24568 KB |
Output is correct |
3 |
Correct |
23 ms |
24568 KB |
Output is correct |
4 |
Correct |
22 ms |
24568 KB |
Output is correct |
5 |
Correct |
22 ms |
24568 KB |
Output is correct |
6 |
Correct |
21 ms |
24568 KB |
Output is correct |
7 |
Correct |
22 ms |
24568 KB |
Output is correct |
8 |
Correct |
20 ms |
24568 KB |
Output is correct |
9 |
Correct |
21 ms |
24568 KB |
Output is correct |
10 |
Correct |
21 ms |
24572 KB |
Output is correct |
11 |
Correct |
20 ms |
24568 KB |
Output is correct |
12 |
Correct |
23 ms |
24568 KB |
Output is correct |
13 |
Correct |
20 ms |
24568 KB |
Output is correct |
14 |
Correct |
22 ms |
24568 KB |
Output is correct |
15 |
Correct |
21 ms |
24696 KB |
Output is correct |
16 |
Correct |
19 ms |
24568 KB |
Output is correct |
17 |
Correct |
20 ms |
24568 KB |
Output is correct |
18 |
Correct |
21 ms |
24568 KB |
Output is correct |
19 |
Correct |
21 ms |
24568 KB |
Output is correct |
20 |
Correct |
20 ms |
24568 KB |
Output is correct |
21 |
Correct |
21 ms |
24568 KB |
Output is correct |
22 |
Correct |
21 ms |
24568 KB |
Output is correct |
23 |
Correct |
20 ms |
24568 KB |
Output is correct |
24 |
Correct |
21 ms |
24568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
24568 KB |
Output is correct |
2 |
Correct |
20 ms |
24568 KB |
Output is correct |
3 |
Correct |
23 ms |
24568 KB |
Output is correct |
4 |
Correct |
22 ms |
24568 KB |
Output is correct |
5 |
Correct |
22 ms |
24568 KB |
Output is correct |
6 |
Correct |
21 ms |
24568 KB |
Output is correct |
7 |
Correct |
22 ms |
24568 KB |
Output is correct |
8 |
Correct |
20 ms |
24568 KB |
Output is correct |
9 |
Correct |
21 ms |
24568 KB |
Output is correct |
10 |
Correct |
21 ms |
24572 KB |
Output is correct |
11 |
Correct |
20 ms |
24568 KB |
Output is correct |
12 |
Correct |
23 ms |
24568 KB |
Output is correct |
13 |
Correct |
20 ms |
24568 KB |
Output is correct |
14 |
Correct |
22 ms |
24568 KB |
Output is correct |
15 |
Correct |
21 ms |
24696 KB |
Output is correct |
16 |
Correct |
19 ms |
24568 KB |
Output is correct |
17 |
Correct |
20 ms |
24568 KB |
Output is correct |
18 |
Correct |
21 ms |
24568 KB |
Output is correct |
19 |
Correct |
21 ms |
24568 KB |
Output is correct |
20 |
Correct |
20 ms |
24568 KB |
Output is correct |
21 |
Correct |
21 ms |
24568 KB |
Output is correct |
22 |
Correct |
21 ms |
24568 KB |
Output is correct |
23 |
Correct |
20 ms |
24568 KB |
Output is correct |
24 |
Correct |
21 ms |
24568 KB |
Output is correct |
25 |
Correct |
22 ms |
24568 KB |
Output is correct |
26 |
Correct |
23 ms |
25080 KB |
Output is correct |
27 |
Correct |
27 ms |
25080 KB |
Output is correct |
28 |
Correct |
23 ms |
25208 KB |
Output is correct |
29 |
Correct |
24 ms |
25080 KB |
Output is correct |
30 |
Correct |
24 ms |
25080 KB |
Output is correct |
31 |
Correct |
20 ms |
24568 KB |
Output is correct |
32 |
Correct |
23 ms |
25208 KB |
Output is correct |
33 |
Correct |
20 ms |
24568 KB |
Output is correct |
34 |
Correct |
24 ms |
25080 KB |
Output is correct |
35 |
Correct |
23 ms |
25080 KB |
Output is correct |
36 |
Correct |
24 ms |
25080 KB |
Output is correct |
37 |
Correct |
24 ms |
25080 KB |
Output is correct |
38 |
Correct |
25 ms |
24568 KB |
Output is correct |
39 |
Correct |
23 ms |
25080 KB |
Output is correct |
40 |
Correct |
25 ms |
25080 KB |
Output is correct |
41 |
Correct |
23 ms |
25080 KB |
Output is correct |
42 |
Correct |
23 ms |
25084 KB |
Output is correct |
43 |
Correct |
28 ms |
25208 KB |
Output is correct |
44 |
Correct |
22 ms |
24568 KB |
Output is correct |
45 |
Correct |
23 ms |
25208 KB |
Output is correct |
46 |
Correct |
23 ms |
25080 KB |
Output is correct |
47 |
Correct |
21 ms |
24568 KB |
Output is correct |
48 |
Correct |
24 ms |
25080 KB |
Output is correct |
49 |
Correct |
24 ms |
25080 KB |
Output is correct |
50 |
Correct |
24 ms |
25080 KB |
Output is correct |
51 |
Correct |
24 ms |
25080 KB |
Output is correct |
52 |
Correct |
23 ms |
25080 KB |
Output is correct |
53 |
Correct |
24 ms |
25080 KB |
Output is correct |
54 |
Correct |
23 ms |
25080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
24568 KB |
Output is correct |
2 |
Correct |
20 ms |
24568 KB |
Output is correct |
3 |
Correct |
23 ms |
24568 KB |
Output is correct |
4 |
Correct |
22 ms |
24568 KB |
Output is correct |
5 |
Correct |
22 ms |
24568 KB |
Output is correct |
6 |
Correct |
21 ms |
24568 KB |
Output is correct |
7 |
Correct |
22 ms |
24568 KB |
Output is correct |
8 |
Correct |
20 ms |
24568 KB |
Output is correct |
9 |
Correct |
21 ms |
24568 KB |
Output is correct |
10 |
Correct |
21 ms |
24572 KB |
Output is correct |
11 |
Correct |
20 ms |
24568 KB |
Output is correct |
12 |
Correct |
23 ms |
24568 KB |
Output is correct |
13 |
Correct |
20 ms |
24568 KB |
Output is correct |
14 |
Correct |
22 ms |
24568 KB |
Output is correct |
15 |
Correct |
21 ms |
24696 KB |
Output is correct |
16 |
Correct |
19 ms |
24568 KB |
Output is correct |
17 |
Correct |
20 ms |
24568 KB |
Output is correct |
18 |
Correct |
21 ms |
24568 KB |
Output is correct |
19 |
Correct |
21 ms |
24568 KB |
Output is correct |
20 |
Correct |
20 ms |
24568 KB |
Output is correct |
21 |
Correct |
21 ms |
24568 KB |
Output is correct |
22 |
Correct |
21 ms |
24568 KB |
Output is correct |
23 |
Correct |
20 ms |
24568 KB |
Output is correct |
24 |
Correct |
21 ms |
24568 KB |
Output is correct |
25 |
Correct |
23 ms |
24568 KB |
Output is correct |
26 |
Correct |
120 ms |
40816 KB |
Output is correct |
27 |
Correct |
164 ms |
40312 KB |
Output is correct |
28 |
Correct |
23 ms |
25080 KB |
Output is correct |
29 |
Correct |
20 ms |
24568 KB |
Output is correct |
30 |
Correct |
25 ms |
24568 KB |
Output is correct |
31 |
Correct |
157 ms |
40440 KB |
Output is correct |
32 |
Correct |
22 ms |
25080 KB |
Output is correct |
33 |
Correct |
201 ms |
46072 KB |
Output is correct |
34 |
Correct |
156 ms |
40440 KB |
Output is correct |
35 |
Correct |
24 ms |
25084 KB |
Output is correct |
36 |
Correct |
162 ms |
41080 KB |
Output is correct |
37 |
Correct |
23 ms |
25080 KB |
Output is correct |
38 |
Correct |
23 ms |
25080 KB |
Output is correct |
39 |
Correct |
111 ms |
40560 KB |
Output is correct |
40 |
Correct |
23 ms |
25208 KB |
Output is correct |
41 |
Correct |
153 ms |
40176 KB |
Output is correct |
42 |
Correct |
185 ms |
42232 KB |
Output is correct |
43 |
Correct |
22 ms |
24568 KB |
Output is correct |
44 |
Correct |
195 ms |
46188 KB |
Output is correct |
45 |
Correct |
189 ms |
43380 KB |
Output is correct |
46 |
Correct |
23 ms |
25080 KB |
Output is correct |
47 |
Correct |
23 ms |
24952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
40688 KB |
Output is correct |
2 |
Correct |
121 ms |
43772 KB |
Output is correct |
3 |
Correct |
24 ms |
25080 KB |
Output is correct |
4 |
Correct |
23 ms |
25080 KB |
Output is correct |
5 |
Correct |
20 ms |
24568 KB |
Output is correct |
6 |
Correct |
20 ms |
24568 KB |
Output is correct |
7 |
Correct |
23 ms |
25080 KB |
Output is correct |
8 |
Correct |
169 ms |
41976 KB |
Output is correct |
9 |
Correct |
23 ms |
25080 KB |
Output is correct |
10 |
Correct |
149 ms |
40952 KB |
Output is correct |
11 |
Correct |
21 ms |
24568 KB |
Output is correct |
12 |
Correct |
160 ms |
40952 KB |
Output is correct |
13 |
Correct |
165 ms |
41976 KB |
Output is correct |
14 |
Correct |
146 ms |
43256 KB |
Output is correct |
15 |
Correct |
111 ms |
40560 KB |
Output is correct |
16 |
Correct |
24 ms |
25080 KB |
Output is correct |
17 |
Correct |
20 ms |
24568 KB |
Output is correct |
18 |
Correct |
125 ms |
43248 KB |
Output is correct |
19 |
Correct |
173 ms |
46328 KB |
Output is correct |
20 |
Correct |
23 ms |
25080 KB |
Output is correct |
21 |
Correct |
20 ms |
24568 KB |
Output is correct |
22 |
Correct |
122 ms |
42092 KB |
Output is correct |
23 |
Correct |
24 ms |
25080 KB |
Output is correct |
24 |
Correct |
175 ms |
41468 KB |
Output is correct |
25 |
Correct |
173 ms |
45304 KB |
Output is correct |
26 |
Correct |
23 ms |
25080 KB |
Output is correct |
27 |
Correct |
23 ms |
25208 KB |
Output is correct |
28 |
Correct |
24 ms |
25080 KB |
Output is correct |
29 |
Correct |
22 ms |
25080 KB |
Output is correct |
30 |
Correct |
23 ms |
25080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
24568 KB |
Output is correct |
2 |
Correct |
20 ms |
24568 KB |
Output is correct |
3 |
Correct |
23 ms |
24568 KB |
Output is correct |
4 |
Correct |
22 ms |
24568 KB |
Output is correct |
5 |
Correct |
22 ms |
24568 KB |
Output is correct |
6 |
Correct |
21 ms |
24568 KB |
Output is correct |
7 |
Correct |
22 ms |
24568 KB |
Output is correct |
8 |
Correct |
20 ms |
24568 KB |
Output is correct |
9 |
Correct |
21 ms |
24568 KB |
Output is correct |
10 |
Correct |
21 ms |
24572 KB |
Output is correct |
11 |
Correct |
20 ms |
24568 KB |
Output is correct |
12 |
Correct |
23 ms |
24568 KB |
Output is correct |
13 |
Correct |
20 ms |
24568 KB |
Output is correct |
14 |
Correct |
22 ms |
24568 KB |
Output is correct |
15 |
Correct |
21 ms |
24696 KB |
Output is correct |
16 |
Correct |
19 ms |
24568 KB |
Output is correct |
17 |
Correct |
20 ms |
24568 KB |
Output is correct |
18 |
Correct |
21 ms |
24568 KB |
Output is correct |
19 |
Correct |
21 ms |
24568 KB |
Output is correct |
20 |
Correct |
20 ms |
24568 KB |
Output is correct |
21 |
Correct |
21 ms |
24568 KB |
Output is correct |
22 |
Correct |
21 ms |
24568 KB |
Output is correct |
23 |
Correct |
20 ms |
24568 KB |
Output is correct |
24 |
Correct |
21 ms |
24568 KB |
Output is correct |
25 |
Correct |
22 ms |
24568 KB |
Output is correct |
26 |
Correct |
23 ms |
25080 KB |
Output is correct |
27 |
Correct |
27 ms |
25080 KB |
Output is correct |
28 |
Correct |
23 ms |
25208 KB |
Output is correct |
29 |
Correct |
24 ms |
25080 KB |
Output is correct |
30 |
Correct |
24 ms |
25080 KB |
Output is correct |
31 |
Correct |
20 ms |
24568 KB |
Output is correct |
32 |
Correct |
23 ms |
25208 KB |
Output is correct |
33 |
Correct |
20 ms |
24568 KB |
Output is correct |
34 |
Correct |
24 ms |
25080 KB |
Output is correct |
35 |
Correct |
23 ms |
25080 KB |
Output is correct |
36 |
Correct |
24 ms |
25080 KB |
Output is correct |
37 |
Correct |
24 ms |
25080 KB |
Output is correct |
38 |
Correct |
25 ms |
24568 KB |
Output is correct |
39 |
Correct |
23 ms |
25080 KB |
Output is correct |
40 |
Correct |
25 ms |
25080 KB |
Output is correct |
41 |
Correct |
23 ms |
25080 KB |
Output is correct |
42 |
Correct |
23 ms |
25084 KB |
Output is correct |
43 |
Correct |
28 ms |
25208 KB |
Output is correct |
44 |
Correct |
22 ms |
24568 KB |
Output is correct |
45 |
Correct |
23 ms |
25208 KB |
Output is correct |
46 |
Correct |
23 ms |
25080 KB |
Output is correct |
47 |
Correct |
21 ms |
24568 KB |
Output is correct |
48 |
Correct |
24 ms |
25080 KB |
Output is correct |
49 |
Correct |
24 ms |
25080 KB |
Output is correct |
50 |
Correct |
24 ms |
25080 KB |
Output is correct |
51 |
Correct |
24 ms |
25080 KB |
Output is correct |
52 |
Correct |
23 ms |
25080 KB |
Output is correct |
53 |
Correct |
24 ms |
25080 KB |
Output is correct |
54 |
Correct |
23 ms |
25080 KB |
Output is correct |
55 |
Correct |
23 ms |
24568 KB |
Output is correct |
56 |
Correct |
120 ms |
40816 KB |
Output is correct |
57 |
Correct |
164 ms |
40312 KB |
Output is correct |
58 |
Correct |
23 ms |
25080 KB |
Output is correct |
59 |
Correct |
20 ms |
24568 KB |
Output is correct |
60 |
Correct |
25 ms |
24568 KB |
Output is correct |
61 |
Correct |
157 ms |
40440 KB |
Output is correct |
62 |
Correct |
22 ms |
25080 KB |
Output is correct |
63 |
Correct |
201 ms |
46072 KB |
Output is correct |
64 |
Correct |
156 ms |
40440 KB |
Output is correct |
65 |
Correct |
24 ms |
25084 KB |
Output is correct |
66 |
Correct |
162 ms |
41080 KB |
Output is correct |
67 |
Correct |
23 ms |
25080 KB |
Output is correct |
68 |
Correct |
23 ms |
25080 KB |
Output is correct |
69 |
Correct |
111 ms |
40560 KB |
Output is correct |
70 |
Correct |
23 ms |
25208 KB |
Output is correct |
71 |
Correct |
153 ms |
40176 KB |
Output is correct |
72 |
Correct |
185 ms |
42232 KB |
Output is correct |
73 |
Correct |
22 ms |
24568 KB |
Output is correct |
74 |
Correct |
195 ms |
46188 KB |
Output is correct |
75 |
Correct |
189 ms |
43380 KB |
Output is correct |
76 |
Correct |
23 ms |
25080 KB |
Output is correct |
77 |
Correct |
23 ms |
24952 KB |
Output is correct |
78 |
Correct |
107 ms |
40688 KB |
Output is correct |
79 |
Correct |
121 ms |
43772 KB |
Output is correct |
80 |
Correct |
24 ms |
25080 KB |
Output is correct |
81 |
Correct |
23 ms |
25080 KB |
Output is correct |
82 |
Correct |
20 ms |
24568 KB |
Output is correct |
83 |
Correct |
20 ms |
24568 KB |
Output is correct |
84 |
Correct |
23 ms |
25080 KB |
Output is correct |
85 |
Correct |
169 ms |
41976 KB |
Output is correct |
86 |
Correct |
23 ms |
25080 KB |
Output is correct |
87 |
Correct |
149 ms |
40952 KB |
Output is correct |
88 |
Correct |
21 ms |
24568 KB |
Output is correct |
89 |
Correct |
160 ms |
40952 KB |
Output is correct |
90 |
Correct |
165 ms |
41976 KB |
Output is correct |
91 |
Correct |
146 ms |
43256 KB |
Output is correct |
92 |
Correct |
111 ms |
40560 KB |
Output is correct |
93 |
Correct |
24 ms |
25080 KB |
Output is correct |
94 |
Correct |
20 ms |
24568 KB |
Output is correct |
95 |
Correct |
125 ms |
43248 KB |
Output is correct |
96 |
Correct |
173 ms |
46328 KB |
Output is correct |
97 |
Correct |
23 ms |
25080 KB |
Output is correct |
98 |
Correct |
20 ms |
24568 KB |
Output is correct |
99 |
Correct |
122 ms |
42092 KB |
Output is correct |
100 |
Correct |
24 ms |
25080 KB |
Output is correct |
101 |
Correct |
175 ms |
41468 KB |
Output is correct |
102 |
Correct |
173 ms |
45304 KB |
Output is correct |
103 |
Correct |
23 ms |
25080 KB |
Output is correct |
104 |
Correct |
23 ms |
25208 KB |
Output is correct |
105 |
Correct |
24 ms |
25080 KB |
Output is correct |
106 |
Correct |
22 ms |
25080 KB |
Output is correct |
107 |
Correct |
23 ms |
25080 KB |
Output is correct |
108 |
Correct |
770 ms |
113508 KB |
Output is correct |
109 |
Correct |
1270 ms |
117672 KB |
Output is correct |
110 |
Correct |
1361 ms |
126712 KB |
Output is correct |
111 |
Correct |
1358 ms |
137592 KB |
Output is correct |
112 |
Correct |
1284 ms |
129784 KB |
Output is correct |
113 |
Correct |
758 ms |
119388 KB |
Output is correct |
114 |
Correct |
1012 ms |
103504 KB |
Output is correct |
115 |
Correct |
985 ms |
103312 KB |
Output is correct |
116 |
Correct |
1175 ms |
110584 KB |
Output is correct |
117 |
Correct |
1029 ms |
119416 KB |
Output is correct |
118 |
Correct |
982 ms |
106340 KB |
Output is correct |
119 |
Correct |
1042 ms |
119416 KB |
Output is correct |
120 |
Correct |
1323 ms |
130344 KB |
Output is correct |
121 |
Correct |
1027 ms |
119416 KB |
Output is correct |
122 |
Correct |
1136 ms |
113532 KB |
Output is correct |
123 |
Correct |
716 ms |
120932 KB |
Output is correct |
124 |
Correct |
1136 ms |
117368 KB |
Output is correct |