# include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
using namespace std;
const int N = 2e5 + 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];
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 (ff || pas > ans) break;
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++;
if (pas > ans) break;
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);
}
init_centroid(1,-1);
cout<<ans;
}
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:22:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for (int i = 0; i < v[a].size(); i++) {
| ~~^~~~~~~~~~~~~
capital_city.cpp: In function 'void init(int, int)':
capital_city.cpp:32:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for (int i = 0; i < v[a].size(); i++) {
| ~~^~~~~~~~~~~~~
capital_city.cpp: In function 'void init_centroid(int, int)':
capital_city.cpp:48:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for (int i = 0; i < vv[x].size(); i++) {
| ~~^~~~~~~~~~~~~~
capital_city.cpp:50:13: warning: suggest parentheses around operand of '!' or change '|' to '||' or '!' to '~' [-Wparentheses]
50 | if (!now[vert1] | blocked[col[vert1]]) {
| ^~~~~~~~~~~
capital_city.cpp:77:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for (int i = 0; i < vv[col[parr]].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~~~~
capital_city.cpp:92:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for (int i = 0; i < vvv.size(); i++) viss[vvv[i]] = 0;
| ~~^~~~~~~~~~~~
capital_city.cpp:97:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
97 | for (int i = 0; i < v[c].size(); i++) {
| ~~^~~~~~~~~~~~~
capital_city.cpp: At global scope:
capital_city.cpp:101:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
101 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
2 |
Correct |
5 ms |
9676 KB |
Output is correct |
3 |
Correct |
5 ms |
9676 KB |
Output is correct |
4 |
Correct |
5 ms |
9676 KB |
Output is correct |
5 |
Correct |
5 ms |
9752 KB |
Output is correct |
6 |
Correct |
6 ms |
9676 KB |
Output is correct |
7 |
Correct |
5 ms |
9676 KB |
Output is correct |
8 |
Correct |
5 ms |
9676 KB |
Output is correct |
9 |
Correct |
5 ms |
9632 KB |
Output is correct |
10 |
Correct |
5 ms |
9676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
2 |
Correct |
5 ms |
9676 KB |
Output is correct |
3 |
Correct |
5 ms |
9676 KB |
Output is correct |
4 |
Correct |
5 ms |
9676 KB |
Output is correct |
5 |
Correct |
5 ms |
9752 KB |
Output is correct |
6 |
Correct |
6 ms |
9676 KB |
Output is correct |
7 |
Correct |
5 ms |
9676 KB |
Output is correct |
8 |
Correct |
5 ms |
9676 KB |
Output is correct |
9 |
Correct |
5 ms |
9632 KB |
Output is correct |
10 |
Correct |
5 ms |
9676 KB |
Output is correct |
11 |
Correct |
6 ms |
9804 KB |
Output is correct |
12 |
Correct |
7 ms |
9884 KB |
Output is correct |
13 |
Correct |
7 ms |
9768 KB |
Output is correct |
14 |
Correct |
7 ms |
9804 KB |
Output is correct |
15 |
Correct |
6 ms |
9804 KB |
Output is correct |
16 |
Correct |
7 ms |
9804 KB |
Output is correct |
17 |
Correct |
6 ms |
9932 KB |
Output is correct |
18 |
Correct |
7 ms |
9932 KB |
Output is correct |
19 |
Correct |
6 ms |
9932 KB |
Output is correct |
20 |
Correct |
7 ms |
9916 KB |
Output is correct |
21 |
Correct |
6 ms |
9932 KB |
Output is correct |
22 |
Correct |
7 ms |
9932 KB |
Output is correct |
23 |
Correct |
7 ms |
9932 KB |
Output is correct |
24 |
Correct |
6 ms |
9932 KB |
Output is correct |
25 |
Correct |
7 ms |
9932 KB |
Output is correct |
26 |
Correct |
7 ms |
9940 KB |
Output is correct |
27 |
Correct |
7 ms |
9884 KB |
Output is correct |
28 |
Correct |
7 ms |
9908 KB |
Output is correct |
29 |
Correct |
7 ms |
9944 KB |
Output is correct |
30 |
Correct |
6 ms |
9932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
521 ms |
39044 KB |
Output is correct |
2 |
Correct |
211 ms |
39332 KB |
Output is correct |
3 |
Correct |
524 ms |
38732 KB |
Output is correct |
4 |
Correct |
206 ms |
39424 KB |
Output is correct |
5 |
Correct |
619 ms |
35828 KB |
Output is correct |
6 |
Correct |
214 ms |
39492 KB |
Output is correct |
7 |
Correct |
622 ms |
36236 KB |
Output is correct |
8 |
Correct |
217 ms |
39112 KB |
Output is correct |
9 |
Correct |
640 ms |
34408 KB |
Output is correct |
10 |
Correct |
630 ms |
31836 KB |
Output is correct |
11 |
Correct |
620 ms |
34876 KB |
Output is correct |
12 |
Correct |
660 ms |
37352 KB |
Output is correct |
13 |
Correct |
620 ms |
31300 KB |
Output is correct |
14 |
Correct |
635 ms |
37752 KB |
Output is correct |
15 |
Correct |
644 ms |
37520 KB |
Output is correct |
16 |
Correct |
644 ms |
32692 KB |
Output is correct |
17 |
Correct |
648 ms |
32964 KB |
Output is correct |
18 |
Correct |
627 ms |
33256 KB |
Output is correct |
19 |
Correct |
622 ms |
36196 KB |
Output is correct |
20 |
Correct |
674 ms |
38528 KB |
Output is correct |
21 |
Correct |
5 ms |
9676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
2 |
Correct |
5 ms |
9676 KB |
Output is correct |
3 |
Correct |
5 ms |
9676 KB |
Output is correct |
4 |
Correct |
5 ms |
9676 KB |
Output is correct |
5 |
Correct |
5 ms |
9752 KB |
Output is correct |
6 |
Correct |
6 ms |
9676 KB |
Output is correct |
7 |
Correct |
5 ms |
9676 KB |
Output is correct |
8 |
Correct |
5 ms |
9676 KB |
Output is correct |
9 |
Correct |
5 ms |
9632 KB |
Output is correct |
10 |
Correct |
5 ms |
9676 KB |
Output is correct |
11 |
Correct |
6 ms |
9804 KB |
Output is correct |
12 |
Correct |
7 ms |
9884 KB |
Output is correct |
13 |
Correct |
7 ms |
9768 KB |
Output is correct |
14 |
Correct |
7 ms |
9804 KB |
Output is correct |
15 |
Correct |
6 ms |
9804 KB |
Output is correct |
16 |
Correct |
7 ms |
9804 KB |
Output is correct |
17 |
Correct |
6 ms |
9932 KB |
Output is correct |
18 |
Correct |
7 ms |
9932 KB |
Output is correct |
19 |
Correct |
6 ms |
9932 KB |
Output is correct |
20 |
Correct |
7 ms |
9916 KB |
Output is correct |
21 |
Correct |
6 ms |
9932 KB |
Output is correct |
22 |
Correct |
7 ms |
9932 KB |
Output is correct |
23 |
Correct |
7 ms |
9932 KB |
Output is correct |
24 |
Correct |
6 ms |
9932 KB |
Output is correct |
25 |
Correct |
7 ms |
9932 KB |
Output is correct |
26 |
Correct |
7 ms |
9940 KB |
Output is correct |
27 |
Correct |
7 ms |
9884 KB |
Output is correct |
28 |
Correct |
7 ms |
9908 KB |
Output is correct |
29 |
Correct |
7 ms |
9944 KB |
Output is correct |
30 |
Correct |
6 ms |
9932 KB |
Output is correct |
31 |
Correct |
521 ms |
39044 KB |
Output is correct |
32 |
Correct |
211 ms |
39332 KB |
Output is correct |
33 |
Correct |
524 ms |
38732 KB |
Output is correct |
34 |
Correct |
206 ms |
39424 KB |
Output is correct |
35 |
Correct |
619 ms |
35828 KB |
Output is correct |
36 |
Correct |
214 ms |
39492 KB |
Output is correct |
37 |
Correct |
622 ms |
36236 KB |
Output is correct |
38 |
Correct |
217 ms |
39112 KB |
Output is correct |
39 |
Correct |
640 ms |
34408 KB |
Output is correct |
40 |
Correct |
630 ms |
31836 KB |
Output is correct |
41 |
Correct |
620 ms |
34876 KB |
Output is correct |
42 |
Correct |
660 ms |
37352 KB |
Output is correct |
43 |
Correct |
620 ms |
31300 KB |
Output is correct |
44 |
Correct |
635 ms |
37752 KB |
Output is correct |
45 |
Correct |
644 ms |
37520 KB |
Output is correct |
46 |
Correct |
644 ms |
32692 KB |
Output is correct |
47 |
Correct |
648 ms |
32964 KB |
Output is correct |
48 |
Correct |
627 ms |
33256 KB |
Output is correct |
49 |
Correct |
622 ms |
36196 KB |
Output is correct |
50 |
Correct |
674 ms |
38528 KB |
Output is correct |
51 |
Correct |
5 ms |
9676 KB |
Output is correct |
52 |
Correct |
417 ms |
22524 KB |
Output is correct |
53 |
Correct |
452 ms |
22368 KB |
Output is correct |
54 |
Correct |
430 ms |
22428 KB |
Output is correct |
55 |
Correct |
433 ms |
22460 KB |
Output is correct |
56 |
Correct |
443 ms |
22392 KB |
Output is correct |
57 |
Correct |
417 ms |
22460 KB |
Output is correct |
58 |
Correct |
400 ms |
25924 KB |
Output is correct |
59 |
Correct |
370 ms |
26196 KB |
Output is correct |
60 |
Correct |
554 ms |
25440 KB |
Output is correct |
61 |
Correct |
614 ms |
28940 KB |
Output is correct |
62 |
Correct |
210 ms |
43108 KB |
Output is correct |
63 |
Correct |
213 ms |
43032 KB |
Output is correct |
64 |
Correct |
220 ms |
42996 KB |
Output is correct |
65 |
Correct |
219 ms |
43228 KB |
Output is correct |
66 |
Correct |
349 ms |
34616 KB |
Output is correct |
67 |
Correct |
371 ms |
34460 KB |
Output is correct |
68 |
Correct |
332 ms |
34608 KB |
Output is correct |
69 |
Correct |
389 ms |
34728 KB |
Output is correct |
70 |
Correct |
339 ms |
34492 KB |
Output is correct |
71 |
Correct |
341 ms |
34516 KB |
Output is correct |
72 |
Correct |
333 ms |
34664 KB |
Output is correct |
73 |
Correct |
393 ms |
33692 KB |
Output is correct |
74 |
Correct |
319 ms |
34600 KB |
Output is correct |
75 |
Correct |
358 ms |
34568 KB |
Output is correct |
76 |
Correct |
476 ms |
33268 KB |
Output is correct |
77 |
Correct |
488 ms |
31656 KB |
Output is correct |
78 |
Correct |
622 ms |
36484 KB |
Output is correct |
79 |
Correct |
614 ms |
34404 KB |
Output is correct |
80 |
Correct |
624 ms |
41764 KB |
Output is correct |
81 |
Correct |
639 ms |
38124 KB |
Output is correct |
82 |
Correct |
619 ms |
38248 KB |
Output is correct |
83 |
Correct |
617 ms |
34912 KB |
Output is correct |
84 |
Correct |
649 ms |
40684 KB |
Output is correct |
85 |
Correct |
650 ms |
39180 KB |
Output is correct |
86 |
Correct |
642 ms |
34448 KB |
Output is correct |
87 |
Correct |
645 ms |
36364 KB |
Output is correct |
88 |
Correct |
524 ms |
37380 KB |
Output is correct |
89 |
Correct |
525 ms |
33336 KB |
Output is correct |
90 |
Correct |
534 ms |
33068 KB |
Output is correct |
91 |
Correct |
544 ms |
35544 KB |
Output is correct |
92 |
Correct |
535 ms |
34116 KB |
Output is correct |
93 |
Correct |
556 ms |
33776 KB |
Output is correct |
94 |
Correct |
523 ms |
33152 KB |
Output is correct |
95 |
Correct |
530 ms |
34756 KB |
Output is correct |
96 |
Correct |
521 ms |
33416 KB |
Output is correct |
97 |
Correct |
543 ms |
35312 KB |
Output is correct |