#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define pii pair <int, int>
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define F first
#define S second
const int maxn = 2e5 + 5;
vector <int> adj[maxn], vec[maxn];
int sz[maxn], par[maxn], c[maxn];
int ans;
bool cent[maxn], mark[maxn], com[maxn], col[maxn];
vector <int> ver;
void dfs(int v, int p = -1){
sz[v] = 1;
com[v] = true;
ver.pb(v);
for(int u : adj[v]){
if(!cent[u] && u != p){
dfs(u, v);
sz[v] += sz[u];
}
}
}
int find_centroid(int v, int s, int p = -1){
for(int u : adj[v]){
if(!cent[u] && u != p && sz[u] > s / 2){
return find_centroid(u, s, v);
}
}
return v;
}
void dfs2(int v, int p = -1){
for(int u : adj[v]){
if(!cent[u] && u != p){
par[u] = v;
dfs2(u, v);
}
}
}
void solve(int v){
ver.clear();
dfs(v);
int cn = find_centroid(v, sz[v]);
// cout << "cen : " << cn << endl;
par[cn] = -1;
dfs2(cn);
queue <int> q;
vector <int> vc;
q.push(cn);
bool flag = true;
while(q.size() && flag){
int x = q.front();
q.pop();
if(!col[c[x]]){
col[c[x]] = true;
vc.pb(c[x]);
for(int u : vec[c[x]]){
if(!com[u]){
flag = false;
break;
}
if(!mark[u]){
q.push(u);
mark[u] = true;
}
}
}
if(par[x] != -1){
if(!com[par[x]]){
flag = false;
break;
}
if(!mark[par[x]]){
q.push(par[x]);
mark[par[x]] = true;
}
}
}
if(flag)
ans = min(ans, (int)vc.size() - 1);
//clear
for(int u : ver){
col[c[u]] = false;
mark[u] = sz[u] = par[u] = com[u] = 0;
}
cent[cn] = true;
for(int u : adj[cn]){
if(!cent[u]){
solve(u);
}
}
}
int main(){
fast_io;
int n, k;
cin >> n >> k;
for(int i = 0; i < n - 1; i ++){
int x, y;
cin >> x >> y;
x --; y --;
adj[x].pb(y);
adj[y].pb(x);
}
for(int i = 0; i < n; i ++){
cin >> c[i];
vec[c[i]].pb(i);
}
ans = k - 1;
solve(0);
cout << ans;
return 0;
}
Compilation message
capital_city.cpp: In function 'void solve(int)':
capital_city.cpp:95:19: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
95 | mark[u] = sz[u] = par[u] = com[u] = 0;
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9708 KB |
Output is correct |
2 |
Correct |
7 ms |
9708 KB |
Output is correct |
3 |
Correct |
7 ms |
9708 KB |
Output is correct |
4 |
Correct |
8 ms |
9708 KB |
Output is correct |
5 |
Correct |
7 ms |
9708 KB |
Output is correct |
6 |
Correct |
7 ms |
9708 KB |
Output is correct |
7 |
Correct |
7 ms |
9708 KB |
Output is correct |
8 |
Correct |
7 ms |
9708 KB |
Output is correct |
9 |
Correct |
7 ms |
9708 KB |
Output is correct |
10 |
Correct |
6 ms |
9708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9708 KB |
Output is correct |
2 |
Correct |
7 ms |
9708 KB |
Output is correct |
3 |
Correct |
7 ms |
9708 KB |
Output is correct |
4 |
Correct |
8 ms |
9708 KB |
Output is correct |
5 |
Correct |
7 ms |
9708 KB |
Output is correct |
6 |
Correct |
7 ms |
9708 KB |
Output is correct |
7 |
Correct |
7 ms |
9708 KB |
Output is correct |
8 |
Correct |
7 ms |
9708 KB |
Output is correct |
9 |
Correct |
7 ms |
9708 KB |
Output is correct |
10 |
Correct |
6 ms |
9708 KB |
Output is correct |
11 |
Correct |
10 ms |
9964 KB |
Output is correct |
12 |
Correct |
9 ms |
9836 KB |
Output is correct |
13 |
Correct |
9 ms |
9836 KB |
Output is correct |
14 |
Correct |
8 ms |
9836 KB |
Output is correct |
15 |
Correct |
8 ms |
9964 KB |
Output is correct |
16 |
Correct |
9 ms |
9964 KB |
Output is correct |
17 |
Correct |
8 ms |
9964 KB |
Output is correct |
18 |
Correct |
8 ms |
9964 KB |
Output is correct |
19 |
Correct |
8 ms |
9964 KB |
Output is correct |
20 |
Correct |
8 ms |
9964 KB |
Output is correct |
21 |
Correct |
10 ms |
9964 KB |
Output is correct |
22 |
Correct |
9 ms |
10092 KB |
Output is correct |
23 |
Correct |
9 ms |
10112 KB |
Output is correct |
24 |
Correct |
9 ms |
9964 KB |
Output is correct |
25 |
Correct |
9 ms |
9964 KB |
Output is correct |
26 |
Correct |
9 ms |
9964 KB |
Output is correct |
27 |
Correct |
10 ms |
9964 KB |
Output is correct |
28 |
Correct |
9 ms |
9964 KB |
Output is correct |
29 |
Correct |
9 ms |
9964 KB |
Output is correct |
30 |
Correct |
10 ms |
9964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
706 ms |
38264 KB |
Output is correct |
2 |
Correct |
247 ms |
38756 KB |
Output is correct |
3 |
Correct |
750 ms |
37988 KB |
Output is correct |
4 |
Correct |
244 ms |
38756 KB |
Output is correct |
5 |
Correct |
676 ms |
34912 KB |
Output is correct |
6 |
Correct |
246 ms |
38624 KB |
Output is correct |
7 |
Correct |
637 ms |
35424 KB |
Output is correct |
8 |
Correct |
247 ms |
42336 KB |
Output is correct |
9 |
Correct |
821 ms |
37472 KB |
Output is correct |
10 |
Correct |
816 ms |
34912 KB |
Output is correct |
11 |
Correct |
726 ms |
37856 KB |
Output is correct |
12 |
Correct |
753 ms |
40416 KB |
Output is correct |
13 |
Correct |
742 ms |
34528 KB |
Output is correct |
14 |
Correct |
698 ms |
40868 KB |
Output is correct |
15 |
Correct |
692 ms |
40416 KB |
Output is correct |
16 |
Correct |
879 ms |
35424 KB |
Output is correct |
17 |
Correct |
680 ms |
35936 KB |
Output is correct |
18 |
Correct |
795 ms |
36320 KB |
Output is correct |
19 |
Correct |
796 ms |
39424 KB |
Output is correct |
20 |
Correct |
804 ms |
41824 KB |
Output is correct |
21 |
Correct |
7 ms |
9708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9708 KB |
Output is correct |
2 |
Correct |
7 ms |
9708 KB |
Output is correct |
3 |
Correct |
7 ms |
9708 KB |
Output is correct |
4 |
Correct |
8 ms |
9708 KB |
Output is correct |
5 |
Correct |
7 ms |
9708 KB |
Output is correct |
6 |
Correct |
7 ms |
9708 KB |
Output is correct |
7 |
Correct |
7 ms |
9708 KB |
Output is correct |
8 |
Correct |
7 ms |
9708 KB |
Output is correct |
9 |
Correct |
7 ms |
9708 KB |
Output is correct |
10 |
Correct |
6 ms |
9708 KB |
Output is correct |
11 |
Correct |
10 ms |
9964 KB |
Output is correct |
12 |
Correct |
9 ms |
9836 KB |
Output is correct |
13 |
Correct |
9 ms |
9836 KB |
Output is correct |
14 |
Correct |
8 ms |
9836 KB |
Output is correct |
15 |
Correct |
8 ms |
9964 KB |
Output is correct |
16 |
Correct |
9 ms |
9964 KB |
Output is correct |
17 |
Correct |
8 ms |
9964 KB |
Output is correct |
18 |
Correct |
8 ms |
9964 KB |
Output is correct |
19 |
Correct |
8 ms |
9964 KB |
Output is correct |
20 |
Correct |
8 ms |
9964 KB |
Output is correct |
21 |
Correct |
10 ms |
9964 KB |
Output is correct |
22 |
Correct |
9 ms |
10092 KB |
Output is correct |
23 |
Correct |
9 ms |
10112 KB |
Output is correct |
24 |
Correct |
9 ms |
9964 KB |
Output is correct |
25 |
Correct |
9 ms |
9964 KB |
Output is correct |
26 |
Correct |
9 ms |
9964 KB |
Output is correct |
27 |
Correct |
10 ms |
9964 KB |
Output is correct |
28 |
Correct |
9 ms |
9964 KB |
Output is correct |
29 |
Correct |
9 ms |
9964 KB |
Output is correct |
30 |
Correct |
10 ms |
9964 KB |
Output is correct |
31 |
Correct |
706 ms |
38264 KB |
Output is correct |
32 |
Correct |
247 ms |
38756 KB |
Output is correct |
33 |
Correct |
750 ms |
37988 KB |
Output is correct |
34 |
Correct |
244 ms |
38756 KB |
Output is correct |
35 |
Correct |
676 ms |
34912 KB |
Output is correct |
36 |
Correct |
246 ms |
38624 KB |
Output is correct |
37 |
Correct |
637 ms |
35424 KB |
Output is correct |
38 |
Correct |
247 ms |
42336 KB |
Output is correct |
39 |
Correct |
821 ms |
37472 KB |
Output is correct |
40 |
Correct |
816 ms |
34912 KB |
Output is correct |
41 |
Correct |
726 ms |
37856 KB |
Output is correct |
42 |
Correct |
753 ms |
40416 KB |
Output is correct |
43 |
Correct |
742 ms |
34528 KB |
Output is correct |
44 |
Correct |
698 ms |
40868 KB |
Output is correct |
45 |
Correct |
692 ms |
40416 KB |
Output is correct |
46 |
Correct |
879 ms |
35424 KB |
Output is correct |
47 |
Correct |
680 ms |
35936 KB |
Output is correct |
48 |
Correct |
795 ms |
36320 KB |
Output is correct |
49 |
Correct |
796 ms |
39424 KB |
Output is correct |
50 |
Correct |
804 ms |
41824 KB |
Output is correct |
51 |
Correct |
7 ms |
9708 KB |
Output is correct |
52 |
Correct |
525 ms |
25448 KB |
Output is correct |
53 |
Correct |
627 ms |
25440 KB |
Output is correct |
54 |
Correct |
541 ms |
25440 KB |
Output is correct |
55 |
Correct |
577 ms |
25504 KB |
Output is correct |
56 |
Correct |
622 ms |
25440 KB |
Output is correct |
57 |
Correct |
537 ms |
25440 KB |
Output is correct |
58 |
Correct |
589 ms |
29028 KB |
Output is correct |
59 |
Correct |
508 ms |
29284 KB |
Output is correct |
60 |
Correct |
678 ms |
28512 KB |
Output is correct |
61 |
Correct |
712 ms |
28128 KB |
Output is correct |
62 |
Correct |
244 ms |
42340 KB |
Output is correct |
63 |
Correct |
241 ms |
42340 KB |
Output is correct |
64 |
Correct |
239 ms |
42336 KB |
Output is correct |
65 |
Correct |
265 ms |
42336 KB |
Output is correct |
66 |
Correct |
453 ms |
34300 KB |
Output is correct |
67 |
Correct |
402 ms |
33952 KB |
Output is correct |
68 |
Correct |
465 ms |
34152 KB |
Output is correct |
69 |
Correct |
409 ms |
34080 KB |
Output is correct |
70 |
Correct |
402 ms |
34084 KB |
Output is correct |
71 |
Correct |
408 ms |
33952 KB |
Output is correct |
72 |
Correct |
397 ms |
33952 KB |
Output is correct |
73 |
Correct |
419 ms |
33184 KB |
Output is correct |
74 |
Correct |
422 ms |
34212 KB |
Output is correct |
75 |
Correct |
410 ms |
34080 KB |
Output is correct |
76 |
Correct |
652 ms |
32612 KB |
Output is correct |
77 |
Correct |
687 ms |
30852 KB |
Output is correct |
78 |
Correct |
759 ms |
36072 KB |
Output is correct |
79 |
Correct |
747 ms |
33888 KB |
Output is correct |
80 |
Correct |
765 ms |
41264 KB |
Output is correct |
81 |
Correct |
781 ms |
37732 KB |
Output is correct |
82 |
Correct |
750 ms |
37732 KB |
Output is correct |
83 |
Correct |
756 ms |
34452 KB |
Output is correct |
84 |
Correct |
739 ms |
40252 KB |
Output is correct |
85 |
Correct |
736 ms |
38824 KB |
Output is correct |
86 |
Correct |
848 ms |
34040 KB |
Output is correct |
87 |
Correct |
779 ms |
35684 KB |
Output is correct |
88 |
Correct |
725 ms |
36808 KB |
Output is correct |
89 |
Correct |
625 ms |
32740 KB |
Output is correct |
90 |
Correct |
693 ms |
32512 KB |
Output is correct |
91 |
Correct |
620 ms |
35060 KB |
Output is correct |
92 |
Correct |
650 ms |
33512 KB |
Output is correct |
93 |
Correct |
650 ms |
33256 KB |
Output is correct |
94 |
Correct |
655 ms |
32608 KB |
Output is correct |
95 |
Correct |
617 ms |
34144 KB |
Output is correct |
96 |
Correct |
644 ms |
32772 KB |
Output is correct |
97 |
Correct |
643 ms |
34824 KB |
Output is correct |