#include <bits/stdc++.h>
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, int &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));
if(mx <= (all + 1) / 2) ret = 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) {
get_sz(u, u), find_cen(u, u, sz[u], u);
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:49: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:62: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:64: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: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 |
12 ms |
9856 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9728 KB |
Output is correct |
4 |
Correct |
12 ms |
9728 KB |
Output is correct |
5 |
Correct |
10 ms |
9600 KB |
Output is correct |
6 |
Correct |
12 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 |
12 ms |
9856 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9728 KB |
Output is correct |
4 |
Correct |
12 ms |
9728 KB |
Output is correct |
5 |
Correct |
10 ms |
9600 KB |
Output is correct |
6 |
Correct |
12 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 |
14 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 |
9984 KB |
Output is correct |
16 |
Correct |
12 ms |
9984 KB |
Output is correct |
17 |
Correct |
12 ms |
9984 KB |
Output is correct |
18 |
Correct |
12 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 |
12 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 |
13 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 |
998 ms |
44280 KB |
Output is correct |
2 |
Correct |
334 ms |
44792 KB |
Output is correct |
3 |
Correct |
1041 ms |
43896 KB |
Output is correct |
4 |
Correct |
307 ms |
44664 KB |
Output is correct |
5 |
Correct |
992 ms |
40312 KB |
Output is correct |
6 |
Correct |
306 ms |
44792 KB |
Output is correct |
7 |
Correct |
1130 ms |
40824 KB |
Output is correct |
8 |
Correct |
324 ms |
44152 KB |
Output is correct |
9 |
Correct |
968 ms |
38412 KB |
Output is correct |
10 |
Correct |
1084 ms |
35448 KB |
Output is correct |
11 |
Correct |
953 ms |
38908 KB |
Output is correct |
12 |
Correct |
1240 ms |
42104 KB |
Output is correct |
13 |
Correct |
955 ms |
35116 KB |
Output is correct |
14 |
Correct |
956 ms |
42616 KB |
Output is correct |
15 |
Correct |
962 ms |
41976 KB |
Output is correct |
16 |
Correct |
952 ms |
36088 KB |
Output is correct |
17 |
Correct |
1086 ms |
36820 KB |
Output is correct |
18 |
Correct |
1037 ms |
36856 KB |
Output is correct |
19 |
Correct |
977 ms |
40696 KB |
Output is correct |
20 |
Correct |
970 ms |
43416 KB |
Output is correct |
21 |
Correct |
12 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
9856 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9728 KB |
Output is correct |
4 |
Correct |
12 ms |
9728 KB |
Output is correct |
5 |
Correct |
10 ms |
9600 KB |
Output is correct |
6 |
Correct |
12 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 |
14 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 |
9984 KB |
Output is correct |
16 |
Correct |
12 ms |
9984 KB |
Output is correct |
17 |
Correct |
12 ms |
9984 KB |
Output is correct |
18 |
Correct |
12 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 |
12 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 |
13 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 |
998 ms |
44280 KB |
Output is correct |
32 |
Correct |
334 ms |
44792 KB |
Output is correct |
33 |
Correct |
1041 ms |
43896 KB |
Output is correct |
34 |
Correct |
307 ms |
44664 KB |
Output is correct |
35 |
Correct |
992 ms |
40312 KB |
Output is correct |
36 |
Correct |
306 ms |
44792 KB |
Output is correct |
37 |
Correct |
1130 ms |
40824 KB |
Output is correct |
38 |
Correct |
324 ms |
44152 KB |
Output is correct |
39 |
Correct |
968 ms |
38412 KB |
Output is correct |
40 |
Correct |
1084 ms |
35448 KB |
Output is correct |
41 |
Correct |
953 ms |
38908 KB |
Output is correct |
42 |
Correct |
1240 ms |
42104 KB |
Output is correct |
43 |
Correct |
955 ms |
35116 KB |
Output is correct |
44 |
Correct |
956 ms |
42616 KB |
Output is correct |
45 |
Correct |
962 ms |
41976 KB |
Output is correct |
46 |
Correct |
952 ms |
36088 KB |
Output is correct |
47 |
Correct |
1086 ms |
36820 KB |
Output is correct |
48 |
Correct |
1037 ms |
36856 KB |
Output is correct |
49 |
Correct |
977 ms |
40696 KB |
Output is correct |
50 |
Correct |
970 ms |
43416 KB |
Output is correct |
51 |
Correct |
12 ms |
9728 KB |
Output is correct |
52 |
Correct |
606 ms |
23804 KB |
Output is correct |
53 |
Correct |
703 ms |
23800 KB |
Output is correct |
54 |
Correct |
728 ms |
23928 KB |
Output is correct |
55 |
Correct |
763 ms |
24004 KB |
Output is correct |
56 |
Correct |
695 ms |
23804 KB |
Output is correct |
57 |
Correct |
680 ms |
23928 KB |
Output is correct |
58 |
Correct |
644 ms |
28664 KB |
Output is correct |
59 |
Correct |
694 ms |
29048 KB |
Output is correct |
60 |
Correct |
827 ms |
28024 KB |
Output is correct |
61 |
Correct |
1018 ms |
27768 KB |
Output is correct |
62 |
Correct |
301 ms |
44664 KB |
Output is correct |
63 |
Correct |
312 ms |
44768 KB |
Output is correct |
64 |
Correct |
301 ms |
44408 KB |
Output is correct |
65 |
Correct |
311 ms |
44664 KB |
Output is correct |
66 |
Correct |
564 ms |
34928 KB |
Output is correct |
67 |
Correct |
534 ms |
34544 KB |
Output is correct |
68 |
Correct |
473 ms |
34544 KB |
Output is correct |
69 |
Correct |
504 ms |
34672 KB |
Output is correct |
70 |
Correct |
477 ms |
34544 KB |
Output is correct |
71 |
Correct |
460 ms |
34544 KB |
Output is correct |
72 |
Correct |
533 ms |
34672 KB |
Output is correct |
73 |
Correct |
472 ms |
33520 KB |
Output is correct |
74 |
Correct |
482 ms |
34764 KB |
Output is correct |
75 |
Correct |
482 ms |
34672 KB |
Output is correct |
76 |
Correct |
798 ms |
33016 KB |
Output is correct |
77 |
Correct |
699 ms |
30456 KB |
Output is correct |
78 |
Correct |
959 ms |
36600 KB |
Output is correct |
79 |
Correct |
1175 ms |
34424 KB |
Output is correct |
80 |
Correct |
998 ms |
42872 KB |
Output is correct |
81 |
Correct |
1048 ms |
38524 KB |
Output is correct |
82 |
Correct |
1093 ms |
38904 KB |
Output is correct |
83 |
Correct |
1054 ms |
34664 KB |
Output is correct |
84 |
Correct |
1002 ms |
41848 KB |
Output is correct |
85 |
Correct |
935 ms |
39800 KB |
Output is correct |
86 |
Correct |
955 ms |
34192 KB |
Output is correct |
87 |
Correct |
949 ms |
36344 KB |
Output is correct |
88 |
Correct |
839 ms |
37624 KB |
Output is correct |
89 |
Correct |
844 ms |
33020 KB |
Output is correct |
90 |
Correct |
989 ms |
32760 KB |
Output is correct |
91 |
Correct |
837 ms |
35608 KB |
Output is correct |
92 |
Correct |
851 ms |
33784 KB |
Output is correct |
93 |
Correct |
833 ms |
33528 KB |
Output is correct |
94 |
Correct |
835 ms |
32888 KB |
Output is correct |
95 |
Correct |
864 ms |
34680 KB |
Output is correct |
96 |
Correct |
822 ms |
32892 KB |
Output is correct |
97 |
Correct |
875 ms |
35576 KB |
Output is correct |