#include<bits/stdc++.h>
using namespace std;
const int maxn=200000+10;
vector<int>adj[maxn],allc[maxn];
int n,k,val[maxn],sz[maxn],part[maxn],visc[maxn],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 |
3 ms |
14172 KB |
Output is correct |
3 |
Correct |
3 ms |
14172 KB |
Output is correct |
4 |
Correct |
3 ms |
14168 KB |
Output is correct |
5 |
Correct |
3 ms |
14368 KB |
Output is correct |
6 |
Correct |
4 ms |
14172 KB |
Output is correct |
7 |
Correct |
4 ms |
14172 KB |
Output is correct |
8 |
Correct |
3 ms |
14172 KB |
Output is correct |
9 |
Correct |
4 ms |
14172 KB |
Output is correct |
10 |
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 |
3 ms |
14172 KB |
Output is correct |
3 |
Correct |
3 ms |
14172 KB |
Output is correct |
4 |
Correct |
3 ms |
14168 KB |
Output is correct |
5 |
Correct |
3 ms |
14368 KB |
Output is correct |
6 |
Correct |
4 ms |
14172 KB |
Output is correct |
7 |
Correct |
4 ms |
14172 KB |
Output is correct |
8 |
Correct |
3 ms |
14172 KB |
Output is correct |
9 |
Correct |
4 ms |
14172 KB |
Output is correct |
10 |
Correct |
3 ms |
14168 KB |
Output is correct |
11 |
Correct |
4 ms |
14428 KB |
Output is correct |
12 |
Correct |
5 ms |
14428 KB |
Output is correct |
13 |
Correct |
4 ms |
14424 KB |
Output is correct |
14 |
Correct |
4 ms |
14428 KB |
Output is correct |
15 |
Correct |
5 ms |
14428 KB |
Output is correct |
16 |
Correct |
5 ms |
14428 KB |
Output is correct |
17 |
Correct |
5 ms |
14324 KB |
Output is correct |
18 |
Correct |
5 ms |
14428 KB |
Output is correct |
19 |
Correct |
5 ms |
14428 KB |
Output is correct |
20 |
Correct |
3 ms |
14428 KB |
Output is correct |
21 |
Correct |
4 ms |
14396 KB |
Output is correct |
22 |
Correct |
4 ms |
14428 KB |
Output is correct |
23 |
Correct |
4 ms |
14428 KB |
Output is correct |
24 |
Correct |
4 ms |
14428 KB |
Output is correct |
25 |
Correct |
6 ms |
14428 KB |
Output is correct |
26 |
Correct |
4 ms |
14428 KB |
Output is correct |
27 |
Correct |
4 ms |
14680 KB |
Output is correct |
28 |
Correct |
4 ms |
14428 KB |
Output is correct |
29 |
Correct |
7 ms |
14428 KB |
Output is correct |
30 |
Correct |
4 ms |
14428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
268 ms |
35928 KB |
Output is correct |
2 |
Correct |
141 ms |
36432 KB |
Output is correct |
3 |
Correct |
374 ms |
35780 KB |
Output is correct |
4 |
Correct |
132 ms |
36292 KB |
Output is correct |
5 |
Correct |
359 ms |
33292 KB |
Output is correct |
6 |
Correct |
136 ms |
36408 KB |
Output is correct |
7 |
Correct |
319 ms |
33364 KB |
Output is correct |
8 |
Correct |
153 ms |
36040 KB |
Output is correct |
9 |
Correct |
412 ms |
32084 KB |
Output is correct |
10 |
Correct |
406 ms |
30060 KB |
Output is correct |
11 |
Correct |
423 ms |
32520 KB |
Output is correct |
12 |
Correct |
475 ms |
34516 KB |
Output is correct |
13 |
Correct |
554 ms |
29708 KB |
Output is correct |
14 |
Correct |
448 ms |
34752 KB |
Output is correct |
15 |
Correct |
574 ms |
34548 KB |
Output is correct |
16 |
Correct |
488 ms |
30564 KB |
Output is correct |
17 |
Correct |
433 ms |
31044 KB |
Output is correct |
18 |
Correct |
498 ms |
31092 KB |
Output is correct |
19 |
Correct |
395 ms |
33504 KB |
Output is correct |
20 |
Correct |
617 ms |
35508 KB |
Output is correct |
21 |
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 |
3 ms |
14172 KB |
Output is correct |
3 |
Correct |
3 ms |
14172 KB |
Output is correct |
4 |
Correct |
3 ms |
14168 KB |
Output is correct |
5 |
Correct |
3 ms |
14368 KB |
Output is correct |
6 |
Correct |
4 ms |
14172 KB |
Output is correct |
7 |
Correct |
4 ms |
14172 KB |
Output is correct |
8 |
Correct |
3 ms |
14172 KB |
Output is correct |
9 |
Correct |
4 ms |
14172 KB |
Output is correct |
10 |
Correct |
3 ms |
14168 KB |
Output is correct |
11 |
Correct |
4 ms |
14428 KB |
Output is correct |
12 |
Correct |
5 ms |
14428 KB |
Output is correct |
13 |
Correct |
4 ms |
14424 KB |
Output is correct |
14 |
Correct |
4 ms |
14428 KB |
Output is correct |
15 |
Correct |
5 ms |
14428 KB |
Output is correct |
16 |
Correct |
5 ms |
14428 KB |
Output is correct |
17 |
Correct |
5 ms |
14324 KB |
Output is correct |
18 |
Correct |
5 ms |
14428 KB |
Output is correct |
19 |
Correct |
5 ms |
14428 KB |
Output is correct |
20 |
Correct |
3 ms |
14428 KB |
Output is correct |
21 |
Correct |
4 ms |
14396 KB |
Output is correct |
22 |
Correct |
4 ms |
14428 KB |
Output is correct |
23 |
Correct |
4 ms |
14428 KB |
Output is correct |
24 |
Correct |
4 ms |
14428 KB |
Output is correct |
25 |
Correct |
6 ms |
14428 KB |
Output is correct |
26 |
Correct |
4 ms |
14428 KB |
Output is correct |
27 |
Correct |
4 ms |
14680 KB |
Output is correct |
28 |
Correct |
4 ms |
14428 KB |
Output is correct |
29 |
Correct |
7 ms |
14428 KB |
Output is correct |
30 |
Correct |
4 ms |
14428 KB |
Output is correct |
31 |
Correct |
268 ms |
35928 KB |
Output is correct |
32 |
Correct |
141 ms |
36432 KB |
Output is correct |
33 |
Correct |
374 ms |
35780 KB |
Output is correct |
34 |
Correct |
132 ms |
36292 KB |
Output is correct |
35 |
Correct |
359 ms |
33292 KB |
Output is correct |
36 |
Correct |
136 ms |
36408 KB |
Output is correct |
37 |
Correct |
319 ms |
33364 KB |
Output is correct |
38 |
Correct |
153 ms |
36040 KB |
Output is correct |
39 |
Correct |
412 ms |
32084 KB |
Output is correct |
40 |
Correct |
406 ms |
30060 KB |
Output is correct |
41 |
Correct |
423 ms |
32520 KB |
Output is correct |
42 |
Correct |
475 ms |
34516 KB |
Output is correct |
43 |
Correct |
554 ms |
29708 KB |
Output is correct |
44 |
Correct |
448 ms |
34752 KB |
Output is correct |
45 |
Correct |
574 ms |
34548 KB |
Output is correct |
46 |
Correct |
488 ms |
30564 KB |
Output is correct |
47 |
Correct |
433 ms |
31044 KB |
Output is correct |
48 |
Correct |
498 ms |
31092 KB |
Output is correct |
49 |
Correct |
395 ms |
33504 KB |
Output is correct |
50 |
Correct |
617 ms |
35508 KB |
Output is correct |
51 |
Correct |
3 ms |
14172 KB |
Output is correct |
52 |
Correct |
344 ms |
23744 KB |
Output is correct |
53 |
Correct |
339 ms |
23736 KB |
Output is correct |
54 |
Correct |
400 ms |
23736 KB |
Output is correct |
55 |
Correct |
248 ms |
23592 KB |
Output is correct |
56 |
Correct |
273 ms |
23732 KB |
Output is correct |
57 |
Correct |
326 ms |
23632 KB |
Output is correct |
58 |
Correct |
347 ms |
25476 KB |
Output is correct |
59 |
Correct |
275 ms |
25956 KB |
Output is correct |
60 |
Correct |
331 ms |
25268 KB |
Output is correct |
61 |
Correct |
340 ms |
24984 KB |
Output is correct |
62 |
Correct |
166 ms |
36436 KB |
Output is correct |
63 |
Correct |
146 ms |
36432 KB |
Output is correct |
64 |
Correct |
148 ms |
36264 KB |
Output is correct |
65 |
Correct |
144 ms |
36236 KB |
Output is correct |
66 |
Correct |
177 ms |
29388 KB |
Output is correct |
67 |
Correct |
224 ms |
29420 KB |
Output is correct |
68 |
Correct |
222 ms |
29488 KB |
Output is correct |
69 |
Correct |
194 ms |
29388 KB |
Output is correct |
70 |
Correct |
325 ms |
29384 KB |
Output is correct |
71 |
Correct |
298 ms |
29480 KB |
Output is correct |
72 |
Correct |
190 ms |
29424 KB |
Output is correct |
73 |
Correct |
225 ms |
28772 KB |
Output is correct |
74 |
Correct |
275 ms |
29272 KB |
Output is correct |
75 |
Correct |
285 ms |
28880 KB |
Output is correct |
76 |
Correct |
294 ms |
30288 KB |
Output is correct |
77 |
Correct |
394 ms |
29012 KB |
Output is correct |
78 |
Correct |
406 ms |
32980 KB |
Output is correct |
79 |
Correct |
374 ms |
31308 KB |
Output is correct |
80 |
Correct |
529 ms |
36576 KB |
Output is correct |
81 |
Correct |
374 ms |
34128 KB |
Output is correct |
82 |
Correct |
495 ms |
34320 KB |
Output is correct |
83 |
Correct |
506 ms |
31568 KB |
Output is correct |
84 |
Correct |
469 ms |
36400 KB |
Output is correct |
85 |
Correct |
541 ms |
35192 KB |
Output is correct |
86 |
Correct |
531 ms |
30796 KB |
Output is correct |
87 |
Correct |
609 ms |
32232 KB |
Output is correct |
88 |
Correct |
396 ms |
33636 KB |
Output is correct |
89 |
Correct |
357 ms |
30388 KB |
Output is correct |
90 |
Correct |
404 ms |
30324 KB |
Output is correct |
91 |
Correct |
418 ms |
32332 KB |
Output is correct |
92 |
Correct |
314 ms |
31504 KB |
Output is correct |
93 |
Correct |
483 ms |
30816 KB |
Output is correct |
94 |
Correct |
323 ms |
30388 KB |
Output is correct |
95 |
Correct |
324 ms |
30948 KB |
Output is correct |
96 |
Correct |
378 ms |
30544 KB |
Output is correct |
97 |
Correct |
397 ms |
31528 KB |
Output is correct |