#include <bits/stdc++.h>
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
const int N = 2e5+5;
int n, k, ans = 1e9;
int C[N], cnt[N];
vector<int> g[N];
int sz[N];
bitset<N> chk;
int get_sz(int u, int p) {
sz[u] = 1;
for(int v : g[u]) if(v != p && !chk[v])
sz[u] += get_sz(v, u);
return sz[u];
}
int find_cen(int u, int p, int all, pii &ret ) {
int mx = all - sz[u];
for(int v : g[u]) if(v != p && !chk[v])
mx = max(mx, find_cen(v, u, all, ret));
ret = min( ret, pii( mx, u ) );
return sz[u];
}
int par[N], used[N];
vector<int> col[N];
queue<int> Q;
void dfs(int u, int p, bool fill) {
if(fill) col[C[u]].emplace_back(u), par[u] = p;
else col[C[u]].clear(), used[C[u]] = 0;
for(int v : g[u]) if(v != p && !chk[v])
dfs(v, u, fill);
}
void process(int u) {
pii cou( 1e9, -1 );
get_sz(u, u), find_cen(u, u, sz[u], cou);
u = cou.y;
dfs(u, 0, 1);
int ret = 1;
bool valid = true;
Q.emplace(C[u]), used[C[u]] = 1;
while(!Q.empty()) {
int now = Q.front(); Q.pop();
if(col[now].size() != cnt[now]) { valid = false; break; }
for(int x : col[now]) if(x != u && !used[C[par[x]]]) {
used[C[par[x]]] = 1, ++ret;
Q.emplace(C[par[x]]);
}
}
if(valid) ans = min(ans, ret);
dfs(u, 0, 0), chk[u] = 1;
for(int v : g[u]) if(!chk[v]) process(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]];
process(1);
printf("%d\n", ans - 1);
return 0;
}
Compilation message
capital_city.cpp: In function 'void process(int)':
capital_city.cpp:54:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(col[now].size() != cnt[now]) { valid = false; break; }
~~~~~~~~~~~~~~~~^~~~~~~~~~~
capital_city.cpp: In function 'int main()':
capital_city.cpp:67: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:69: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:72:51: 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", C + i), ++cnt[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 |
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 |
11 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 |
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 |
11 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 |
20 ms |
9984 KB |
Output is correct |
18 |
Correct |
13 ms |
9984 KB |
Output is correct |
19 |
Correct |
12 ms |
9984 KB |
Output is correct |
20 |
Correct |
13 ms |
9984 KB |
Output is correct |
21 |
Correct |
11 ms |
9984 KB |
Output is correct |
22 |
Correct |
12 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 |
13 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 |
13 ms |
9856 KB |
Output is correct |
30 |
Correct |
13 ms |
9984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
954 ms |
37496 KB |
Output is correct |
2 |
Correct |
297 ms |
37960 KB |
Output is correct |
3 |
Correct |
943 ms |
37368 KB |
Output is correct |
4 |
Correct |
297 ms |
38012 KB |
Output is correct |
5 |
Correct |
931 ms |
34168 KB |
Output is correct |
6 |
Correct |
297 ms |
37880 KB |
Output is correct |
7 |
Correct |
936 ms |
34560 KB |
Output is correct |
8 |
Correct |
294 ms |
37496 KB |
Output is correct |
9 |
Correct |
980 ms |
32504 KB |
Output is correct |
10 |
Correct |
967 ms |
30072 KB |
Output is correct |
11 |
Correct |
980 ms |
32888 KB |
Output is correct |
12 |
Correct |
979 ms |
35320 KB |
Output is correct |
13 |
Correct |
980 ms |
29560 KB |
Output is correct |
14 |
Correct |
977 ms |
35832 KB |
Output is correct |
15 |
Correct |
980 ms |
35448 KB |
Output is correct |
16 |
Correct |
970 ms |
30456 KB |
Output is correct |
17 |
Correct |
971 ms |
30972 KB |
Output is correct |
18 |
Correct |
981 ms |
31224 KB |
Output is correct |
19 |
Correct |
984 ms |
34296 KB |
Output is correct |
20 |
Correct |
972 ms |
36472 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 |
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 |
11 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 |
20 ms |
9984 KB |
Output is correct |
18 |
Correct |
13 ms |
9984 KB |
Output is correct |
19 |
Correct |
12 ms |
9984 KB |
Output is correct |
20 |
Correct |
13 ms |
9984 KB |
Output is correct |
21 |
Correct |
11 ms |
9984 KB |
Output is correct |
22 |
Correct |
12 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 |
13 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 |
13 ms |
9856 KB |
Output is correct |
30 |
Correct |
13 ms |
9984 KB |
Output is correct |
31 |
Correct |
954 ms |
37496 KB |
Output is correct |
32 |
Correct |
297 ms |
37960 KB |
Output is correct |
33 |
Correct |
943 ms |
37368 KB |
Output is correct |
34 |
Correct |
297 ms |
38012 KB |
Output is correct |
35 |
Correct |
931 ms |
34168 KB |
Output is correct |
36 |
Correct |
297 ms |
37880 KB |
Output is correct |
37 |
Correct |
936 ms |
34560 KB |
Output is correct |
38 |
Correct |
294 ms |
37496 KB |
Output is correct |
39 |
Correct |
980 ms |
32504 KB |
Output is correct |
40 |
Correct |
967 ms |
30072 KB |
Output is correct |
41 |
Correct |
980 ms |
32888 KB |
Output is correct |
42 |
Correct |
979 ms |
35320 KB |
Output is correct |
43 |
Correct |
980 ms |
29560 KB |
Output is correct |
44 |
Correct |
977 ms |
35832 KB |
Output is correct |
45 |
Correct |
980 ms |
35448 KB |
Output is correct |
46 |
Correct |
970 ms |
30456 KB |
Output is correct |
47 |
Correct |
971 ms |
30972 KB |
Output is correct |
48 |
Correct |
981 ms |
31224 KB |
Output is correct |
49 |
Correct |
984 ms |
34296 KB |
Output is correct |
50 |
Correct |
972 ms |
36472 KB |
Output is correct |
51 |
Correct |
10 ms |
9728 KB |
Output is correct |
52 |
Correct |
681 ms |
20364 KB |
Output is correct |
53 |
Correct |
710 ms |
20344 KB |
Output is correct |
54 |
Correct |
715 ms |
20372 KB |
Output is correct |
55 |
Correct |
750 ms |
20400 KB |
Output is correct |
56 |
Correct |
754 ms |
20472 KB |
Output is correct |
57 |
Correct |
733 ms |
20472 KB |
Output is correct |
58 |
Correct |
619 ms |
24184 KB |
Output is correct |
59 |
Correct |
659 ms |
24768 KB |
Output is correct |
60 |
Correct |
931 ms |
24060 KB |
Output is correct |
61 |
Correct |
954 ms |
23688 KB |
Output is correct |
62 |
Correct |
307 ms |
38264 KB |
Output is correct |
63 |
Correct |
308 ms |
37880 KB |
Output is correct |
64 |
Correct |
296 ms |
37776 KB |
Output is correct |
65 |
Correct |
300 ms |
37908 KB |
Output is correct |
66 |
Correct |
482 ms |
29424 KB |
Output is correct |
67 |
Correct |
485 ms |
29296 KB |
Output is correct |
68 |
Correct |
498 ms |
29424 KB |
Output is correct |
69 |
Correct |
506 ms |
29296 KB |
Output is correct |
70 |
Correct |
493 ms |
29296 KB |
Output is correct |
71 |
Correct |
474 ms |
29296 KB |
Output is correct |
72 |
Correct |
476 ms |
29296 KB |
Output is correct |
73 |
Correct |
502 ms |
28400 KB |
Output is correct |
74 |
Correct |
473 ms |
29296 KB |
Output is correct |
75 |
Correct |
499 ms |
29444 KB |
Output is correct |
76 |
Correct |
749 ms |
28024 KB |
Output is correct |
77 |
Correct |
755 ms |
26232 KB |
Output is correct |
78 |
Correct |
991 ms |
30968 KB |
Output is correct |
79 |
Correct |
988 ms |
28920 KB |
Output is correct |
80 |
Correct |
976 ms |
35960 KB |
Output is correct |
81 |
Correct |
991 ms |
32496 KB |
Output is correct |
82 |
Correct |
995 ms |
32804 KB |
Output is correct |
83 |
Correct |
967 ms |
29176 KB |
Output is correct |
84 |
Correct |
994 ms |
35328 KB |
Output is correct |
85 |
Correct |
979 ms |
33656 KB |
Output is correct |
86 |
Correct |
976 ms |
28796 KB |
Output is correct |
87 |
Correct |
975 ms |
30648 KB |
Output is correct |
88 |
Correct |
841 ms |
31736 KB |
Output is correct |
89 |
Correct |
850 ms |
27784 KB |
Output is correct |
90 |
Correct |
828 ms |
27512 KB |
Output is correct |
91 |
Correct |
841 ms |
30072 KB |
Output is correct |
92 |
Correct |
868 ms |
28664 KB |
Output is correct |
93 |
Correct |
885 ms |
28276 KB |
Output is correct |
94 |
Correct |
854 ms |
27752 KB |
Output is correct |
95 |
Correct |
865 ms |
29152 KB |
Output is correct |
96 |
Correct |
871 ms |
27948 KB |
Output is correct |
97 |
Correct |
841 ms |
29944 KB |
Output is correct |