# include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
using namespace std;
const int N = 3e5 + 5;
int sz[N],par[N],col[N],viss[N],blocked[N],now[N],vis[N],ans,n,k,a,b;
vector <int> v[N],vv[N];
void dfs(int a, int p) {
if (vis[a]) return ;
sz[a] = 1;
par[a] = p;
now[a] = 1;
for (int i = 0; i < v[a].size(); i++) {
int to = v[a][i];
//cout<<a<<" "<<to<<endl;
if (to == p || vis[to]) continue;
dfs(to,a);
sz[a] += sz[to];
}
}
int find_centroid(int a, int p, int raod) {
for (int i = 0; i < v[a].size(); i++) {
int to = v[a][i];
if (vis[to] || to == p) continue;
if (sz[to] > raod/2) return find_centroid(to,a,raod);
}
return a;
}
void init(int a, int p) {
if (vis[a]) return;
now[a] = 0;
for (int i = 0; i < v[a].size(); i++) {
int to = v[a][i];
if (to == p || vis[to]) continue;
init(to,a);
}
}
void init_centroid(int a, int p){
dfs(a,p);
int c = find_centroid(a, p ,sz[a]);
dfs(c,p);
vis[c] = 1;
par[c] = p;
int x = col[c];
queue <int> q;
int ff = 0;
for (int i = 0; i < vv[x].size(); i++) {
int vert1 = vv[x][i];
if (!now[vert1] | blocked[col[vert1]]) {
ff = 1;
break;
}
q.push(vv[x][i]);
viss[x] = 1;
}
int pas = 0;
vector <int> vvv;vvv.pb(x);
if (!ff) {
while (q.size()) {
int vert = q.front();
int parr = par[vert];
if (!now[vert]) {
ff = 1;break;
}
if (blocked[col[vert]]) {
ff = 1;
break;
}
q.pop();
if (parr == -1) continue;
if (vert != c && par[vert] && !viss[col[parr]]) {
pas++;
vvv.pb(col[parr]);
for (int i = 0; i < vv[col[parr]].size(); i++) {
int vert1 = vv[col[parr]][i];
if (!now[vert1] || blocked[col[vert1]]) {
ff = 1;
break;
}
q.push(vert1);
}if (ff) break;
viss[col[parr]] = 1;
}
}
}
if (!ff) {
ans = min(ans, pas);
}
for (int i = 0; i < vvv.size(); i++) viss[vvv[i]] = 0;
init(a,p);
blocked[col[c]] = 1;
vis[c] = 1;
for (int i = 0; i < v[c].size(); i++) {
if(!vis[v[c][i]]) init_centroid(v[c][i],c);
}
}
main() {
std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>n>>k;
ans = 1e9;
for (int i = 1; i <= n - 1; i++) {
cin>>a>>b;
v[a].pb(b);
v[b].pb(a);
}
for (int i = 1; i <= n; i++) {
cin>>col[i];
vv[col[i]].pb(i);
} //cout<<endl<<endl;
init_centroid(1,-1);
cout<<ans<<endl;
}
Compilation message
capital_city.cpp: In function 'void dfs(int, int)':
capital_city.cpp:14:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | for (int i = 0; i < v[a].size(); i++) {
| ~~^~~~~~~~~~~~~
capital_city.cpp: In function 'int find_centroid(int, int, int)':
capital_city.cpp:23:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for (int i = 0; i < v[a].size(); i++) {
| ~~^~~~~~~~~~~~~
capital_city.cpp: In function 'void init(int, int)':
capital_city.cpp:33:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for (int i = 0; i < v[a].size(); i++) {
| ~~^~~~~~~~~~~~~
capital_city.cpp: In function 'void init_centroid(int, int)':
capital_city.cpp:49:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for (int i = 0; i < vv[x].size(); i++) {
| ~~^~~~~~~~~~~~~~
capital_city.cpp:51:13: warning: suggest parentheses around operand of '!' or change '|' to '||' or '!' to '~' [-Wparentheses]
51 | if (!now[vert1] | blocked[col[vert1]]) {
| ^~~~~~~~~~~
capital_city.cpp:76:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | for (int i = 0; i < vv[col[parr]].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~~~~
capital_city.cpp:91:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for (int i = 0; i < vvv.size(); i++) viss[vvv[i]] = 0;
| ~~^~~~~~~~~~~~
capital_city.cpp:96:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
96 | for (int i = 0; i < v[c].size(); i++) {
| ~~^~~~~~~~~~~~~
capital_city.cpp: At global scope:
capital_city.cpp:100:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
100 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14412 KB |
Output is correct |
2 |
Correct |
11 ms |
14420 KB |
Output is correct |
3 |
Correct |
9 ms |
14412 KB |
Output is correct |
4 |
Correct |
8 ms |
14412 KB |
Output is correct |
5 |
Correct |
8 ms |
14332 KB |
Output is correct |
6 |
Correct |
8 ms |
14372 KB |
Output is correct |
7 |
Correct |
8 ms |
14416 KB |
Output is correct |
8 |
Correct |
8 ms |
14424 KB |
Output is correct |
9 |
Correct |
8 ms |
14328 KB |
Output is correct |
10 |
Correct |
8 ms |
14340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14412 KB |
Output is correct |
2 |
Correct |
11 ms |
14420 KB |
Output is correct |
3 |
Correct |
9 ms |
14412 KB |
Output is correct |
4 |
Correct |
8 ms |
14412 KB |
Output is correct |
5 |
Correct |
8 ms |
14332 KB |
Output is correct |
6 |
Correct |
8 ms |
14372 KB |
Output is correct |
7 |
Correct |
8 ms |
14416 KB |
Output is correct |
8 |
Correct |
8 ms |
14424 KB |
Output is correct |
9 |
Correct |
8 ms |
14328 KB |
Output is correct |
10 |
Correct |
8 ms |
14340 KB |
Output is correct |
11 |
Correct |
9 ms |
14580 KB |
Output is correct |
12 |
Correct |
10 ms |
14540 KB |
Output is correct |
13 |
Correct |
10 ms |
14540 KB |
Output is correct |
14 |
Correct |
13 ms |
14540 KB |
Output is correct |
15 |
Correct |
14 ms |
14572 KB |
Output is correct |
16 |
Correct |
11 ms |
14540 KB |
Output is correct |
17 |
Correct |
12 ms |
14668 KB |
Output is correct |
18 |
Correct |
9 ms |
14668 KB |
Output is correct |
19 |
Correct |
9 ms |
14668 KB |
Output is correct |
20 |
Correct |
9 ms |
14596 KB |
Output is correct |
21 |
Correct |
11 ms |
14560 KB |
Output is correct |
22 |
Correct |
9 ms |
14684 KB |
Output is correct |
23 |
Correct |
12 ms |
14668 KB |
Output is correct |
24 |
Correct |
9 ms |
14540 KB |
Output is correct |
25 |
Correct |
9 ms |
14668 KB |
Output is correct |
26 |
Correct |
10 ms |
14752 KB |
Output is correct |
27 |
Correct |
12 ms |
14648 KB |
Output is correct |
28 |
Correct |
14 ms |
14684 KB |
Output is correct |
29 |
Correct |
10 ms |
14668 KB |
Output is correct |
30 |
Correct |
9 ms |
14668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
612 ms |
45212 KB |
Output is correct |
2 |
Correct |
247 ms |
45720 KB |
Output is correct |
3 |
Correct |
642 ms |
44828 KB |
Output is correct |
4 |
Correct |
232 ms |
45776 KB |
Output is correct |
5 |
Correct |
894 ms |
42304 KB |
Output is correct |
6 |
Correct |
248 ms |
45884 KB |
Output is correct |
7 |
Correct |
834 ms |
42664 KB |
Output is correct |
8 |
Correct |
263 ms |
47584 KB |
Output is correct |
9 |
Correct |
765 ms |
42640 KB |
Output is correct |
10 |
Correct |
726 ms |
40236 KB |
Output is correct |
11 |
Correct |
722 ms |
43176 KB |
Output is correct |
12 |
Correct |
759 ms |
45764 KB |
Output is correct |
13 |
Correct |
673 ms |
39800 KB |
Output is correct |
14 |
Correct |
701 ms |
46000 KB |
Output is correct |
15 |
Correct |
718 ms |
45976 KB |
Output is correct |
16 |
Correct |
759 ms |
41164 KB |
Output is correct |
17 |
Correct |
675 ms |
41276 KB |
Output is correct |
18 |
Correct |
671 ms |
41496 KB |
Output is correct |
19 |
Correct |
763 ms |
44632 KB |
Output is correct |
20 |
Correct |
678 ms |
46928 KB |
Output is correct |
21 |
Correct |
7 ms |
14412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14412 KB |
Output is correct |
2 |
Correct |
11 ms |
14420 KB |
Output is correct |
3 |
Correct |
9 ms |
14412 KB |
Output is correct |
4 |
Correct |
8 ms |
14412 KB |
Output is correct |
5 |
Correct |
8 ms |
14332 KB |
Output is correct |
6 |
Correct |
8 ms |
14372 KB |
Output is correct |
7 |
Correct |
8 ms |
14416 KB |
Output is correct |
8 |
Correct |
8 ms |
14424 KB |
Output is correct |
9 |
Correct |
8 ms |
14328 KB |
Output is correct |
10 |
Correct |
8 ms |
14340 KB |
Output is correct |
11 |
Correct |
9 ms |
14580 KB |
Output is correct |
12 |
Correct |
10 ms |
14540 KB |
Output is correct |
13 |
Correct |
10 ms |
14540 KB |
Output is correct |
14 |
Correct |
13 ms |
14540 KB |
Output is correct |
15 |
Correct |
14 ms |
14572 KB |
Output is correct |
16 |
Correct |
11 ms |
14540 KB |
Output is correct |
17 |
Correct |
12 ms |
14668 KB |
Output is correct |
18 |
Correct |
9 ms |
14668 KB |
Output is correct |
19 |
Correct |
9 ms |
14668 KB |
Output is correct |
20 |
Correct |
9 ms |
14596 KB |
Output is correct |
21 |
Correct |
11 ms |
14560 KB |
Output is correct |
22 |
Correct |
9 ms |
14684 KB |
Output is correct |
23 |
Correct |
12 ms |
14668 KB |
Output is correct |
24 |
Correct |
9 ms |
14540 KB |
Output is correct |
25 |
Correct |
9 ms |
14668 KB |
Output is correct |
26 |
Correct |
10 ms |
14752 KB |
Output is correct |
27 |
Correct |
12 ms |
14648 KB |
Output is correct |
28 |
Correct |
14 ms |
14684 KB |
Output is correct |
29 |
Correct |
10 ms |
14668 KB |
Output is correct |
30 |
Correct |
9 ms |
14668 KB |
Output is correct |
31 |
Correct |
612 ms |
45212 KB |
Output is correct |
32 |
Correct |
247 ms |
45720 KB |
Output is correct |
33 |
Correct |
642 ms |
44828 KB |
Output is correct |
34 |
Correct |
232 ms |
45776 KB |
Output is correct |
35 |
Correct |
894 ms |
42304 KB |
Output is correct |
36 |
Correct |
248 ms |
45884 KB |
Output is correct |
37 |
Correct |
834 ms |
42664 KB |
Output is correct |
38 |
Correct |
263 ms |
47584 KB |
Output is correct |
39 |
Correct |
765 ms |
42640 KB |
Output is correct |
40 |
Correct |
726 ms |
40236 KB |
Output is correct |
41 |
Correct |
722 ms |
43176 KB |
Output is correct |
42 |
Correct |
759 ms |
45764 KB |
Output is correct |
43 |
Correct |
673 ms |
39800 KB |
Output is correct |
44 |
Correct |
701 ms |
46000 KB |
Output is correct |
45 |
Correct |
718 ms |
45976 KB |
Output is correct |
46 |
Correct |
759 ms |
41164 KB |
Output is correct |
47 |
Correct |
675 ms |
41276 KB |
Output is correct |
48 |
Correct |
671 ms |
41496 KB |
Output is correct |
49 |
Correct |
763 ms |
44632 KB |
Output is correct |
50 |
Correct |
678 ms |
46928 KB |
Output is correct |
51 |
Correct |
7 ms |
14412 KB |
Output is correct |
52 |
Correct |
474 ms |
30892 KB |
Output is correct |
53 |
Correct |
497 ms |
30616 KB |
Output is correct |
54 |
Correct |
502 ms |
30596 KB |
Output is correct |
55 |
Correct |
455 ms |
30852 KB |
Output is correct |
56 |
Correct |
485 ms |
30600 KB |
Output is correct |
57 |
Correct |
471 ms |
30684 KB |
Output is correct |
58 |
Correct |
539 ms |
34600 KB |
Output is correct |
59 |
Correct |
447 ms |
34600 KB |
Output is correct |
60 |
Execution timed out |
3060 ms |
34032 KB |
Time limit exceeded |
61 |
Halted |
0 ms |
0 KB |
- |