#include<bits/stdc++.h>
using namespace std;
const int N = 2e5;
const int inf = 2e9;
vector <int> e[N];
int v[N];
int sub[N];
vector <int> pos[N];
bool bad[N];
bool tree[N];
bool vis[N];
bool vis2[N];
int ans = inf;
int pr[N];
void cendecomp(int node){
int sz = 0;
int cen = -1,mn = inf;
auto dfs = [&](auto self, int node, int p) -> void{
for(auto i:e[node]){
if(i == p || bad[i])continue;
self(self, i, node);
}
sz++;
};
auto dfs2 = [&](auto self, int node, int p) -> void{
int mx = -1;
sub[node] = 1;
for(auto i:e[node]){
if(i == p || bad[i])continue;
self(self, i, node);
sub[node]+=sub[i];
mx = max(mx,sub[i]);
}
mx = max(mx,sz - sub[node]);
if(mx < mn){
mn = mx;
cen = node;
}
};
auto dfs3 = [&](auto self, int node, int p) -> void{
vis[node] = 0;
tree[node] = 0;
vis2[v[node]] = 0;
for(auto i:e[node]){
if(i == p || bad[i])continue;
self(self, i, node);
}
};
auto dfs4 = [&](auto self, int node, int p) -> void{
pr[node] = p;
tree[node] = 1;
for(auto i:e[node]){
if(i == p || bad[i])continue;
self(self, i, node);
}
};
dfs(dfs, node, -1);
dfs2(dfs2, node, -1);
dfs4(dfs4, cen, -1);
queue <int> q;
q.push(cen);
vis[cen] = 1;
bool ok = 1;
int nr = 0;
while(!q.empty()){
int x = q.front();
//cout<<"investigate:"<<x<<'\n';
q.pop();
if(!vis2[v[x]]){
vis2[v[x]] = 1;
nr++;
for(auto i:pos[v[x]]){
if(!tree[i]){
ok = 0;
break;
}
if(tree[i] && !vis[i]){
vis[i] = 1;
q.push(i);
}
}
if(!ok)break;
}
if(pr[x] != -1 && !vis[pr[x]]){
vis[pr[x]] = 1;
q.push(pr[x]);
}
}
if(ok){
ans = min(ans,nr);
}
dfs3(dfs3, cen, -1);
//cout<<cen<<' '<<nr<<' '<<ok<<'\n';
bad[cen] = 1;
for(auto i:e[cen]){
if(!bad[i]){
cendecomp(i);
}
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n,k;
cin>>n>>k;
for(int i = 0;i < n - 1;i++){
int u,w;
cin>>u>>w;
u--;w--;
e[u].push_back(w);
e[w].push_back(u);
}
for(int i = 0;i < n;i++){
cin>>v[i];
v[i]--;
pos[v[i]].push_back(i);
}
cendecomp(0);
cout<<ans - 1<<'\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12820 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
2 ms |
12816 KB |
Output is correct |
7 |
Correct |
3 ms |
12636 KB |
Output is correct |
8 |
Correct |
3 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12820 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
2 ms |
12816 KB |
Output is correct |
7 |
Correct |
3 ms |
12636 KB |
Output is correct |
8 |
Correct |
3 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
5 ms |
12832 KB |
Output is correct |
12 |
Correct |
3 ms |
12892 KB |
Output is correct |
13 |
Correct |
3 ms |
12892 KB |
Output is correct |
14 |
Correct |
5 ms |
12892 KB |
Output is correct |
15 |
Correct |
5 ms |
12972 KB |
Output is correct |
16 |
Correct |
4 ms |
12888 KB |
Output is correct |
17 |
Correct |
5 ms |
12892 KB |
Output is correct |
18 |
Correct |
5 ms |
12892 KB |
Output is correct |
19 |
Correct |
3 ms |
12892 KB |
Output is correct |
20 |
Correct |
3 ms |
12972 KB |
Output is correct |
21 |
Correct |
3 ms |
12892 KB |
Output is correct |
22 |
Correct |
3 ms |
13040 KB |
Output is correct |
23 |
Correct |
3 ms |
12892 KB |
Output is correct |
24 |
Correct |
3 ms |
12828 KB |
Output is correct |
25 |
Correct |
4 ms |
12888 KB |
Output is correct |
26 |
Correct |
4 ms |
12892 KB |
Output is correct |
27 |
Correct |
3 ms |
12892 KB |
Output is correct |
28 |
Correct |
4 ms |
12892 KB |
Output is correct |
29 |
Correct |
3 ms |
12892 KB |
Output is correct |
30 |
Correct |
3 ms |
12892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
289 ms |
34900 KB |
Output is correct |
2 |
Correct |
149 ms |
35328 KB |
Output is correct |
3 |
Correct |
279 ms |
34896 KB |
Output is correct |
4 |
Correct |
146 ms |
35396 KB |
Output is correct |
5 |
Correct |
274 ms |
32848 KB |
Output is correct |
6 |
Correct |
149 ms |
35336 KB |
Output is correct |
7 |
Correct |
264 ms |
33104 KB |
Output is correct |
8 |
Correct |
143 ms |
35280 KB |
Output is correct |
9 |
Correct |
341 ms |
31700 KB |
Output is correct |
10 |
Correct |
347 ms |
30444 KB |
Output is correct |
11 |
Correct |
336 ms |
32232 KB |
Output is correct |
12 |
Correct |
338 ms |
33620 KB |
Output is correct |
13 |
Correct |
337 ms |
30200 KB |
Output is correct |
14 |
Correct |
312 ms |
33872 KB |
Output is correct |
15 |
Correct |
320 ms |
33628 KB |
Output is correct |
16 |
Correct |
345 ms |
30656 KB |
Output is correct |
17 |
Correct |
353 ms |
30992 KB |
Output is correct |
18 |
Correct |
326 ms |
31104 KB |
Output is correct |
19 |
Correct |
320 ms |
33104 KB |
Output is correct |
20 |
Correct |
336 ms |
34384 KB |
Output is correct |
21 |
Correct |
2 ms |
12636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12820 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
2 ms |
12816 KB |
Output is correct |
7 |
Correct |
3 ms |
12636 KB |
Output is correct |
8 |
Correct |
3 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
5 ms |
12832 KB |
Output is correct |
12 |
Correct |
3 ms |
12892 KB |
Output is correct |
13 |
Correct |
3 ms |
12892 KB |
Output is correct |
14 |
Correct |
5 ms |
12892 KB |
Output is correct |
15 |
Correct |
5 ms |
12972 KB |
Output is correct |
16 |
Correct |
4 ms |
12888 KB |
Output is correct |
17 |
Correct |
5 ms |
12892 KB |
Output is correct |
18 |
Correct |
5 ms |
12892 KB |
Output is correct |
19 |
Correct |
3 ms |
12892 KB |
Output is correct |
20 |
Correct |
3 ms |
12972 KB |
Output is correct |
21 |
Correct |
3 ms |
12892 KB |
Output is correct |
22 |
Correct |
3 ms |
13040 KB |
Output is correct |
23 |
Correct |
3 ms |
12892 KB |
Output is correct |
24 |
Correct |
3 ms |
12828 KB |
Output is correct |
25 |
Correct |
4 ms |
12888 KB |
Output is correct |
26 |
Correct |
4 ms |
12892 KB |
Output is correct |
27 |
Correct |
3 ms |
12892 KB |
Output is correct |
28 |
Correct |
4 ms |
12892 KB |
Output is correct |
29 |
Correct |
3 ms |
12892 KB |
Output is correct |
30 |
Correct |
3 ms |
12892 KB |
Output is correct |
31 |
Correct |
289 ms |
34900 KB |
Output is correct |
32 |
Correct |
149 ms |
35328 KB |
Output is correct |
33 |
Correct |
279 ms |
34896 KB |
Output is correct |
34 |
Correct |
146 ms |
35396 KB |
Output is correct |
35 |
Correct |
274 ms |
32848 KB |
Output is correct |
36 |
Correct |
149 ms |
35336 KB |
Output is correct |
37 |
Correct |
264 ms |
33104 KB |
Output is correct |
38 |
Correct |
143 ms |
35280 KB |
Output is correct |
39 |
Correct |
341 ms |
31700 KB |
Output is correct |
40 |
Correct |
347 ms |
30444 KB |
Output is correct |
41 |
Correct |
336 ms |
32232 KB |
Output is correct |
42 |
Correct |
338 ms |
33620 KB |
Output is correct |
43 |
Correct |
337 ms |
30200 KB |
Output is correct |
44 |
Correct |
312 ms |
33872 KB |
Output is correct |
45 |
Correct |
320 ms |
33628 KB |
Output is correct |
46 |
Correct |
345 ms |
30656 KB |
Output is correct |
47 |
Correct |
353 ms |
30992 KB |
Output is correct |
48 |
Correct |
326 ms |
31104 KB |
Output is correct |
49 |
Correct |
320 ms |
33104 KB |
Output is correct |
50 |
Correct |
336 ms |
34384 KB |
Output is correct |
51 |
Correct |
2 ms |
12636 KB |
Output is correct |
52 |
Correct |
223 ms |
24604 KB |
Output is correct |
53 |
Correct |
242 ms |
24656 KB |
Output is correct |
54 |
Correct |
242 ms |
24784 KB |
Output is correct |
55 |
Correct |
241 ms |
24660 KB |
Output is correct |
56 |
Correct |
238 ms |
24656 KB |
Output is correct |
57 |
Correct |
244 ms |
24660 KB |
Output is correct |
58 |
Correct |
203 ms |
26964 KB |
Output is correct |
59 |
Correct |
209 ms |
27220 KB |
Output is correct |
60 |
Correct |
257 ms |
26964 KB |
Output is correct |
61 |
Correct |
247 ms |
26648 KB |
Output is correct |
62 |
Correct |
142 ms |
35412 KB |
Output is correct |
63 |
Correct |
141 ms |
35360 KB |
Output is correct |
64 |
Correct |
147 ms |
35244 KB |
Output is correct |
65 |
Correct |
148 ms |
35408 KB |
Output is correct |
66 |
Correct |
167 ms |
30368 KB |
Output is correct |
67 |
Correct |
172 ms |
30268 KB |
Output is correct |
68 |
Correct |
172 ms |
30408 KB |
Output is correct |
69 |
Correct |
184 ms |
30412 KB |
Output is correct |
70 |
Correct |
191 ms |
30152 KB |
Output is correct |
71 |
Correct |
177 ms |
30156 KB |
Output is correct |
72 |
Correct |
169 ms |
30216 KB |
Output is correct |
73 |
Correct |
171 ms |
29644 KB |
Output is correct |
74 |
Correct |
177 ms |
30408 KB |
Output is correct |
75 |
Correct |
178 ms |
30408 KB |
Output is correct |
76 |
Correct |
239 ms |
29408 KB |
Output is correct |
77 |
Correct |
269 ms |
28236 KB |
Output is correct |
78 |
Correct |
339 ms |
30948 KB |
Output is correct |
79 |
Correct |
346 ms |
29856 KB |
Output is correct |
80 |
Correct |
338 ms |
33996 KB |
Output is correct |
81 |
Correct |
336 ms |
32024 KB |
Output is correct |
82 |
Correct |
332 ms |
32032 KB |
Output is correct |
83 |
Correct |
331 ms |
29928 KB |
Output is correct |
84 |
Correct |
340 ms |
33496 KB |
Output is correct |
85 |
Correct |
328 ms |
32568 KB |
Output is correct |
86 |
Correct |
342 ms |
29776 KB |
Output is correct |
87 |
Correct |
326 ms |
30800 KB |
Output is correct |
88 |
Correct |
295 ms |
31316 KB |
Output is correct |
89 |
Correct |
280 ms |
29180 KB |
Output is correct |
90 |
Correct |
300 ms |
29012 KB |
Output is correct |
91 |
Correct |
324 ms |
30548 KB |
Output is correct |
92 |
Correct |
307 ms |
29780 KB |
Output is correct |
93 |
Correct |
313 ms |
29364 KB |
Output is correct |
94 |
Correct |
308 ms |
29012 KB |
Output is correct |
95 |
Correct |
310 ms |
29892 KB |
Output is correct |
96 |
Correct |
306 ms |
29008 KB |
Output is correct |
97 |
Correct |
328 ms |
30292 KB |
Output is correct |