#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
const int mxn=2e5+5;
const int inf=1e9;
vector<int> adj[mxn];
bool visited[mxn];
int sz[mxn];
vector<int> belong[mxn];
bool now[mxn];
int par[mxn];
int c[mxn];
bool did[mxn];
int ans=inf;
vector<int> st;
int dfs(int v,int p=0){
sz[v]=1;
st.push_back(v);
now[v]=true;
for(auto u:adj[v]){
if(visited[u] or u==p) continue;
sz[v]+=dfs(u,v);
}
return sz[v];
}
int find(int v,int val,int p=0){
for(auto u:adj[v]){
if(visited[u] or u==p) continue;
if(sz[u]*2>val) return find(u,val,v);
}
return v;
}
void dfs2(int v,int p=0){
par[v]=p;
for(auto u:adj[v]){
if(visited[u] or u==p) continue;
dfs2(u,v);
}
}
void centroid(int v){
v=find(v,dfs(v));
dfs2(v);
queue<int> q;
vector<int> tmp;
q.push(c[v]);
did[c[v]]=true;
tmp.push_back(c[v]);
bool ok=true;
while(!q.empty()){
int C=q.front();
q.pop();
for(auto x:belong[C]){
if(!now[x]){
ok=false;
break;
}
if(par[x]!=0 and !did[c[par[x]]]){
did[c[par[x]]]=true;
tmp.push_back(c[par[x]]);
q.push(c[par[x]]);
}
}
if(!ok){
break;
}
}
for(auto C:tmp){
did[C]=false;
}
for(auto x:st){
now[x]=false;
}
st.clear();
if(ok){
ans=min(ans,(int)tmp.size()-1);
}
visited[v]=true;
for(auto u:adj[v]){
if(!visited[u]){
centroid(u);
}
}
}
int main() {_
int n,k;
cin>>n>>k;
for(int i=0;i<n-1;i++){
int a,b;
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for(int i=1;i<=n;i++){
cin>>c[i];
belong[c[i]].push_back(i);
}
centroid(1);
assert(ans!=inf);
cout<<ans<<'\n';
return 0;
}
//maybe its multiset not set
//yeeorz
//laborz
Compilation message
capital_city.cpp: In function 'void setIO(std::string)':
capital_city.cpp:15:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
capital_city.cpp:16:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
12632 KB |
Output is correct |
2 |
Correct |
4 ms |
12576 KB |
Output is correct |
3 |
Correct |
4 ms |
12572 KB |
Output is correct |
4 |
Correct |
4 ms |
12380 KB |
Output is correct |
5 |
Correct |
4 ms |
12380 KB |
Output is correct |
6 |
Correct |
4 ms |
12380 KB |
Output is correct |
7 |
Correct |
4 ms |
12380 KB |
Output is correct |
8 |
Correct |
4 ms |
12380 KB |
Output is correct |
9 |
Correct |
4 ms |
12380 KB |
Output is correct |
10 |
Correct |
4 ms |
12608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
12632 KB |
Output is correct |
2 |
Correct |
4 ms |
12576 KB |
Output is correct |
3 |
Correct |
4 ms |
12572 KB |
Output is correct |
4 |
Correct |
4 ms |
12380 KB |
Output is correct |
5 |
Correct |
4 ms |
12380 KB |
Output is correct |
6 |
Correct |
4 ms |
12380 KB |
Output is correct |
7 |
Correct |
4 ms |
12380 KB |
Output is correct |
8 |
Correct |
4 ms |
12380 KB |
Output is correct |
9 |
Correct |
4 ms |
12380 KB |
Output is correct |
10 |
Correct |
4 ms |
12608 KB |
Output is correct |
11 |
Correct |
5 ms |
12632 KB |
Output is correct |
12 |
Correct |
5 ms |
12636 KB |
Output is correct |
13 |
Correct |
5 ms |
12636 KB |
Output is correct |
14 |
Correct |
5 ms |
12636 KB |
Output is correct |
15 |
Correct |
5 ms |
12632 KB |
Output is correct |
16 |
Correct |
5 ms |
12636 KB |
Output is correct |
17 |
Correct |
5 ms |
12636 KB |
Output is correct |
18 |
Correct |
5 ms |
12636 KB |
Output is correct |
19 |
Correct |
5 ms |
12576 KB |
Output is correct |
20 |
Correct |
5 ms |
12636 KB |
Output is correct |
21 |
Correct |
5 ms |
12636 KB |
Output is correct |
22 |
Correct |
5 ms |
12632 KB |
Output is correct |
23 |
Correct |
5 ms |
12632 KB |
Output is correct |
24 |
Correct |
5 ms |
12636 KB |
Output is correct |
25 |
Correct |
5 ms |
12636 KB |
Output is correct |
26 |
Correct |
5 ms |
12632 KB |
Output is correct |
27 |
Correct |
6 ms |
12636 KB |
Output is correct |
28 |
Correct |
5 ms |
12676 KB |
Output is correct |
29 |
Correct |
5 ms |
12636 KB |
Output is correct |
30 |
Correct |
5 ms |
12636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
266 ms |
34912 KB |
Output is correct |
2 |
Correct |
145 ms |
35732 KB |
Output is correct |
3 |
Correct |
333 ms |
34764 KB |
Output is correct |
4 |
Correct |
146 ms |
35480 KB |
Output is correct |
5 |
Correct |
269 ms |
32196 KB |
Output is correct |
6 |
Correct |
155 ms |
35304 KB |
Output is correct |
7 |
Correct |
266 ms |
32540 KB |
Output is correct |
8 |
Correct |
138 ms |
35016 KB |
Output is correct |
9 |
Correct |
323 ms |
31172 KB |
Output is correct |
10 |
Correct |
321 ms |
29132 KB |
Output is correct |
11 |
Correct |
421 ms |
31376 KB |
Output is correct |
12 |
Correct |
323 ms |
33984 KB |
Output is correct |
13 |
Correct |
339 ms |
28872 KB |
Output is correct |
14 |
Correct |
325 ms |
33848 KB |
Output is correct |
15 |
Correct |
384 ms |
33664 KB |
Output is correct |
16 |
Correct |
365 ms |
29640 KB |
Output is correct |
17 |
Correct |
310 ms |
30144 KB |
Output is correct |
18 |
Correct |
318 ms |
30408 KB |
Output is correct |
19 |
Correct |
353 ms |
32716 KB |
Output is correct |
20 |
Correct |
392 ms |
34576 KB |
Output is correct |
21 |
Correct |
4 ms |
12376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
12632 KB |
Output is correct |
2 |
Correct |
4 ms |
12576 KB |
Output is correct |
3 |
Correct |
4 ms |
12572 KB |
Output is correct |
4 |
Correct |
4 ms |
12380 KB |
Output is correct |
5 |
Correct |
4 ms |
12380 KB |
Output is correct |
6 |
Correct |
4 ms |
12380 KB |
Output is correct |
7 |
Correct |
4 ms |
12380 KB |
Output is correct |
8 |
Correct |
4 ms |
12380 KB |
Output is correct |
9 |
Correct |
4 ms |
12380 KB |
Output is correct |
10 |
Correct |
4 ms |
12608 KB |
Output is correct |
11 |
Correct |
5 ms |
12632 KB |
Output is correct |
12 |
Correct |
5 ms |
12636 KB |
Output is correct |
13 |
Correct |
5 ms |
12636 KB |
Output is correct |
14 |
Correct |
5 ms |
12636 KB |
Output is correct |
15 |
Correct |
5 ms |
12632 KB |
Output is correct |
16 |
Correct |
5 ms |
12636 KB |
Output is correct |
17 |
Correct |
5 ms |
12636 KB |
Output is correct |
18 |
Correct |
5 ms |
12636 KB |
Output is correct |
19 |
Correct |
5 ms |
12576 KB |
Output is correct |
20 |
Correct |
5 ms |
12636 KB |
Output is correct |
21 |
Correct |
5 ms |
12636 KB |
Output is correct |
22 |
Correct |
5 ms |
12632 KB |
Output is correct |
23 |
Correct |
5 ms |
12632 KB |
Output is correct |
24 |
Correct |
5 ms |
12636 KB |
Output is correct |
25 |
Correct |
5 ms |
12636 KB |
Output is correct |
26 |
Correct |
5 ms |
12632 KB |
Output is correct |
27 |
Correct |
6 ms |
12636 KB |
Output is correct |
28 |
Correct |
5 ms |
12676 KB |
Output is correct |
29 |
Correct |
5 ms |
12636 KB |
Output is correct |
30 |
Correct |
5 ms |
12636 KB |
Output is correct |
31 |
Correct |
266 ms |
34912 KB |
Output is correct |
32 |
Correct |
145 ms |
35732 KB |
Output is correct |
33 |
Correct |
333 ms |
34764 KB |
Output is correct |
34 |
Correct |
146 ms |
35480 KB |
Output is correct |
35 |
Correct |
269 ms |
32196 KB |
Output is correct |
36 |
Correct |
155 ms |
35304 KB |
Output is correct |
37 |
Correct |
266 ms |
32540 KB |
Output is correct |
38 |
Correct |
138 ms |
35016 KB |
Output is correct |
39 |
Correct |
323 ms |
31172 KB |
Output is correct |
40 |
Correct |
321 ms |
29132 KB |
Output is correct |
41 |
Correct |
421 ms |
31376 KB |
Output is correct |
42 |
Correct |
323 ms |
33984 KB |
Output is correct |
43 |
Correct |
339 ms |
28872 KB |
Output is correct |
44 |
Correct |
325 ms |
33848 KB |
Output is correct |
45 |
Correct |
384 ms |
33664 KB |
Output is correct |
46 |
Correct |
365 ms |
29640 KB |
Output is correct |
47 |
Correct |
310 ms |
30144 KB |
Output is correct |
48 |
Correct |
318 ms |
30408 KB |
Output is correct |
49 |
Correct |
353 ms |
32716 KB |
Output is correct |
50 |
Correct |
392 ms |
34576 KB |
Output is correct |
51 |
Correct |
4 ms |
12376 KB |
Output is correct |
52 |
Correct |
219 ms |
21716 KB |
Output is correct |
53 |
Correct |
207 ms |
21652 KB |
Output is correct |
54 |
Correct |
237 ms |
21692 KB |
Output is correct |
55 |
Correct |
209 ms |
21760 KB |
Output is correct |
56 |
Correct |
251 ms |
21764 KB |
Output is correct |
57 |
Correct |
214 ms |
21704 KB |
Output is correct |
58 |
Correct |
208 ms |
24520 KB |
Output is correct |
59 |
Correct |
238 ms |
24756 KB |
Output is correct |
60 |
Correct |
257 ms |
24500 KB |
Output is correct |
61 |
Correct |
270 ms |
24064 KB |
Output is correct |
62 |
Correct |
145 ms |
35492 KB |
Output is correct |
63 |
Correct |
146 ms |
35272 KB |
Output is correct |
64 |
Correct |
144 ms |
35268 KB |
Output is correct |
65 |
Correct |
144 ms |
35268 KB |
Output is correct |
66 |
Correct |
188 ms |
28476 KB |
Output is correct |
67 |
Correct |
182 ms |
28296 KB |
Output is correct |
68 |
Correct |
176 ms |
28528 KB |
Output is correct |
69 |
Correct |
237 ms |
28548 KB |
Output is correct |
70 |
Correct |
179 ms |
28628 KB |
Output is correct |
71 |
Correct |
180 ms |
28612 KB |
Output is correct |
72 |
Correct |
181 ms |
28556 KB |
Output is correct |
73 |
Correct |
191 ms |
27896 KB |
Output is correct |
74 |
Correct |
191 ms |
28592 KB |
Output is correct |
75 |
Correct |
185 ms |
28616 KB |
Output is correct |
76 |
Correct |
255 ms |
27596 KB |
Output is correct |
77 |
Correct |
307 ms |
26260 KB |
Output is correct |
78 |
Correct |
319 ms |
30160 KB |
Output is correct |
79 |
Correct |
355 ms |
28496 KB |
Output is correct |
80 |
Correct |
318 ms |
33980 KB |
Output is correct |
81 |
Correct |
343 ms |
31428 KB |
Output is correct |
82 |
Correct |
319 ms |
31420 KB |
Output is correct |
83 |
Correct |
378 ms |
28620 KB |
Output is correct |
84 |
Correct |
345 ms |
33340 KB |
Output is correct |
85 |
Correct |
337 ms |
32132 KB |
Output is correct |
86 |
Correct |
368 ms |
28368 KB |
Output is correct |
87 |
Correct |
345 ms |
29952 KB |
Output is correct |
88 |
Correct |
271 ms |
30668 KB |
Output is correct |
89 |
Correct |
277 ms |
27344 KB |
Output is correct |
90 |
Correct |
272 ms |
27240 KB |
Output is correct |
91 |
Correct |
287 ms |
29212 KB |
Output is correct |
92 |
Correct |
372 ms |
28172 KB |
Output is correct |
93 |
Correct |
300 ms |
27856 KB |
Output is correct |
94 |
Correct |
298 ms |
27280 KB |
Output is correct |
95 |
Correct |
279 ms |
28620 KB |
Output is correct |
96 |
Correct |
282 ms |
27520 KB |
Output is correct |
97 |
Correct |
290 ms |
29080 KB |
Output is correct |