#include <bits/stdc++.h>
#define eb emplace_back
const int inf=987654321;
using namespace std;
typedef long long LL;
int n, k, col[200010], siz[200010];
vector<int> link[200010], vcm[200010];
void dfs(int num, int par){
siz[num]=1;
vcm[col[num]].eb(num);
for(auto i:link[num]){
if(i==par)continue;
dfs(i, num);
siz[num]+=siz[i];
}
}
bool ch[200010];
int get_centroid(int num, int par, int sz){
bool flag=true;
for(auto i:link[num]){
if(ch[i])continue;
if(siz[i]>sz/2)flag=false;
}
if(flag)return num;
for(auto i:link[num]){
if(i==par||ch[i])continue;
int temp=siz[i];
siz[num]=sz-siz[i];
siz[i]=sz;
int ret=get_centroid(i, num, sz);
if(ret)return ret;
siz[num]=sz;
siz[i]=temp;
}
return 0;
}
int ans=inf, p[200010];
vector<int> vc[200010], tmp;
void dfs2(int num, int par){
p[num]=par;
tmp.eb(col[num]);
vc[col[num]].eb(num);
for(auto i:link[num]){
if(i==par||ch[i])continue;
dfs2(i, num);
}
}
int que[200010], re, fr;
bool ch2[200010];
void make_centroidtree(int num, int par){
int cen=get_centroid(num, 0, siz[num]), ret=0; re=0;
ch[cen]=true;
dfs2(cen, 0);
if(vc[col[cen]].size()!=vcm[col[cen]].size())ret=inf;
for(auto i:vc[col[cen]])que[++re]=i;
ch2[col[cen]]=true;
for(fr=1; fr<=re; fr++){
if(ch2[col[p[que[fr]]]])continue;
ch2[col[p[que[fr]]]]=true;
if(vc[col[p[que[fr]]]].size()!=vcm[col[p[que[fr]]]].size())ret=inf;
for(auto i:vc[col[p[que[fr]]]])que[++re]=i;
ret++;
}
ans=min(ans, ret);
for(auto i:tmp){
ch2[i]=false;
vc[i].clear();
}
tmp.clear();
for(auto i:link[cen]){
if(!ch[i])make_centroidtree(i, cen);
}
}
int main(){
scanf("%d %d", &n, &k);
ch2[0]=true;
for(int i=1; i<n; i++){
int a, b;
scanf("%d %d", &a, &b);
link[a].eb(b);
link[b].eb(a);
}
for(int i=1; i<=n; i++)scanf("%d", &col[i]);
dfs(1, 0);
make_centroidtree(1, 0);
printf("%d", ans);
}
Compilation message
capital_city.cpp: In function 'int main()':
capital_city.cpp:75:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &k);
~~~~~^~~~~~~~~~~~~~~~~
capital_city.cpp:79:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~~
capital_city.cpp:83:33: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1; i<=n; i++)scanf("%d", &col[i]);
~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Correct |
13 ms |
14464 KB |
Output is correct |
3 |
Correct |
13 ms |
14464 KB |
Output is correct |
4 |
Correct |
13 ms |
14464 KB |
Output is correct |
5 |
Correct |
13 ms |
14464 KB |
Output is correct |
6 |
Correct |
15 ms |
14464 KB |
Output is correct |
7 |
Correct |
15 ms |
14464 KB |
Output is correct |
8 |
Correct |
13 ms |
14464 KB |
Output is correct |
9 |
Correct |
13 ms |
14464 KB |
Output is correct |
10 |
Correct |
13 ms |
14464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Correct |
13 ms |
14464 KB |
Output is correct |
3 |
Correct |
13 ms |
14464 KB |
Output is correct |
4 |
Correct |
13 ms |
14464 KB |
Output is correct |
5 |
Correct |
13 ms |
14464 KB |
Output is correct |
6 |
Correct |
15 ms |
14464 KB |
Output is correct |
7 |
Correct |
15 ms |
14464 KB |
Output is correct |
8 |
Correct |
13 ms |
14464 KB |
Output is correct |
9 |
Correct |
13 ms |
14464 KB |
Output is correct |
10 |
Correct |
13 ms |
14464 KB |
Output is correct |
11 |
Correct |
18 ms |
14592 KB |
Output is correct |
12 |
Correct |
15 ms |
14592 KB |
Output is correct |
13 |
Correct |
15 ms |
14592 KB |
Output is correct |
14 |
Correct |
14 ms |
14592 KB |
Output is correct |
15 |
Correct |
15 ms |
14592 KB |
Output is correct |
16 |
Correct |
16 ms |
14592 KB |
Output is correct |
17 |
Correct |
14 ms |
14592 KB |
Output is correct |
18 |
Correct |
15 ms |
14720 KB |
Output is correct |
19 |
Correct |
15 ms |
14720 KB |
Output is correct |
20 |
Correct |
16 ms |
14720 KB |
Output is correct |
21 |
Correct |
16 ms |
14592 KB |
Output is correct |
22 |
Correct |
15 ms |
14720 KB |
Output is correct |
23 |
Correct |
15 ms |
14720 KB |
Output is correct |
24 |
Correct |
15 ms |
14592 KB |
Output is correct |
25 |
Correct |
15 ms |
14592 KB |
Output is correct |
26 |
Correct |
15 ms |
14720 KB |
Output is correct |
27 |
Correct |
16 ms |
14720 KB |
Output is correct |
28 |
Correct |
15 ms |
14592 KB |
Output is correct |
29 |
Correct |
15 ms |
14592 KB |
Output is correct |
30 |
Correct |
15 ms |
14592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
788 ms |
42604 KB |
Output is correct |
2 |
Correct |
241 ms |
42988 KB |
Output is correct |
3 |
Correct |
730 ms |
42468 KB |
Output is correct |
4 |
Correct |
249 ms |
42856 KB |
Output is correct |
5 |
Correct |
709 ms |
39660 KB |
Output is correct |
6 |
Correct |
248 ms |
42988 KB |
Output is correct |
7 |
Correct |
688 ms |
39788 KB |
Output is correct |
8 |
Correct |
245 ms |
45804 KB |
Output is correct |
9 |
Correct |
729 ms |
41452 KB |
Output is correct |
10 |
Correct |
752 ms |
39524 KB |
Output is correct |
11 |
Correct |
950 ms |
41708 KB |
Output is correct |
12 |
Correct |
712 ms |
43836 KB |
Output is correct |
13 |
Correct |
700 ms |
39020 KB |
Output is correct |
14 |
Correct |
725 ms |
44252 KB |
Output is correct |
15 |
Correct |
804 ms |
44008 KB |
Output is correct |
16 |
Correct |
749 ms |
39856 KB |
Output is correct |
17 |
Correct |
752 ms |
40428 KB |
Output is correct |
18 |
Correct |
698 ms |
40552 KB |
Output is correct |
19 |
Correct |
716 ms |
43244 KB |
Output is correct |
20 |
Correct |
756 ms |
45036 KB |
Output is correct |
21 |
Correct |
15 ms |
14464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Correct |
13 ms |
14464 KB |
Output is correct |
3 |
Correct |
13 ms |
14464 KB |
Output is correct |
4 |
Correct |
13 ms |
14464 KB |
Output is correct |
5 |
Correct |
13 ms |
14464 KB |
Output is correct |
6 |
Correct |
15 ms |
14464 KB |
Output is correct |
7 |
Correct |
15 ms |
14464 KB |
Output is correct |
8 |
Correct |
13 ms |
14464 KB |
Output is correct |
9 |
Correct |
13 ms |
14464 KB |
Output is correct |
10 |
Correct |
13 ms |
14464 KB |
Output is correct |
11 |
Correct |
18 ms |
14592 KB |
Output is correct |
12 |
Correct |
15 ms |
14592 KB |
Output is correct |
13 |
Correct |
15 ms |
14592 KB |
Output is correct |
14 |
Correct |
14 ms |
14592 KB |
Output is correct |
15 |
Correct |
15 ms |
14592 KB |
Output is correct |
16 |
Correct |
16 ms |
14592 KB |
Output is correct |
17 |
Correct |
14 ms |
14592 KB |
Output is correct |
18 |
Correct |
15 ms |
14720 KB |
Output is correct |
19 |
Correct |
15 ms |
14720 KB |
Output is correct |
20 |
Correct |
16 ms |
14720 KB |
Output is correct |
21 |
Correct |
16 ms |
14592 KB |
Output is correct |
22 |
Correct |
15 ms |
14720 KB |
Output is correct |
23 |
Correct |
15 ms |
14720 KB |
Output is correct |
24 |
Correct |
15 ms |
14592 KB |
Output is correct |
25 |
Correct |
15 ms |
14592 KB |
Output is correct |
26 |
Correct |
15 ms |
14720 KB |
Output is correct |
27 |
Correct |
16 ms |
14720 KB |
Output is correct |
28 |
Correct |
15 ms |
14592 KB |
Output is correct |
29 |
Correct |
15 ms |
14592 KB |
Output is correct |
30 |
Correct |
15 ms |
14592 KB |
Output is correct |
31 |
Correct |
788 ms |
42604 KB |
Output is correct |
32 |
Correct |
241 ms |
42988 KB |
Output is correct |
33 |
Correct |
730 ms |
42468 KB |
Output is correct |
34 |
Correct |
249 ms |
42856 KB |
Output is correct |
35 |
Correct |
709 ms |
39660 KB |
Output is correct |
36 |
Correct |
248 ms |
42988 KB |
Output is correct |
37 |
Correct |
688 ms |
39788 KB |
Output is correct |
38 |
Correct |
245 ms |
45804 KB |
Output is correct |
39 |
Correct |
729 ms |
41452 KB |
Output is correct |
40 |
Correct |
752 ms |
39524 KB |
Output is correct |
41 |
Correct |
950 ms |
41708 KB |
Output is correct |
42 |
Correct |
712 ms |
43836 KB |
Output is correct |
43 |
Correct |
700 ms |
39020 KB |
Output is correct |
44 |
Correct |
725 ms |
44252 KB |
Output is correct |
45 |
Correct |
804 ms |
44008 KB |
Output is correct |
46 |
Correct |
749 ms |
39856 KB |
Output is correct |
47 |
Correct |
752 ms |
40428 KB |
Output is correct |
48 |
Correct |
698 ms |
40552 KB |
Output is correct |
49 |
Correct |
716 ms |
43244 KB |
Output is correct |
50 |
Correct |
756 ms |
45036 KB |
Output is correct |
51 |
Correct |
15 ms |
14464 KB |
Output is correct |
52 |
Correct |
524 ms |
31604 KB |
Output is correct |
53 |
Correct |
549 ms |
31724 KB |
Output is correct |
54 |
Correct |
518 ms |
31468 KB |
Output is correct |
55 |
Correct |
643 ms |
31696 KB |
Output is correct |
56 |
Correct |
552 ms |
31596 KB |
Output is correct |
57 |
Correct |
553 ms |
31684 KB |
Output is correct |
58 |
Correct |
574 ms |
35436 KB |
Output is correct |
59 |
Correct |
522 ms |
35304 KB |
Output is correct |
60 |
Correct |
667 ms |
35052 KB |
Output is correct |
61 |
Correct |
701 ms |
35012 KB |
Output is correct |
62 |
Correct |
255 ms |
46548 KB |
Output is correct |
63 |
Correct |
245 ms |
46572 KB |
Output is correct |
64 |
Correct |
260 ms |
46192 KB |
Output is correct |
65 |
Correct |
251 ms |
46444 KB |
Output is correct |
66 |
Correct |
400 ms |
39148 KB |
Output is correct |
67 |
Correct |
382 ms |
39036 KB |
Output is correct |
68 |
Correct |
425 ms |
39148 KB |
Output is correct |
69 |
Correct |
377 ms |
39116 KB |
Output is correct |
70 |
Correct |
521 ms |
39020 KB |
Output is correct |
71 |
Correct |
378 ms |
39020 KB |
Output is correct |
72 |
Correct |
463 ms |
39020 KB |
Output is correct |
73 |
Correct |
381 ms |
38448 KB |
Output is correct |
74 |
Correct |
445 ms |
39356 KB |
Output is correct |
75 |
Correct |
399 ms |
39148 KB |
Output is correct |
76 |
Correct |
588 ms |
39020 KB |
Output is correct |
77 |
Correct |
689 ms |
37276 KB |
Output is correct |
78 |
Correct |
777 ms |
40296 KB |
Output is correct |
79 |
Correct |
713 ms |
38804 KB |
Output is correct |
80 |
Correct |
712 ms |
44268 KB |
Output is correct |
81 |
Correct |
694 ms |
41452 KB |
Output is correct |
82 |
Correct |
745 ms |
41644 KB |
Output is correct |
83 |
Correct |
716 ms |
39020 KB |
Output is correct |
84 |
Correct |
824 ms |
43752 KB |
Output is correct |
85 |
Correct |
728 ms |
42528 KB |
Output is correct |
86 |
Correct |
909 ms |
38632 KB |
Output is correct |
87 |
Correct |
717 ms |
40048 KB |
Output is correct |
88 |
Correct |
625 ms |
40940 KB |
Output is correct |
89 |
Correct |
722 ms |
37744 KB |
Output is correct |
90 |
Correct |
611 ms |
37632 KB |
Output is correct |
91 |
Correct |
612 ms |
39436 KB |
Output is correct |
92 |
Correct |
611 ms |
38504 KB |
Output is correct |
93 |
Correct |
680 ms |
38124 KB |
Output is correct |
94 |
Correct |
613 ms |
37488 KB |
Output is correct |
95 |
Correct |
756 ms |
38888 KB |
Output is correct |
96 |
Correct |
615 ms |
37864 KB |
Output is correct |
97 |
Correct |
597 ms |
39276 KB |
Output is correct |