#include<bits/stdc++.h>
using namespace std;
const int maxn=200000+10,K=200000+10;
vector<int>adj[maxn],allc[K];
int n,k,val[maxn],sz[maxn],part[maxn],visc[K],vis[maxn],now[maxn],rishe,res;
void calsz(int u,int par=0){
now[u]=rishe;
part[u]=par;
visc[val[u]]=0;
sz[u]=1;
for(auto x:adj[u]){
if(vis[x]==0&&x!=par){
calsz(x,u);
sz[u]+=sz[x];
}
}
}
int findcen(int u,int su,int par=0){
for(auto x:adj[u]){
if(vis[x]==0&&x!=par&&sz[x]*2>su){
return findcen(x,su,u);
}
}
return u;
}
void solve(int u){
if(now[u]==-rishe){
return ;
}
if(now[u]!=rishe){
res=n+2;
return ;
}
if(visc[val[u]]==0){
res++;
visc[val[u]]=1;
for(auto x:allc[val[u]]){
solve(x);
if(res>n){
return ;
}
}
}
now[u]=-rishe;
solve(part[u]);
}
int cent(int u){
int ret=n+1;
calsz(u);
int v=findcen(u,sz[u]);
rishe=v;
calsz(v,v);
res=0;
solve(v);
ret=min(ret,res);
vis[v]=1;
for(auto x:adj[v]){
if(vis[x]==0){
ret=min(ret,cent(x));
}
}
return ret;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>k;
for(int i=0;i<n-1;i++){
int u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for(int i=1;i<=n;i++){
cin>>val[i];
allc[val[i]].push_back(i);
}
cout<<(cent(1)-1)<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14168 KB |
Output is correct |
2 |
Correct |
4 ms |
14168 KB |
Output is correct |
3 |
Correct |
3 ms |
14364 KB |
Output is correct |
4 |
Correct |
3 ms |
14172 KB |
Output is correct |
5 |
Correct |
3 ms |
14264 KB |
Output is correct |
6 |
Correct |
3 ms |
14172 KB |
Output is correct |
7 |
Correct |
3 ms |
14168 KB |
Output is correct |
8 |
Correct |
3 ms |
14172 KB |
Output is correct |
9 |
Correct |
3 ms |
14172 KB |
Output is correct |
10 |
Correct |
3 ms |
14172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14168 KB |
Output is correct |
2 |
Correct |
4 ms |
14168 KB |
Output is correct |
3 |
Correct |
3 ms |
14364 KB |
Output is correct |
4 |
Correct |
3 ms |
14172 KB |
Output is correct |
5 |
Correct |
3 ms |
14264 KB |
Output is correct |
6 |
Correct |
3 ms |
14172 KB |
Output is correct |
7 |
Correct |
3 ms |
14168 KB |
Output is correct |
8 |
Correct |
3 ms |
14172 KB |
Output is correct |
9 |
Correct |
3 ms |
14172 KB |
Output is correct |
10 |
Correct |
3 ms |
14172 KB |
Output is correct |
11 |
Correct |
4 ms |
14428 KB |
Output is correct |
12 |
Correct |
4 ms |
14428 KB |
Output is correct |
13 |
Correct |
5 ms |
14428 KB |
Output is correct |
14 |
Correct |
4 ms |
14428 KB |
Output is correct |
15 |
Correct |
4 ms |
14428 KB |
Output is correct |
16 |
Correct |
5 ms |
14604 KB |
Output is correct |
17 |
Correct |
4 ms |
14428 KB |
Output is correct |
18 |
Correct |
5 ms |
14428 KB |
Output is correct |
19 |
Correct |
4 ms |
14424 KB |
Output is correct |
20 |
Correct |
4 ms |
14428 KB |
Output is correct |
21 |
Correct |
4 ms |
14428 KB |
Output is correct |
22 |
Correct |
4 ms |
14424 KB |
Output is correct |
23 |
Correct |
4 ms |
14376 KB |
Output is correct |
24 |
Correct |
4 ms |
14428 KB |
Output is correct |
25 |
Correct |
4 ms |
14348 KB |
Output is correct |
26 |
Correct |
5 ms |
14424 KB |
Output is correct |
27 |
Correct |
4 ms |
14428 KB |
Output is correct |
28 |
Correct |
4 ms |
14364 KB |
Output is correct |
29 |
Correct |
6 ms |
14428 KB |
Output is correct |
30 |
Correct |
4 ms |
14428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
294 ms |
39504 KB |
Output is correct |
2 |
Correct |
132 ms |
39956 KB |
Output is correct |
3 |
Correct |
309 ms |
39452 KB |
Output is correct |
4 |
Correct |
138 ms |
40016 KB |
Output is correct |
5 |
Correct |
317 ms |
36944 KB |
Output is correct |
6 |
Correct |
144 ms |
40096 KB |
Output is correct |
7 |
Correct |
358 ms |
37300 KB |
Output is correct |
8 |
Correct |
140 ms |
39764 KB |
Output is correct |
9 |
Correct |
415 ms |
35668 KB |
Output is correct |
10 |
Correct |
364 ms |
33568 KB |
Output is correct |
11 |
Correct |
335 ms |
36180 KB |
Output is correct |
12 |
Correct |
373 ms |
37972 KB |
Output is correct |
13 |
Correct |
345 ms |
33164 KB |
Output is correct |
14 |
Correct |
376 ms |
38268 KB |
Output is correct |
15 |
Correct |
343 ms |
37968 KB |
Output is correct |
16 |
Correct |
387 ms |
34384 KB |
Output is correct |
17 |
Correct |
352 ms |
34716 KB |
Output is correct |
18 |
Correct |
336 ms |
35104 KB |
Output is correct |
19 |
Correct |
421 ms |
37272 KB |
Output is correct |
20 |
Correct |
380 ms |
39096 KB |
Output is correct |
21 |
Correct |
3 ms |
14168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14168 KB |
Output is correct |
2 |
Correct |
4 ms |
14168 KB |
Output is correct |
3 |
Correct |
3 ms |
14364 KB |
Output is correct |
4 |
Correct |
3 ms |
14172 KB |
Output is correct |
5 |
Correct |
3 ms |
14264 KB |
Output is correct |
6 |
Correct |
3 ms |
14172 KB |
Output is correct |
7 |
Correct |
3 ms |
14168 KB |
Output is correct |
8 |
Correct |
3 ms |
14172 KB |
Output is correct |
9 |
Correct |
3 ms |
14172 KB |
Output is correct |
10 |
Correct |
3 ms |
14172 KB |
Output is correct |
11 |
Correct |
4 ms |
14428 KB |
Output is correct |
12 |
Correct |
4 ms |
14428 KB |
Output is correct |
13 |
Correct |
5 ms |
14428 KB |
Output is correct |
14 |
Correct |
4 ms |
14428 KB |
Output is correct |
15 |
Correct |
4 ms |
14428 KB |
Output is correct |
16 |
Correct |
5 ms |
14604 KB |
Output is correct |
17 |
Correct |
4 ms |
14428 KB |
Output is correct |
18 |
Correct |
5 ms |
14428 KB |
Output is correct |
19 |
Correct |
4 ms |
14424 KB |
Output is correct |
20 |
Correct |
4 ms |
14428 KB |
Output is correct |
21 |
Correct |
4 ms |
14428 KB |
Output is correct |
22 |
Correct |
4 ms |
14424 KB |
Output is correct |
23 |
Correct |
4 ms |
14376 KB |
Output is correct |
24 |
Correct |
4 ms |
14428 KB |
Output is correct |
25 |
Correct |
4 ms |
14348 KB |
Output is correct |
26 |
Correct |
5 ms |
14424 KB |
Output is correct |
27 |
Correct |
4 ms |
14428 KB |
Output is correct |
28 |
Correct |
4 ms |
14364 KB |
Output is correct |
29 |
Correct |
6 ms |
14428 KB |
Output is correct |
30 |
Correct |
4 ms |
14428 KB |
Output is correct |
31 |
Correct |
294 ms |
39504 KB |
Output is correct |
32 |
Correct |
132 ms |
39956 KB |
Output is correct |
33 |
Correct |
309 ms |
39452 KB |
Output is correct |
34 |
Correct |
138 ms |
40016 KB |
Output is correct |
35 |
Correct |
317 ms |
36944 KB |
Output is correct |
36 |
Correct |
144 ms |
40096 KB |
Output is correct |
37 |
Correct |
358 ms |
37300 KB |
Output is correct |
38 |
Correct |
140 ms |
39764 KB |
Output is correct |
39 |
Correct |
415 ms |
35668 KB |
Output is correct |
40 |
Correct |
364 ms |
33568 KB |
Output is correct |
41 |
Correct |
335 ms |
36180 KB |
Output is correct |
42 |
Correct |
373 ms |
37972 KB |
Output is correct |
43 |
Correct |
345 ms |
33164 KB |
Output is correct |
44 |
Correct |
376 ms |
38268 KB |
Output is correct |
45 |
Correct |
343 ms |
37968 KB |
Output is correct |
46 |
Correct |
387 ms |
34384 KB |
Output is correct |
47 |
Correct |
352 ms |
34716 KB |
Output is correct |
48 |
Correct |
336 ms |
35104 KB |
Output is correct |
49 |
Correct |
421 ms |
37272 KB |
Output is correct |
50 |
Correct |
380 ms |
39096 KB |
Output is correct |
51 |
Correct |
3 ms |
14168 KB |
Output is correct |
52 |
Correct |
217 ms |
27220 KB |
Output is correct |
53 |
Correct |
247 ms |
26968 KB |
Output is correct |
54 |
Correct |
272 ms |
27204 KB |
Output is correct |
55 |
Correct |
223 ms |
27216 KB |
Output is correct |
56 |
Correct |
236 ms |
27220 KB |
Output is correct |
57 |
Correct |
214 ms |
27208 KB |
Output is correct |
58 |
Correct |
307 ms |
29012 KB |
Output is correct |
59 |
Correct |
257 ms |
29268 KB |
Output is correct |
60 |
Correct |
363 ms |
28748 KB |
Output is correct |
61 |
Correct |
403 ms |
28664 KB |
Output is correct |
62 |
Correct |
144 ms |
40112 KB |
Output is correct |
63 |
Correct |
133 ms |
39912 KB |
Output is correct |
64 |
Correct |
145 ms |
39704 KB |
Output is correct |
65 |
Correct |
148 ms |
40080 KB |
Output is correct |
66 |
Correct |
257 ms |
33156 KB |
Output is correct |
67 |
Correct |
271 ms |
33100 KB |
Output is correct |
68 |
Correct |
247 ms |
33204 KB |
Output is correct |
69 |
Correct |
241 ms |
33208 KB |
Output is correct |
70 |
Correct |
266 ms |
33068 KB |
Output is correct |
71 |
Correct |
268 ms |
33124 KB |
Output is correct |
72 |
Correct |
206 ms |
32972 KB |
Output is correct |
73 |
Correct |
253 ms |
32548 KB |
Output is correct |
74 |
Correct |
208 ms |
33168 KB |
Output is correct |
75 |
Correct |
272 ms |
33172 KB |
Output is correct |
76 |
Correct |
321 ms |
32080 KB |
Output is correct |
77 |
Correct |
325 ms |
30644 KB |
Output is correct |
78 |
Correct |
433 ms |
34560 KB |
Output is correct |
79 |
Correct |
454 ms |
32904 KB |
Output is correct |
80 |
Correct |
429 ms |
38772 KB |
Output is correct |
81 |
Correct |
459 ms |
35792 KB |
Output is correct |
82 |
Correct |
451 ms |
35720 KB |
Output is correct |
83 |
Correct |
411 ms |
33524 KB |
Output is correct |
84 |
Correct |
473 ms |
38032 KB |
Output is correct |
85 |
Correct |
416 ms |
36588 KB |
Output is correct |
86 |
Correct |
467 ms |
32872 KB |
Output is correct |
87 |
Correct |
480 ms |
34292 KB |
Output is correct |
88 |
Correct |
365 ms |
35152 KB |
Output is correct |
89 |
Correct |
296 ms |
32032 KB |
Output is correct |
90 |
Correct |
303 ms |
31948 KB |
Output is correct |
91 |
Correct |
289 ms |
33948 KB |
Output is correct |
92 |
Correct |
361 ms |
32856 KB |
Output is correct |
93 |
Correct |
298 ms |
32340 KB |
Output is correct |
94 |
Correct |
326 ms |
32068 KB |
Output is correct |
95 |
Correct |
355 ms |
33048 KB |
Output is correct |
96 |
Correct |
394 ms |
32332 KB |
Output is correct |
97 |
Correct |
309 ms |
33856 KB |
Output is correct |