#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define f first
#define s second
#define endl "\n"
const int N = 2e5 + 5, mod = 1e9 + 7; //!
int t, sz[N], f[N], n, vis[N], c[N], ans, p[N], col[N];
int cnt[N];
vector<int> rem, x[N];
vector<int> V[N];
queue<int> q;
void dfs0(int u,int p) {
sz[u] = 1;
for(int i = 0; i < V[u].size(); i++) {
if(V[u][i] == p || f[V[u][i]]) continue;
dfs0(V[u][i], u);
sz[u] += sz[V[u][i]];
}
}
int find(int u, int p, int SZ) {
for(int i = 0; i < V[u].size(); i++) {
if(V[u][i] == p || f[V[u][i]]) continue;
if(sz[V[u][i]] > SZ / 2) return find(V[u][i], u, SZ);
}
return u;
}
void dfs(int u,int C) {
rem.push_back(c[u]);
x[c[u]].push_back(u);
for(int i = 0; i < V[u].size(); i++) {
if(V[u][i] == p[u] || f[V[u][i]]) continue;
p[V[u][i]] = u;
dfs(V[u][i], C);
}
}
void decompose(int U) {
dfs0(U, 0);
int C = find(U, 0, sz[U]);
p[C] = 0;
dfs(C, C);
int cn = 0;
q.push(C);
while(q.size()) {
int u = q.front();
q.pop();
if(cnt[c[u]] != x[c[u]].size()) cn = n;
if(vis[u] == C) continue;
if(col[c[u]] != C) {
cn++;
for(int j = 0; j < x[c[u]].size(); j++) {
q.push(x[c[u]][j]);
}
}
col[c[u]] = C; vis[u] = C;
if(p[u]) q.push(p[u]);
}
while(rem.size()) x[rem.back()].clear(), rem.pop_back();
ans = min(ans, cn);
f[C] = 1;
for(int i = 0; i < V[C].size(); i++) {
if(!f[V[C][i]]) decompose(V[C][i]);
}
}
main() {
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int k;
cin >> n >> k;
for(int i = 2; i <= n; i++) {
int u, v;
cin >> u >> v;
V[u].push_back(v);
V[v].push_back(u);
}
for(int i = 1; i <= n; i++) cin >> c[i], cnt[c[i]]++;
ans = k;
decompose(1);
cout << --ans;
}
Compilation message
capital_city.cpp: In function 'void dfs0(long long int, long long int)':
capital_city.cpp:16:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | for(int i = 0; i < V[u].size(); i++) {
| ~~^~~~~~~~~~~~~
capital_city.cpp: In function 'long long int find(long long int, long long int, long long int)':
capital_city.cpp:23:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for(int i = 0; i < V[u].size(); i++) {
| ~~^~~~~~~~~~~~~
capital_city.cpp: In function 'void dfs(long long int, long long int)':
capital_city.cpp:32:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for(int i = 0; i < V[u].size(); i++) {
| ~~^~~~~~~~~~~~~
capital_city.cpp: In function 'void decompose(long long int)':
capital_city.cpp:49:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | if(cnt[c[u]] != x[c[u]].size()) cn = n;
| ~~~~~~~~~~^~~~~~~~~~~~~~~~~
capital_city.cpp:53:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for(int j = 0; j < x[c[u]].size(); j++) {
| ~~^~~~~~~~~~~~~~~~
capital_city.cpp:63:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for(int i = 0; i < V[C].size(); i++) {
| ~~^~~~~~~~~~~~~
capital_city.cpp: At global scope:
capital_city.cpp:67:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
67 | main() {
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9688 KB |
Output is correct |
2 |
Correct |
5 ms |
9732 KB |
Output is correct |
3 |
Correct |
6 ms |
9692 KB |
Output is correct |
4 |
Correct |
5 ms |
9728 KB |
Output is correct |
5 |
Correct |
6 ms |
9652 KB |
Output is correct |
6 |
Correct |
5 ms |
9696 KB |
Output is correct |
7 |
Correct |
5 ms |
9736 KB |
Output is correct |
8 |
Correct |
6 ms |
9688 KB |
Output is correct |
9 |
Correct |
6 ms |
9684 KB |
Output is correct |
10 |
Correct |
6 ms |
9728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9688 KB |
Output is correct |
2 |
Correct |
5 ms |
9732 KB |
Output is correct |
3 |
Correct |
6 ms |
9692 KB |
Output is correct |
4 |
Correct |
5 ms |
9728 KB |
Output is correct |
5 |
Correct |
6 ms |
9652 KB |
Output is correct |
6 |
Correct |
5 ms |
9696 KB |
Output is correct |
7 |
Correct |
5 ms |
9736 KB |
Output is correct |
8 |
Correct |
6 ms |
9688 KB |
Output is correct |
9 |
Correct |
6 ms |
9684 KB |
Output is correct |
10 |
Correct |
6 ms |
9728 KB |
Output is correct |
11 |
Correct |
9 ms |
9940 KB |
Output is correct |
12 |
Correct |
11 ms |
9988 KB |
Output is correct |
13 |
Correct |
8 ms |
9940 KB |
Output is correct |
14 |
Correct |
8 ms |
9924 KB |
Output is correct |
15 |
Correct |
9 ms |
9940 KB |
Output is correct |
16 |
Correct |
8 ms |
10012 KB |
Output is correct |
17 |
Correct |
7 ms |
9992 KB |
Output is correct |
18 |
Correct |
7 ms |
10000 KB |
Output is correct |
19 |
Correct |
6 ms |
10044 KB |
Output is correct |
20 |
Correct |
7 ms |
9996 KB |
Output is correct |
21 |
Correct |
8 ms |
10120 KB |
Output is correct |
22 |
Correct |
7 ms |
10068 KB |
Output is correct |
23 |
Correct |
10 ms |
9992 KB |
Output is correct |
24 |
Correct |
7 ms |
9940 KB |
Output is correct |
25 |
Correct |
9 ms |
10000 KB |
Output is correct |
26 |
Correct |
9 ms |
10068 KB |
Output is correct |
27 |
Correct |
9 ms |
10068 KB |
Output is correct |
28 |
Correct |
8 ms |
10056 KB |
Output is correct |
29 |
Correct |
7 ms |
9992 KB |
Output is correct |
30 |
Correct |
9 ms |
9996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
842 ms |
42348 KB |
Output is correct |
2 |
Correct |
211 ms |
46244 KB |
Output is correct |
3 |
Correct |
819 ms |
45524 KB |
Output is correct |
4 |
Correct |
233 ms |
46196 KB |
Output is correct |
5 |
Correct |
823 ms |
43224 KB |
Output is correct |
6 |
Correct |
229 ms |
46160 KB |
Output is correct |
7 |
Correct |
807 ms |
43500 KB |
Output is correct |
8 |
Correct |
232 ms |
46136 KB |
Output is correct |
9 |
Correct |
987 ms |
41872 KB |
Output is correct |
10 |
Correct |
970 ms |
40256 KB |
Output is correct |
11 |
Correct |
940 ms |
42360 KB |
Output is correct |
12 |
Correct |
954 ms |
44408 KB |
Output is correct |
13 |
Correct |
961 ms |
39628 KB |
Output is correct |
14 |
Correct |
1004 ms |
44728 KB |
Output is correct |
15 |
Correct |
922 ms |
44324 KB |
Output is correct |
16 |
Correct |
934 ms |
40320 KB |
Output is correct |
17 |
Correct |
939 ms |
40892 KB |
Output is correct |
18 |
Correct |
924 ms |
40892 KB |
Output is correct |
19 |
Correct |
996 ms |
43440 KB |
Output is correct |
20 |
Correct |
957 ms |
45392 KB |
Output is correct |
21 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9688 KB |
Output is correct |
2 |
Correct |
5 ms |
9732 KB |
Output is correct |
3 |
Correct |
6 ms |
9692 KB |
Output is correct |
4 |
Correct |
5 ms |
9728 KB |
Output is correct |
5 |
Correct |
6 ms |
9652 KB |
Output is correct |
6 |
Correct |
5 ms |
9696 KB |
Output is correct |
7 |
Correct |
5 ms |
9736 KB |
Output is correct |
8 |
Correct |
6 ms |
9688 KB |
Output is correct |
9 |
Correct |
6 ms |
9684 KB |
Output is correct |
10 |
Correct |
6 ms |
9728 KB |
Output is correct |
11 |
Correct |
9 ms |
9940 KB |
Output is correct |
12 |
Correct |
11 ms |
9988 KB |
Output is correct |
13 |
Correct |
8 ms |
9940 KB |
Output is correct |
14 |
Correct |
8 ms |
9924 KB |
Output is correct |
15 |
Correct |
9 ms |
9940 KB |
Output is correct |
16 |
Correct |
8 ms |
10012 KB |
Output is correct |
17 |
Correct |
7 ms |
9992 KB |
Output is correct |
18 |
Correct |
7 ms |
10000 KB |
Output is correct |
19 |
Correct |
6 ms |
10044 KB |
Output is correct |
20 |
Correct |
7 ms |
9996 KB |
Output is correct |
21 |
Correct |
8 ms |
10120 KB |
Output is correct |
22 |
Correct |
7 ms |
10068 KB |
Output is correct |
23 |
Correct |
10 ms |
9992 KB |
Output is correct |
24 |
Correct |
7 ms |
9940 KB |
Output is correct |
25 |
Correct |
9 ms |
10000 KB |
Output is correct |
26 |
Correct |
9 ms |
10068 KB |
Output is correct |
27 |
Correct |
9 ms |
10068 KB |
Output is correct |
28 |
Correct |
8 ms |
10056 KB |
Output is correct |
29 |
Correct |
7 ms |
9992 KB |
Output is correct |
30 |
Correct |
9 ms |
9996 KB |
Output is correct |
31 |
Correct |
842 ms |
42348 KB |
Output is correct |
32 |
Correct |
211 ms |
46244 KB |
Output is correct |
33 |
Correct |
819 ms |
45524 KB |
Output is correct |
34 |
Correct |
233 ms |
46196 KB |
Output is correct |
35 |
Correct |
823 ms |
43224 KB |
Output is correct |
36 |
Correct |
229 ms |
46160 KB |
Output is correct |
37 |
Correct |
807 ms |
43500 KB |
Output is correct |
38 |
Correct |
232 ms |
46136 KB |
Output is correct |
39 |
Correct |
987 ms |
41872 KB |
Output is correct |
40 |
Correct |
970 ms |
40256 KB |
Output is correct |
41 |
Correct |
940 ms |
42360 KB |
Output is correct |
42 |
Correct |
954 ms |
44408 KB |
Output is correct |
43 |
Correct |
961 ms |
39628 KB |
Output is correct |
44 |
Correct |
1004 ms |
44728 KB |
Output is correct |
45 |
Correct |
922 ms |
44324 KB |
Output is correct |
46 |
Correct |
934 ms |
40320 KB |
Output is correct |
47 |
Correct |
939 ms |
40892 KB |
Output is correct |
48 |
Correct |
924 ms |
40892 KB |
Output is correct |
49 |
Correct |
996 ms |
43440 KB |
Output is correct |
50 |
Correct |
957 ms |
45392 KB |
Output is correct |
51 |
Correct |
5 ms |
9684 KB |
Output is correct |
52 |
Correct |
615 ms |
34248 KB |
Output is correct |
53 |
Correct |
625 ms |
34336 KB |
Output is correct |
54 |
Correct |
574 ms |
34300 KB |
Output is correct |
55 |
Correct |
655 ms |
34320 KB |
Output is correct |
56 |
Correct |
572 ms |
34292 KB |
Output is correct |
57 |
Correct |
618 ms |
34232 KB |
Output is correct |
58 |
Correct |
601 ms |
36804 KB |
Output is correct |
59 |
Correct |
559 ms |
37180 KB |
Output is correct |
60 |
Correct |
737 ms |
36276 KB |
Output is correct |
61 |
Correct |
777 ms |
36060 KB |
Output is correct |
62 |
Correct |
209 ms |
46216 KB |
Output is correct |
63 |
Correct |
222 ms |
46348 KB |
Output is correct |
64 |
Correct |
253 ms |
46192 KB |
Output is correct |
65 |
Correct |
204 ms |
46132 KB |
Output is correct |
66 |
Correct |
480 ms |
40076 KB |
Output is correct |
67 |
Correct |
420 ms |
40016 KB |
Output is correct |
68 |
Correct |
423 ms |
40116 KB |
Output is correct |
69 |
Correct |
429 ms |
40096 KB |
Output is correct |
70 |
Correct |
423 ms |
40176 KB |
Output is correct |
71 |
Correct |
416 ms |
40064 KB |
Output is correct |
72 |
Correct |
420 ms |
39992 KB |
Output is correct |
73 |
Correct |
439 ms |
39828 KB |
Output is correct |
74 |
Correct |
422 ms |
40120 KB |
Output is correct |
75 |
Correct |
432 ms |
40000 KB |
Output is correct |
76 |
Correct |
731 ms |
39840 KB |
Output is correct |
77 |
Correct |
635 ms |
38308 KB |
Output is correct |
78 |
Correct |
867 ms |
40660 KB |
Output is correct |
79 |
Correct |
874 ms |
39096 KB |
Output is correct |
80 |
Correct |
873 ms |
44984 KB |
Output is correct |
81 |
Correct |
856 ms |
42108 KB |
Output is correct |
82 |
Correct |
891 ms |
42168 KB |
Output is correct |
83 |
Correct |
884 ms |
39536 KB |
Output is correct |
84 |
Correct |
911 ms |
44192 KB |
Output is correct |
85 |
Correct |
904 ms |
42996 KB |
Output is correct |
86 |
Correct |
881 ms |
39208 KB |
Output is correct |
87 |
Correct |
881 ms |
40524 KB |
Output is correct |
88 |
Correct |
751 ms |
41840 KB |
Output is correct |
89 |
Correct |
743 ms |
38800 KB |
Output is correct |
90 |
Correct |
792 ms |
38764 KB |
Output is correct |
91 |
Correct |
768 ms |
40756 KB |
Output is correct |
92 |
Correct |
802 ms |
39224 KB |
Output is correct |
93 |
Correct |
836 ms |
39324 KB |
Output is correct |
94 |
Correct |
774 ms |
38800 KB |
Output is correct |
95 |
Correct |
791 ms |
40184 KB |
Output is correct |
96 |
Correct |
788 ms |
38732 KB |
Output is correct |
97 |
Correct |
763 ms |
40512 KB |
Output is correct |