#include <stdio.h>
#define N 200000
#define M (2*N-2)
#define N_ N+1
#define M_ M+1
#define K N
int n, k, color[N], answer;
/* graph */
int head[N], nxt[M_], vv[M_];
/* node with color */
int head_[K], nxt_[N_], vv_[N_];
void link(int*head, int*nxt, int*vv, int*i, int u, int v)
{
nxt[i[0]] = head[u];
vv[i[0]] = v;
head[u] = i[0]++;
}
int dead[N], sz[N];
void dfs2(int u, int p)
{
sz[u] = 1;
for (int j = head[u]; j; j = nxt[j])
{
if (vv[j] == p || dead[vv[j]])
continue;
dfs2(vv[j], u);
sz[u] += sz[vv[j]];
}
}
int par[N], visit[K], dfreq[K], cfreq[K];
void dfs3(int u, int p)
{
visit[color[u]] = -1;
++dfreq[color[u]];
par[u] = p;
for (int j = head[u]; j; j = nxt[j])
{
if (vv[j] == p || dead[vv[j]])
continue;
dfs3(vv[j], u);
}
}
void dfs4(int u, int p)
{
visit[color[u]] = -1;
dfreq[color[u]] = 0;
for (int j = head[u]; j; j = nxt[j])
{
if (vv[j] == p || dead[vv[j]])
continue;
dfs4(vv[j], u);
}
}
int get_centroid(int u, int p, int tsize)
{
for (int j = head[u]; j; j = nxt[j])
if (!dead[vv[j]] && vv[j] != p && sz[vv[j]] * 2 >= tsize)
return get_centroid(vv[j], u, tsize);
return u;
}
void centroid_decomposition(int u)
{
dfs2(u, u);
u = get_centroid(u, u, sz[u]);
dead[u] = 1;
dfs3(u, u);
int col = color[u];
if (dfreq[col] == cfreq[col])
{
static int qu[N], head, tail;
head = tail = 0;
int dcolor = 0;
for (int at, jj = head_[col]; jj; jj = nxt_[jj])
qu[head++] = vv_[jj];
visit[col] = 1;
while (head-tail)
{
int uu = qu[tail++];
if (uu == u || visit[color[par[uu]]] == 1)
continue;
int nextcol = color[par[uu]];
if (cfreq[nextcol] != dfreq[nextcol])
goto NOTOK;
++dcolor;
for (int jj = head_[nextcol]; jj; jj = nxt_[jj])
qu[head++] = vv_[jj];
visit[nextcol] = 1;
}
if (dcolor < answer)
answer = dcolor;
NOTOK:;
}
dfs4(u, u);
for (int j = head[u]; j; j = nxt[j])
if (!dead[vv[j]])
centroid_decomposition(vv[j]);
}
int main()
{
scanf("%d%d", &n, &k);
answer = n;
for (int i = 1, u, v, eo[1] = { 1 }; i < n; ++i)
{
scanf("%d%d", &u, &v); --u, --v;
link(head, nxt, vv, eo, u, v);
link(head, nxt, vv, eo, v, u);
}
for (int eo[1] = { 1 }, i = 0; i < n; ++i)
{
scanf("%d", color+i);
++cfreq[--color[i]];
link(head_, nxt_, vv_, eo, color[i], i);
}
centroid_decomposition(0);
printf("%d", answer);
}
Compilation message
capital_city.c: In function 'centroid_decomposition':
capital_city.c:87:18: warning: unused variable 'at' [-Wunused-variable]
87 | for (int at, jj = head_[col]; jj; jj = nxt_[jj])
| ^~
capital_city.c: In function 'main':
capital_city.c:118:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
118 | scanf("%d%d", &n, &k);
| ^~~~~~~~~~~~~~~~~~~~~
capital_city.c:122:9: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
122 | scanf("%d%d", &u, &v); --u, --v;
| ^~~~~~~~~~~~~~~~~~~~~
capital_city.c:128:9: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
128 | scanf("%d", color+i);
| ^~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10688 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10688 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
3 ms |
10588 KB |
Output is correct |
15 |
Correct |
3 ms |
10736 KB |
Output is correct |
16 |
Correct |
3 ms |
10588 KB |
Output is correct |
17 |
Correct |
3 ms |
10684 KB |
Output is correct |
18 |
Correct |
2 ms |
10588 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
2 ms |
10584 KB |
Output is correct |
23 |
Correct |
2 ms |
10588 KB |
Output is correct |
24 |
Correct |
2 ms |
10588 KB |
Output is correct |
25 |
Correct |
3 ms |
10588 KB |
Output is correct |
26 |
Correct |
3 ms |
10588 KB |
Output is correct |
27 |
Correct |
3 ms |
10588 KB |
Output is correct |
28 |
Correct |
3 ms |
10588 KB |
Output is correct |
29 |
Correct |
3 ms |
10584 KB |
Output is correct |
30 |
Correct |
3 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
229 ms |
23632 KB |
Output is correct |
2 |
Correct |
149 ms |
24244 KB |
Output is correct |
3 |
Correct |
253 ms |
23632 KB |
Output is correct |
4 |
Correct |
130 ms |
24244 KB |
Output is correct |
5 |
Correct |
240 ms |
21332 KB |
Output is correct |
6 |
Correct |
129 ms |
24272 KB |
Output is correct |
7 |
Correct |
247 ms |
21584 KB |
Output is correct |
8 |
Correct |
133 ms |
24300 KB |
Output is correct |
9 |
Correct |
335 ms |
20964 KB |
Output is correct |
10 |
Correct |
292 ms |
18928 KB |
Output is correct |
11 |
Correct |
307 ms |
21316 KB |
Output is correct |
12 |
Correct |
320 ms |
23428 KB |
Output is correct |
13 |
Correct |
296 ms |
18516 KB |
Output is correct |
14 |
Correct |
313 ms |
23632 KB |
Output is correct |
15 |
Correct |
303 ms |
23380 KB |
Output is correct |
16 |
Correct |
306 ms |
19440 KB |
Output is correct |
17 |
Correct |
294 ms |
19848 KB |
Output is correct |
18 |
Correct |
300 ms |
19952 KB |
Output is correct |
19 |
Correct |
310 ms |
22508 KB |
Output is correct |
20 |
Correct |
299 ms |
24144 KB |
Output is correct |
21 |
Correct |
1 ms |
6488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10688 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
3 ms |
10588 KB |
Output is correct |
15 |
Correct |
3 ms |
10736 KB |
Output is correct |
16 |
Correct |
3 ms |
10588 KB |
Output is correct |
17 |
Correct |
3 ms |
10684 KB |
Output is correct |
18 |
Correct |
2 ms |
10588 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
2 ms |
10584 KB |
Output is correct |
23 |
Correct |
2 ms |
10588 KB |
Output is correct |
24 |
Correct |
2 ms |
10588 KB |
Output is correct |
25 |
Correct |
3 ms |
10588 KB |
Output is correct |
26 |
Correct |
3 ms |
10588 KB |
Output is correct |
27 |
Correct |
3 ms |
10588 KB |
Output is correct |
28 |
Correct |
3 ms |
10588 KB |
Output is correct |
29 |
Correct |
3 ms |
10584 KB |
Output is correct |
30 |
Correct |
3 ms |
10588 KB |
Output is correct |
31 |
Correct |
229 ms |
23632 KB |
Output is correct |
32 |
Correct |
149 ms |
24244 KB |
Output is correct |
33 |
Correct |
253 ms |
23632 KB |
Output is correct |
34 |
Correct |
130 ms |
24244 KB |
Output is correct |
35 |
Correct |
240 ms |
21332 KB |
Output is correct |
36 |
Correct |
129 ms |
24272 KB |
Output is correct |
37 |
Correct |
247 ms |
21584 KB |
Output is correct |
38 |
Correct |
133 ms |
24300 KB |
Output is correct |
39 |
Correct |
335 ms |
20964 KB |
Output is correct |
40 |
Correct |
292 ms |
18928 KB |
Output is correct |
41 |
Correct |
307 ms |
21316 KB |
Output is correct |
42 |
Correct |
320 ms |
23428 KB |
Output is correct |
43 |
Correct |
296 ms |
18516 KB |
Output is correct |
44 |
Correct |
313 ms |
23632 KB |
Output is correct |
45 |
Correct |
303 ms |
23380 KB |
Output is correct |
46 |
Correct |
306 ms |
19440 KB |
Output is correct |
47 |
Correct |
294 ms |
19848 KB |
Output is correct |
48 |
Correct |
300 ms |
19952 KB |
Output is correct |
49 |
Correct |
310 ms |
22508 KB |
Output is correct |
50 |
Correct |
299 ms |
24144 KB |
Output is correct |
51 |
Correct |
1 ms |
6488 KB |
Output is correct |
52 |
Correct |
187 ms |
11924 KB |
Output is correct |
53 |
Correct |
195 ms |
11856 KB |
Output is correct |
54 |
Correct |
201 ms |
11920 KB |
Output is correct |
55 |
Correct |
196 ms |
11860 KB |
Output is correct |
56 |
Correct |
210 ms |
11748 KB |
Output is correct |
57 |
Correct |
189 ms |
12088 KB |
Output is correct |
58 |
Correct |
184 ms |
13928 KB |
Output is correct |
59 |
Correct |
212 ms |
14168 KB |
Output is correct |
60 |
Correct |
267 ms |
13204 KB |
Output is correct |
61 |
Correct |
256 ms |
12836 KB |
Output is correct |
62 |
Correct |
132 ms |
24148 KB |
Output is correct |
63 |
Correct |
132 ms |
24148 KB |
Output is correct |
64 |
Correct |
150 ms |
24076 KB |
Output is correct |
65 |
Correct |
134 ms |
24244 KB |
Output is correct |
66 |
Correct |
147 ms |
18032 KB |
Output is correct |
67 |
Correct |
174 ms |
17964 KB |
Output is correct |
68 |
Correct |
160 ms |
18036 KB |
Output is correct |
69 |
Correct |
170 ms |
17972 KB |
Output is correct |
70 |
Correct |
164 ms |
17956 KB |
Output is correct |
71 |
Correct |
168 ms |
18032 KB |
Output is correct |
72 |
Correct |
153 ms |
18020 KB |
Output is correct |
73 |
Correct |
173 ms |
17252 KB |
Output is correct |
74 |
Correct |
153 ms |
18004 KB |
Output is correct |
75 |
Correct |
143 ms |
18036 KB |
Output is correct |
76 |
Correct |
222 ms |
16552 KB |
Output is correct |
77 |
Correct |
210 ms |
14992 KB |
Output is correct |
78 |
Correct |
321 ms |
19820 KB |
Output is correct |
79 |
Correct |
320 ms |
18264 KB |
Output is correct |
80 |
Correct |
380 ms |
23960 KB |
Output is correct |
81 |
Correct |
304 ms |
21072 KB |
Output is correct |
82 |
Correct |
302 ms |
21164 KB |
Output is correct |
83 |
Correct |
290 ms |
18684 KB |
Output is correct |
84 |
Correct |
405 ms |
23204 KB |
Output is correct |
85 |
Correct |
334 ms |
22004 KB |
Output is correct |
86 |
Correct |
354 ms |
18096 KB |
Output is correct |
87 |
Correct |
330 ms |
19676 KB |
Output is correct |
88 |
Correct |
284 ms |
20412 KB |
Output is correct |
89 |
Correct |
282 ms |
16980 KB |
Output is correct |
90 |
Correct |
311 ms |
17108 KB |
Output is correct |
91 |
Correct |
277 ms |
18972 KB |
Output is correct |
92 |
Correct |
301 ms |
17760 KB |
Output is correct |
93 |
Correct |
291 ms |
17748 KB |
Output is correct |
94 |
Correct |
363 ms |
17176 KB |
Output is correct |
95 |
Correct |
277 ms |
18352 KB |
Output is correct |
96 |
Correct |
270 ms |
17268 KB |
Output is correct |
97 |
Correct |
287 ms |
18876 KB |
Output is correct |