#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(), 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, 0, 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, 0, 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 |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
9 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9728 KB |
Output is correct |
4 |
Correct |
9 ms |
9728 KB |
Output is correct |
5 |
Correct |
9 ms |
9728 KB |
Output is correct |
6 |
Correct |
11 ms |
9728 KB |
Output is correct |
7 |
Correct |
11 ms |
9728 KB |
Output is correct |
8 |
Correct |
10 ms |
9728 KB |
Output is correct |
9 |
Correct |
9 ms |
9728 KB |
Output is correct |
10 |
Correct |
9 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
9 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9728 KB |
Output is correct |
4 |
Correct |
9 ms |
9728 KB |
Output is correct |
5 |
Correct |
9 ms |
9728 KB |
Output is correct |
6 |
Correct |
11 ms |
9728 KB |
Output is correct |
7 |
Correct |
11 ms |
9728 KB |
Output is correct |
8 |
Correct |
10 ms |
9728 KB |
Output is correct |
9 |
Correct |
9 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 |
13 ms |
9856 KB |
Output is correct |
14 |
Correct |
12 ms |
9856 KB |
Output is correct |
15 |
Correct |
12 ms |
9856 KB |
Output is correct |
16 |
Correct |
12 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 |
12 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 |
9936 KB |
Output is correct |
25 |
Correct |
13 ms |
9984 KB |
Output is correct |
26 |
Correct |
14 ms |
9984 KB |
Output is correct |
27 |
Correct |
12 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 |
939 ms |
37624 KB |
Output is correct |
2 |
Correct |
298 ms |
38136 KB |
Output is correct |
3 |
Correct |
930 ms |
37356 KB |
Output is correct |
4 |
Correct |
323 ms |
38136 KB |
Output is correct |
5 |
Correct |
922 ms |
34296 KB |
Output is correct |
6 |
Correct |
299 ms |
38136 KB |
Output is correct |
7 |
Correct |
877 ms |
34552 KB |
Output is correct |
8 |
Correct |
303 ms |
37752 KB |
Output is correct |
9 |
Correct |
970 ms |
32508 KB |
Output is correct |
10 |
Correct |
970 ms |
30072 KB |
Output is correct |
11 |
Correct |
999 ms |
33016 KB |
Output is correct |
12 |
Correct |
995 ms |
35704 KB |
Output is correct |
13 |
Correct |
979 ms |
29688 KB |
Output is correct |
14 |
Correct |
1010 ms |
36088 KB |
Output is correct |
15 |
Correct |
993 ms |
35704 KB |
Output is correct |
16 |
Correct |
1092 ms |
30584 KB |
Output is correct |
17 |
Correct |
1148 ms |
31204 KB |
Output is correct |
18 |
Correct |
1072 ms |
31320 KB |
Output is correct |
19 |
Correct |
1010 ms |
34424 KB |
Output is correct |
20 |
Correct |
996 ms |
36668 KB |
Output is correct |
21 |
Correct |
9 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
9 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9728 KB |
Output is correct |
4 |
Correct |
9 ms |
9728 KB |
Output is correct |
5 |
Correct |
9 ms |
9728 KB |
Output is correct |
6 |
Correct |
11 ms |
9728 KB |
Output is correct |
7 |
Correct |
11 ms |
9728 KB |
Output is correct |
8 |
Correct |
10 ms |
9728 KB |
Output is correct |
9 |
Correct |
9 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 |
13 ms |
9856 KB |
Output is correct |
14 |
Correct |
12 ms |
9856 KB |
Output is correct |
15 |
Correct |
12 ms |
9856 KB |
Output is correct |
16 |
Correct |
12 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 |
12 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 |
9936 KB |
Output is correct |
25 |
Correct |
13 ms |
9984 KB |
Output is correct |
26 |
Correct |
14 ms |
9984 KB |
Output is correct |
27 |
Correct |
12 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 |
939 ms |
37624 KB |
Output is correct |
32 |
Correct |
298 ms |
38136 KB |
Output is correct |
33 |
Correct |
930 ms |
37356 KB |
Output is correct |
34 |
Correct |
323 ms |
38136 KB |
Output is correct |
35 |
Correct |
922 ms |
34296 KB |
Output is correct |
36 |
Correct |
299 ms |
38136 KB |
Output is correct |
37 |
Correct |
877 ms |
34552 KB |
Output is correct |
38 |
Correct |
303 ms |
37752 KB |
Output is correct |
39 |
Correct |
970 ms |
32508 KB |
Output is correct |
40 |
Correct |
970 ms |
30072 KB |
Output is correct |
41 |
Correct |
999 ms |
33016 KB |
Output is correct |
42 |
Correct |
995 ms |
35704 KB |
Output is correct |
43 |
Correct |
979 ms |
29688 KB |
Output is correct |
44 |
Correct |
1010 ms |
36088 KB |
Output is correct |
45 |
Correct |
993 ms |
35704 KB |
Output is correct |
46 |
Correct |
1092 ms |
30584 KB |
Output is correct |
47 |
Correct |
1148 ms |
31204 KB |
Output is correct |
48 |
Correct |
1072 ms |
31320 KB |
Output is correct |
49 |
Correct |
1010 ms |
34424 KB |
Output is correct |
50 |
Correct |
996 ms |
36668 KB |
Output is correct |
51 |
Correct |
9 ms |
9728 KB |
Output is correct |
52 |
Correct |
682 ms |
20492 KB |
Output is correct |
53 |
Correct |
756 ms |
20568 KB |
Output is correct |
54 |
Correct |
772 ms |
20556 KB |
Output is correct |
55 |
Correct |
810 ms |
20600 KB |
Output is correct |
56 |
Correct |
742 ms |
20604 KB |
Output is correct |
57 |
Correct |
710 ms |
20472 KB |
Output is correct |
58 |
Correct |
638 ms |
24440 KB |
Output is correct |
59 |
Correct |
630 ms |
24952 KB |
Output is correct |
60 |
Correct |
865 ms |
24184 KB |
Output is correct |
61 |
Correct |
951 ms |
23800 KB |
Output is correct |
62 |
Correct |
302 ms |
38140 KB |
Output is correct |
63 |
Correct |
308 ms |
38136 KB |
Output is correct |
64 |
Correct |
299 ms |
37752 KB |
Output is correct |
65 |
Correct |
309 ms |
38136 KB |
Output is correct |
66 |
Correct |
461 ms |
29552 KB |
Output is correct |
67 |
Correct |
476 ms |
29496 KB |
Output is correct |
68 |
Correct |
467 ms |
29420 KB |
Output is correct |
69 |
Correct |
484 ms |
29552 KB |
Output is correct |
70 |
Correct |
466 ms |
29552 KB |
Output is correct |
71 |
Correct |
461 ms |
29420 KB |
Output is correct |
72 |
Correct |
477 ms |
29424 KB |
Output is correct |
73 |
Correct |
472 ms |
28528 KB |
Output is correct |
74 |
Correct |
484 ms |
29680 KB |
Output is correct |
75 |
Correct |
488 ms |
29424 KB |
Output is correct |
76 |
Correct |
743 ms |
28080 KB |
Output is correct |
77 |
Correct |
735 ms |
26276 KB |
Output is correct |
78 |
Correct |
1022 ms |
31224 KB |
Output is correct |
79 |
Correct |
988 ms |
29048 KB |
Output is correct |
80 |
Correct |
981 ms |
36344 KB |
Output is correct |
81 |
Correct |
964 ms |
32632 KB |
Output is correct |
82 |
Correct |
1000 ms |
32868 KB |
Output is correct |
83 |
Correct |
971 ms |
29564 KB |
Output is correct |
84 |
Correct |
1000 ms |
35344 KB |
Output is correct |
85 |
Correct |
975 ms |
33912 KB |
Output is correct |
86 |
Correct |
978 ms |
29048 KB |
Output is correct |
87 |
Correct |
965 ms |
30968 KB |
Output is correct |
88 |
Correct |
845 ms |
32120 KB |
Output is correct |
89 |
Correct |
830 ms |
27896 KB |
Output is correct |
90 |
Correct |
847 ms |
27896 KB |
Output is correct |
91 |
Correct |
853 ms |
30328 KB |
Output is correct |
92 |
Correct |
831 ms |
28792 KB |
Output is correct |
93 |
Correct |
827 ms |
28536 KB |
Output is correct |
94 |
Correct |
826 ms |
27896 KB |
Output is correct |
95 |
Correct |
838 ms |
29432 KB |
Output is correct |
96 |
Correct |
840 ms |
28152 KB |
Output is correct |
97 |
Correct |
857 ms |
30072 KB |
Output is correct |