#include <bits/stdc++.h>
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
const int N = 2e5 + 10;
int par[N], cc[N], sz[N], c[N], cnt[N], ans = 1e9, n, k;
bool chk[N];
vector<int> col[N], g[N];
queue<int> q;
int getsz( int u, int p ) { sz[u] = 1; for( int v : g[u] ) if( v != p && !chk[v] ) sz[u] += getsz( v, u ); return sz[u]; }
int findcen(int u, int p, int all, int &ret) {
int mx = all - sz[u];
for(int v : g[u]) if(v != p && !chk[v])
mx = max(mx, findcen(v, u, all, ret));
if(mx <= (all + 1) / 2) ret = u;
return sz[u];
}
void dfs( int u, int p, bool fill ) {
if( fill ) col[c[u]].emplace_back( u ), par[u] = p;
else col[c[u]].clear(), par[u] = -1, cc[c[u]] = false;
for( int v : g[u] ) if( v != p && !chk[v] ) dfs( v, u, fill );
}
void centroid( int u ) {
//printf("%d ",u);
// pii ret( 1e9, -1 );
getsz( u, u );
findcen( u, u, sz[u], u );
dfs( u, u, true );
bool check = true;
//u = ret.y;
//printf("CEN : %d\n",u);
int cou = 1;
q.emplace( c[u] ), cc[c[u]] = true;
while( !q.empty() ) {
int now = q.front(); q.pop();
if( col[now].size() != cnt[now] ) {
check = false;
break ;
}
for( int x : col[now] ) if( x != u && !cc[c[par[x]]] ) {
cc[c[par[x]]] = true;
cou++;
q.emplace( c[par[x]] );
}
}
while( !q.empty() ) q.pop();
if( check ) ans = min( ans, cou );
dfs( u, u, false ), chk[u] = true;
for( int v : g[u] ) if( !chk[v] ) centroid( v );
}
int main()
{
scanf("%d %d",&n,&k);
for( int i = 1, a, b ; i < n ; i++ ) {
scanf("%d %d",&a,&b);
g[a].emplace_back( b ), g[b].emplace_back( a );
}
for( int i = 1 ; i <= n ; i++ ) {
scanf("%d",&c[i]);
cnt[c[i]]++;
}
centroid( 1 );
printf("%d\n",ans-1);
return 0;
}
Compilation message
capital_city.cpp: In function 'void centroid(int)':
capital_city.cpp:42:29: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if( col[now].size() != cnt[now] ) {
~~~~~~~~~~~~~~~~^~~~~~~~~~~
capital_city.cpp: In function 'int main()':
capital_city.cpp:61: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:63: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:67:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&c[i]);
~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
9728 KB |
Output is correct |
2 |
Correct |
9 ms |
9728 KB |
Output is correct |
3 |
Correct |
9 ms |
9728 KB |
Output is correct |
4 |
Correct |
10 ms |
9728 KB |
Output is correct |
5 |
Correct |
10 ms |
9728 KB |
Output is correct |
6 |
Correct |
10 ms |
9728 KB |
Output is correct |
7 |
Correct |
10 ms |
9728 KB |
Output is correct |
8 |
Correct |
9 ms |
9728 KB |
Output is correct |
9 |
Correct |
11 ms |
9728 KB |
Output is correct |
10 |
Correct |
9 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
9728 KB |
Output is correct |
2 |
Correct |
9 ms |
9728 KB |
Output is correct |
3 |
Correct |
9 ms |
9728 KB |
Output is correct |
4 |
Correct |
10 ms |
9728 KB |
Output is correct |
5 |
Correct |
10 ms |
9728 KB |
Output is correct |
6 |
Correct |
10 ms |
9728 KB |
Output is correct |
7 |
Correct |
10 ms |
9728 KB |
Output is correct |
8 |
Correct |
9 ms |
9728 KB |
Output is correct |
9 |
Correct |
11 ms |
9728 KB |
Output is correct |
10 |
Correct |
9 ms |
9728 KB |
Output is correct |
11 |
Correct |
12 ms |
9856 KB |
Output is correct |
12 |
Correct |
12 ms |
9856 KB |
Output is correct |
13 |
Correct |
12 ms |
9856 KB |
Output is correct |
14 |
Correct |
12 ms |
9856 KB |
Output is correct |
15 |
Correct |
13 ms |
9856 KB |
Output is correct |
16 |
Correct |
13 ms |
9856 KB |
Output is correct |
17 |
Correct |
11 ms |
9984 KB |
Output is correct |
18 |
Correct |
11 ms |
9984 KB |
Output is correct |
19 |
Correct |
11 ms |
9984 KB |
Output is correct |
20 |
Correct |
11 ms |
9984 KB |
Output is correct |
21 |
Correct |
11 ms |
9984 KB |
Output is correct |
22 |
Correct |
11 ms |
9984 KB |
Output is correct |
23 |
Correct |
12 ms |
9984 KB |
Output is correct |
24 |
Correct |
12 ms |
9856 KB |
Output is correct |
25 |
Correct |
12 ms |
9984 KB |
Output is correct |
26 |
Correct |
12 ms |
9984 KB |
Output is correct |
27 |
Correct |
13 ms |
9984 KB |
Output is correct |
28 |
Correct |
12 ms |
9984 KB |
Output is correct |
29 |
Correct |
12 ms |
9856 KB |
Output is correct |
30 |
Correct |
13 ms |
9984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
942 ms |
37712 KB |
Output is correct |
2 |
Correct |
291 ms |
38136 KB |
Output is correct |
3 |
Correct |
969 ms |
37380 KB |
Output is correct |
4 |
Correct |
295 ms |
38136 KB |
Output is correct |
5 |
Correct |
923 ms |
34296 KB |
Output is correct |
6 |
Correct |
295 ms |
38136 KB |
Output is correct |
7 |
Correct |
940 ms |
34552 KB |
Output is correct |
8 |
Correct |
289 ms |
37624 KB |
Output is correct |
9 |
Correct |
1012 ms |
32508 KB |
Output is correct |
10 |
Correct |
1008 ms |
30104 KB |
Output is correct |
11 |
Correct |
1000 ms |
32888 KB |
Output is correct |
12 |
Correct |
995 ms |
35576 KB |
Output is correct |
13 |
Correct |
1008 ms |
29688 KB |
Output is correct |
14 |
Correct |
993 ms |
35920 KB |
Output is correct |
15 |
Correct |
1052 ms |
35640 KB |
Output is correct |
16 |
Correct |
1167 ms |
30576 KB |
Output is correct |
17 |
Correct |
1095 ms |
31224 KB |
Output is correct |
18 |
Correct |
1094 ms |
31312 KB |
Output is correct |
19 |
Correct |
1008 ms |
34472 KB |
Output is correct |
20 |
Correct |
990 ms |
36856 KB |
Output is correct |
21 |
Correct |
9 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
9728 KB |
Output is correct |
2 |
Correct |
9 ms |
9728 KB |
Output is correct |
3 |
Correct |
9 ms |
9728 KB |
Output is correct |
4 |
Correct |
10 ms |
9728 KB |
Output is correct |
5 |
Correct |
10 ms |
9728 KB |
Output is correct |
6 |
Correct |
10 ms |
9728 KB |
Output is correct |
7 |
Correct |
10 ms |
9728 KB |
Output is correct |
8 |
Correct |
9 ms |
9728 KB |
Output is correct |
9 |
Correct |
11 ms |
9728 KB |
Output is correct |
10 |
Correct |
9 ms |
9728 KB |
Output is correct |
11 |
Correct |
12 ms |
9856 KB |
Output is correct |
12 |
Correct |
12 ms |
9856 KB |
Output is correct |
13 |
Correct |
12 ms |
9856 KB |
Output is correct |
14 |
Correct |
12 ms |
9856 KB |
Output is correct |
15 |
Correct |
13 ms |
9856 KB |
Output is correct |
16 |
Correct |
13 ms |
9856 KB |
Output is correct |
17 |
Correct |
11 ms |
9984 KB |
Output is correct |
18 |
Correct |
11 ms |
9984 KB |
Output is correct |
19 |
Correct |
11 ms |
9984 KB |
Output is correct |
20 |
Correct |
11 ms |
9984 KB |
Output is correct |
21 |
Correct |
11 ms |
9984 KB |
Output is correct |
22 |
Correct |
11 ms |
9984 KB |
Output is correct |
23 |
Correct |
12 ms |
9984 KB |
Output is correct |
24 |
Correct |
12 ms |
9856 KB |
Output is correct |
25 |
Correct |
12 ms |
9984 KB |
Output is correct |
26 |
Correct |
12 ms |
9984 KB |
Output is correct |
27 |
Correct |
13 ms |
9984 KB |
Output is correct |
28 |
Correct |
12 ms |
9984 KB |
Output is correct |
29 |
Correct |
12 ms |
9856 KB |
Output is correct |
30 |
Correct |
13 ms |
9984 KB |
Output is correct |
31 |
Correct |
942 ms |
37712 KB |
Output is correct |
32 |
Correct |
291 ms |
38136 KB |
Output is correct |
33 |
Correct |
969 ms |
37380 KB |
Output is correct |
34 |
Correct |
295 ms |
38136 KB |
Output is correct |
35 |
Correct |
923 ms |
34296 KB |
Output is correct |
36 |
Correct |
295 ms |
38136 KB |
Output is correct |
37 |
Correct |
940 ms |
34552 KB |
Output is correct |
38 |
Correct |
289 ms |
37624 KB |
Output is correct |
39 |
Correct |
1012 ms |
32508 KB |
Output is correct |
40 |
Correct |
1008 ms |
30104 KB |
Output is correct |
41 |
Correct |
1000 ms |
32888 KB |
Output is correct |
42 |
Correct |
995 ms |
35576 KB |
Output is correct |
43 |
Correct |
1008 ms |
29688 KB |
Output is correct |
44 |
Correct |
993 ms |
35920 KB |
Output is correct |
45 |
Correct |
1052 ms |
35640 KB |
Output is correct |
46 |
Correct |
1167 ms |
30576 KB |
Output is correct |
47 |
Correct |
1095 ms |
31224 KB |
Output is correct |
48 |
Correct |
1094 ms |
31312 KB |
Output is correct |
49 |
Correct |
1008 ms |
34472 KB |
Output is correct |
50 |
Correct |
990 ms |
36856 KB |
Output is correct |
51 |
Correct |
9 ms |
9728 KB |
Output is correct |
52 |
Correct |
672 ms |
20600 KB |
Output is correct |
53 |
Correct |
755 ms |
20600 KB |
Output is correct |
54 |
Correct |
719 ms |
20600 KB |
Output is correct |
55 |
Correct |
747 ms |
20472 KB |
Output is correct |
56 |
Correct |
783 ms |
20728 KB |
Output is correct |
57 |
Correct |
789 ms |
20472 KB |
Output is correct |
58 |
Correct |
734 ms |
24440 KB |
Output is correct |
59 |
Correct |
620 ms |
24824 KB |
Output is correct |
60 |
Correct |
870 ms |
24184 KB |
Output is correct |
61 |
Correct |
949 ms |
23784 KB |
Output is correct |
62 |
Correct |
289 ms |
38136 KB |
Output is correct |
63 |
Correct |
290 ms |
38136 KB |
Output is correct |
64 |
Correct |
292 ms |
37752 KB |
Output is correct |
65 |
Correct |
300 ms |
38264 KB |
Output is correct |
66 |
Correct |
459 ms |
29552 KB |
Output is correct |
67 |
Correct |
496 ms |
29556 KB |
Output is correct |
68 |
Correct |
464 ms |
29424 KB |
Output is correct |
69 |
Correct |
467 ms |
29424 KB |
Output is correct |
70 |
Correct |
484 ms |
29680 KB |
Output is correct |
71 |
Correct |
466 ms |
29552 KB |
Output is correct |
72 |
Correct |
488 ms |
29684 KB |
Output is correct |
73 |
Correct |
459 ms |
28528 KB |
Output is correct |
74 |
Correct |
478 ms |
29552 KB |
Output is correct |
75 |
Correct |
491 ms |
29420 KB |
Output is correct |
76 |
Correct |
773 ms |
28192 KB |
Output is correct |
77 |
Correct |
713 ms |
26232 KB |
Output is correct |
78 |
Correct |
1030 ms |
31096 KB |
Output is correct |
79 |
Correct |
1028 ms |
29176 KB |
Output is correct |
80 |
Correct |
1008 ms |
36344 KB |
Output is correct |
81 |
Correct |
997 ms |
32760 KB |
Output is correct |
82 |
Correct |
997 ms |
32764 KB |
Output is correct |
83 |
Correct |
986 ms |
29560 KB |
Output is correct |
84 |
Correct |
1004 ms |
35448 KB |
Output is correct |
85 |
Correct |
1012 ms |
33888 KB |
Output is correct |
86 |
Correct |
983 ms |
29048 KB |
Output is correct |
87 |
Correct |
999 ms |
30940 KB |
Output is correct |
88 |
Correct |
864 ms |
31944 KB |
Output is correct |
89 |
Correct |
846 ms |
27896 KB |
Output is correct |
90 |
Correct |
862 ms |
27848 KB |
Output is correct |
91 |
Correct |
850 ms |
30072 KB |
Output is correct |
92 |
Correct |
862 ms |
28792 KB |
Output is correct |
93 |
Correct |
898 ms |
28408 KB |
Output is correct |
94 |
Correct |
852 ms |
27896 KB |
Output is correct |
95 |
Correct |
867 ms |
29560 KB |
Output is correct |
96 |
Correct |
866 ms |
28152 KB |
Output is correct |
97 |
Correct |
855 ms |
29944 KB |
Output is correct |