#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 now, int p, int all, pii &ret ) {
int mx = all - sz[now];
for( int v : g[now] ) if( v != p && !chk[v] ) mx = max( mx, findcen( v, now, all, ret ) );
ret = min( ret, pii( mx, now ) );
return sz[now];
}
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 ) {
pii ret( 1e9, -1 );
getsz( u, u );
findcen( u, u, sz[u], ret );
u = ret.y;
dfs( u, u, true );
bool check = true;
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:40: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:59: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:61: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:65: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 |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9728 KB |
Output is correct |
4 |
Correct |
11 ms |
9856 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 |
10 ms |
9728 KB |
Output is correct |
9 |
Correct |
10 ms |
9728 KB |
Output is correct |
10 |
Correct |
10 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9728 KB |
Output is correct |
4 |
Correct |
11 ms |
9856 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 |
10 ms |
9728 KB |
Output is correct |
9 |
Correct |
10 ms |
9728 KB |
Output is correct |
10 |
Correct |
10 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 |
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 |
12 ms |
9984 KB |
Output is correct |
20 |
Correct |
12 ms |
9984 KB |
Output is correct |
21 |
Correct |
12 ms |
9984 KB |
Output is correct |
22 |
Correct |
12 ms |
10112 KB |
Output is correct |
23 |
Correct |
13 ms |
9984 KB |
Output is correct |
24 |
Correct |
14 ms |
9984 KB |
Output is correct |
25 |
Correct |
13 ms |
9984 KB |
Output is correct |
26 |
Correct |
13 ms |
9984 KB |
Output is correct |
27 |
Correct |
12 ms |
9984 KB |
Output is correct |
28 |
Correct |
13 ms |
9984 KB |
Output is correct |
29 |
Correct |
13 ms |
9984 KB |
Output is correct |
30 |
Correct |
13 ms |
9984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
972 ms |
37752 KB |
Output is correct |
2 |
Correct |
295 ms |
38136 KB |
Output is correct |
3 |
Correct |
975 ms |
37496 KB |
Output is correct |
4 |
Correct |
303 ms |
38136 KB |
Output is correct |
5 |
Correct |
958 ms |
34296 KB |
Output is correct |
6 |
Correct |
306 ms |
38340 KB |
Output is correct |
7 |
Correct |
918 ms |
34556 KB |
Output is correct |
8 |
Correct |
299 ms |
37496 KB |
Output is correct |
9 |
Correct |
1018 ms |
32604 KB |
Output is correct |
10 |
Correct |
1008 ms |
30080 KB |
Output is correct |
11 |
Correct |
1064 ms |
32868 KB |
Output is correct |
12 |
Correct |
1008 ms |
35576 KB |
Output is correct |
13 |
Correct |
1006 ms |
29560 KB |
Output is correct |
14 |
Correct |
1010 ms |
35960 KB |
Output is correct |
15 |
Correct |
1018 ms |
35704 KB |
Output is correct |
16 |
Correct |
1012 ms |
30584 KB |
Output is correct |
17 |
Correct |
1008 ms |
31224 KB |
Output is correct |
18 |
Correct |
1004 ms |
31472 KB |
Output is correct |
19 |
Correct |
1006 ms |
34392 KB |
Output is correct |
20 |
Correct |
996 ms |
36812 KB |
Output is correct |
21 |
Correct |
10 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9728 KB |
Output is correct |
4 |
Correct |
11 ms |
9856 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 |
10 ms |
9728 KB |
Output is correct |
9 |
Correct |
10 ms |
9728 KB |
Output is correct |
10 |
Correct |
10 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 |
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 |
12 ms |
9984 KB |
Output is correct |
20 |
Correct |
12 ms |
9984 KB |
Output is correct |
21 |
Correct |
12 ms |
9984 KB |
Output is correct |
22 |
Correct |
12 ms |
10112 KB |
Output is correct |
23 |
Correct |
13 ms |
9984 KB |
Output is correct |
24 |
Correct |
14 ms |
9984 KB |
Output is correct |
25 |
Correct |
13 ms |
9984 KB |
Output is correct |
26 |
Correct |
13 ms |
9984 KB |
Output is correct |
27 |
Correct |
12 ms |
9984 KB |
Output is correct |
28 |
Correct |
13 ms |
9984 KB |
Output is correct |
29 |
Correct |
13 ms |
9984 KB |
Output is correct |
30 |
Correct |
13 ms |
9984 KB |
Output is correct |
31 |
Correct |
972 ms |
37752 KB |
Output is correct |
32 |
Correct |
295 ms |
38136 KB |
Output is correct |
33 |
Correct |
975 ms |
37496 KB |
Output is correct |
34 |
Correct |
303 ms |
38136 KB |
Output is correct |
35 |
Correct |
958 ms |
34296 KB |
Output is correct |
36 |
Correct |
306 ms |
38340 KB |
Output is correct |
37 |
Correct |
918 ms |
34556 KB |
Output is correct |
38 |
Correct |
299 ms |
37496 KB |
Output is correct |
39 |
Correct |
1018 ms |
32604 KB |
Output is correct |
40 |
Correct |
1008 ms |
30080 KB |
Output is correct |
41 |
Correct |
1064 ms |
32868 KB |
Output is correct |
42 |
Correct |
1008 ms |
35576 KB |
Output is correct |
43 |
Correct |
1006 ms |
29560 KB |
Output is correct |
44 |
Correct |
1010 ms |
35960 KB |
Output is correct |
45 |
Correct |
1018 ms |
35704 KB |
Output is correct |
46 |
Correct |
1012 ms |
30584 KB |
Output is correct |
47 |
Correct |
1008 ms |
31224 KB |
Output is correct |
48 |
Correct |
1004 ms |
31472 KB |
Output is correct |
49 |
Correct |
1006 ms |
34392 KB |
Output is correct |
50 |
Correct |
996 ms |
36812 KB |
Output is correct |
51 |
Correct |
10 ms |
9728 KB |
Output is correct |
52 |
Correct |
687 ms |
20564 KB |
Output is correct |
53 |
Correct |
730 ms |
20600 KB |
Output is correct |
54 |
Correct |
735 ms |
20672 KB |
Output is correct |
55 |
Correct |
763 ms |
20600 KB |
Output is correct |
56 |
Correct |
752 ms |
20600 KB |
Output is correct |
57 |
Correct |
735 ms |
20568 KB |
Output is correct |
58 |
Correct |
632 ms |
24440 KB |
Output is correct |
59 |
Correct |
657 ms |
24696 KB |
Output is correct |
60 |
Correct |
940 ms |
24056 KB |
Output is correct |
61 |
Correct |
983 ms |
23824 KB |
Output is correct |
62 |
Correct |
302 ms |
38136 KB |
Output is correct |
63 |
Correct |
296 ms |
38140 KB |
Output is correct |
64 |
Correct |
302 ms |
37752 KB |
Output is correct |
65 |
Correct |
297 ms |
38008 KB |
Output is correct |
66 |
Correct |
502 ms |
29424 KB |
Output is correct |
67 |
Correct |
482 ms |
29424 KB |
Output is correct |
68 |
Correct |
493 ms |
29552 KB |
Output is correct |
69 |
Correct |
484 ms |
29424 KB |
Output is correct |
70 |
Correct |
517 ms |
29424 KB |
Output is correct |
71 |
Correct |
490 ms |
29552 KB |
Output is correct |
72 |
Correct |
476 ms |
29424 KB |
Output is correct |
73 |
Correct |
493 ms |
29044 KB |
Output is correct |
74 |
Correct |
498 ms |
29424 KB |
Output is correct |
75 |
Correct |
482 ms |
29424 KB |
Output is correct |
76 |
Correct |
755 ms |
28152 KB |
Output is correct |
77 |
Correct |
754 ms |
26328 KB |
Output is correct |
78 |
Correct |
1018 ms |
31224 KB |
Output is correct |
79 |
Correct |
1008 ms |
29176 KB |
Output is correct |
80 |
Correct |
1004 ms |
36348 KB |
Output is correct |
81 |
Correct |
1010 ms |
32600 KB |
Output is correct |
82 |
Correct |
1018 ms |
32924 KB |
Output is correct |
83 |
Correct |
1020 ms |
29560 KB |
Output is correct |
84 |
Correct |
1020 ms |
35448 KB |
Output is correct |
85 |
Correct |
1038 ms |
33820 KB |
Output is correct |
86 |
Correct |
1010 ms |
29048 KB |
Output is correct |
87 |
Correct |
1002 ms |
31096 KB |
Output is correct |
88 |
Correct |
879 ms |
32116 KB |
Output is correct |
89 |
Correct |
865 ms |
28152 KB |
Output is correct |
90 |
Correct |
860 ms |
27768 KB |
Output is correct |
91 |
Correct |
871 ms |
30328 KB |
Output is correct |
92 |
Correct |
884 ms |
28920 KB |
Output is correct |
93 |
Correct |
875 ms |
28536 KB |
Output is correct |
94 |
Correct |
867 ms |
28024 KB |
Output is correct |
95 |
Correct |
898 ms |
29432 KB |
Output is correct |
96 |
Correct |
870 ms |
28152 KB |
Output is correct |
97 |
Correct |
902 ms |
30200 KB |
Output is correct |