// In the name of God
// Hope is last to die
#include <bits/stdc++.h>
using namespace std;
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2")
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
// #define int long long
#define S second
#define F first
#define mp make_pair
#define smax(x, y) (x) = max((x), (y))
#define smin(x, y) (x) = min((x), (y))
#define all(x) (x).begin(), (x).end()
const int maxn = 2e5 + 5;
const int inf = 1e9 + 7;
int n, k, a[maxn], sz[maxn], par[maxn], id[maxn];
vector<int> adj[maxn], vec[maxn];
bool mark[maxn], vis[maxn];
queue<int> q;
int get_sz(int v, int p = -1){
sz[v] = 1;
for(auto u: adj[v]){
if(u != p && !mark[u]) sz[v] += get_sz(u, v);
}
return sz[v];
}
int find(int v, int p, int s){
for(auto u: adj[v]){
if(u != p && !mark[u] && sz[u] * 2 > s) return find(u, v, s);
}
return v;
}
void dfs(int v, int p, int r){
vis[a[v]] = 0;
par[v] = p;
id[v] = r;
for(auto u: adj[v]){
if(u != p && !mark[u]) dfs(u, v, r);
}
}
int centroid_decompose(int v){
v = find(v, 0, get_sz(v));
dfs(v, v, v);
q.push(a[v]);
int ans = 0;
while(q.size()){
int c = q.front();
q.pop();
if(vis[c]) continue;
vis[c] = 1;
for(auto u: vec[c]){
if(id[u] != v){
ans = k;
break;
}
q.push(a[par[u]]);
}
ans++;
}
mark[v] = 1;
for(auto u: adj[v]) if(!mark[u]) smin(ans, centroid_decompose(u));
return ans;
}
int32_t main()
{
ios_base::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].pb(v);
adj[v].pb(u);
}
for(int i = 1; i <= n; i++){
cin >> a[i];
vec[a[i]].pb(i);
}
cout << centroid_decompose(1) - 1 << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
3 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12632 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12632 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
3 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
12724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
3 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12632 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12632 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
3 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
12724 KB |
Output is correct |
11 |
Correct |
4 ms |
12632 KB |
Output is correct |
12 |
Correct |
3 ms |
12636 KB |
Output is correct |
13 |
Correct |
3 ms |
12636 KB |
Output is correct |
14 |
Correct |
3 ms |
12728 KB |
Output is correct |
15 |
Correct |
3 ms |
12892 KB |
Output is correct |
16 |
Correct |
3 ms |
12636 KB |
Output is correct |
17 |
Correct |
3 ms |
12892 KB |
Output is correct |
18 |
Correct |
4 ms |
12736 KB |
Output is correct |
19 |
Correct |
3 ms |
12892 KB |
Output is correct |
20 |
Correct |
3 ms |
12892 KB |
Output is correct |
21 |
Correct |
5 ms |
12892 KB |
Output is correct |
22 |
Correct |
3 ms |
12892 KB |
Output is correct |
23 |
Correct |
3 ms |
12724 KB |
Output is correct |
24 |
Correct |
3 ms |
12888 KB |
Output is correct |
25 |
Correct |
3 ms |
12892 KB |
Output is correct |
26 |
Correct |
4 ms |
12892 KB |
Output is correct |
27 |
Correct |
4 ms |
12892 KB |
Output is correct |
28 |
Correct |
4 ms |
12892 KB |
Output is correct |
29 |
Correct |
4 ms |
12892 KB |
Output is correct |
30 |
Correct |
4 ms |
12732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
324 ms |
38228 KB |
Output is correct |
2 |
Correct |
138 ms |
38852 KB |
Output is correct |
3 |
Correct |
294 ms |
37972 KB |
Output is correct |
4 |
Correct |
132 ms |
38740 KB |
Output is correct |
5 |
Correct |
298 ms |
35784 KB |
Output is correct |
6 |
Correct |
128 ms |
38736 KB |
Output is correct |
7 |
Correct |
261 ms |
35828 KB |
Output is correct |
8 |
Correct |
132 ms |
38496 KB |
Output is correct |
9 |
Correct |
362 ms |
34548 KB |
Output is correct |
10 |
Correct |
323 ms |
32852 KB |
Output is correct |
11 |
Correct |
340 ms |
34900 KB |
Output is correct |
12 |
Correct |
349 ms |
37204 KB |
Output is correct |
13 |
Correct |
309 ms |
32080 KB |
Output is correct |
14 |
Correct |
328 ms |
37292 KB |
Output is correct |
15 |
Correct |
317 ms |
36948 KB |
Output is correct |
16 |
Correct |
323 ms |
32988 KB |
Output is correct |
17 |
Correct |
344 ms |
33344 KB |
Output is correct |
18 |
Correct |
328 ms |
33532 KB |
Output is correct |
19 |
Correct |
352 ms |
35916 KB |
Output is correct |
20 |
Correct |
324 ms |
37968 KB |
Output is correct |
21 |
Correct |
3 ms |
12636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
3 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12632 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12632 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
3 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
12724 KB |
Output is correct |
11 |
Correct |
4 ms |
12632 KB |
Output is correct |
12 |
Correct |
3 ms |
12636 KB |
Output is correct |
13 |
Correct |
3 ms |
12636 KB |
Output is correct |
14 |
Correct |
3 ms |
12728 KB |
Output is correct |
15 |
Correct |
3 ms |
12892 KB |
Output is correct |
16 |
Correct |
3 ms |
12636 KB |
Output is correct |
17 |
Correct |
3 ms |
12892 KB |
Output is correct |
18 |
Correct |
4 ms |
12736 KB |
Output is correct |
19 |
Correct |
3 ms |
12892 KB |
Output is correct |
20 |
Correct |
3 ms |
12892 KB |
Output is correct |
21 |
Correct |
5 ms |
12892 KB |
Output is correct |
22 |
Correct |
3 ms |
12892 KB |
Output is correct |
23 |
Correct |
3 ms |
12724 KB |
Output is correct |
24 |
Correct |
3 ms |
12888 KB |
Output is correct |
25 |
Correct |
3 ms |
12892 KB |
Output is correct |
26 |
Correct |
4 ms |
12892 KB |
Output is correct |
27 |
Correct |
4 ms |
12892 KB |
Output is correct |
28 |
Correct |
4 ms |
12892 KB |
Output is correct |
29 |
Correct |
4 ms |
12892 KB |
Output is correct |
30 |
Correct |
4 ms |
12732 KB |
Output is correct |
31 |
Correct |
324 ms |
38228 KB |
Output is correct |
32 |
Correct |
138 ms |
38852 KB |
Output is correct |
33 |
Correct |
294 ms |
37972 KB |
Output is correct |
34 |
Correct |
132 ms |
38740 KB |
Output is correct |
35 |
Correct |
298 ms |
35784 KB |
Output is correct |
36 |
Correct |
128 ms |
38736 KB |
Output is correct |
37 |
Correct |
261 ms |
35828 KB |
Output is correct |
38 |
Correct |
132 ms |
38496 KB |
Output is correct |
39 |
Correct |
362 ms |
34548 KB |
Output is correct |
40 |
Correct |
323 ms |
32852 KB |
Output is correct |
41 |
Correct |
340 ms |
34900 KB |
Output is correct |
42 |
Correct |
349 ms |
37204 KB |
Output is correct |
43 |
Correct |
309 ms |
32080 KB |
Output is correct |
44 |
Correct |
328 ms |
37292 KB |
Output is correct |
45 |
Correct |
317 ms |
36948 KB |
Output is correct |
46 |
Correct |
323 ms |
32988 KB |
Output is correct |
47 |
Correct |
344 ms |
33344 KB |
Output is correct |
48 |
Correct |
328 ms |
33532 KB |
Output is correct |
49 |
Correct |
352 ms |
35916 KB |
Output is correct |
50 |
Correct |
324 ms |
37968 KB |
Output is correct |
51 |
Correct |
3 ms |
12636 KB |
Output is correct |
52 |
Correct |
190 ms |
25172 KB |
Output is correct |
53 |
Correct |
208 ms |
25024 KB |
Output is correct |
54 |
Correct |
210 ms |
25172 KB |
Output is correct |
55 |
Correct |
214 ms |
25236 KB |
Output is correct |
56 |
Correct |
212 ms |
25252 KB |
Output is correct |
57 |
Correct |
232 ms |
25252 KB |
Output is correct |
58 |
Correct |
198 ms |
27732 KB |
Output is correct |
59 |
Correct |
193 ms |
27988 KB |
Output is correct |
60 |
Correct |
243 ms |
27916 KB |
Output is correct |
61 |
Correct |
237 ms |
27424 KB |
Output is correct |
62 |
Correct |
161 ms |
38660 KB |
Output is correct |
63 |
Correct |
135 ms |
38668 KB |
Output is correct |
64 |
Correct |
124 ms |
38420 KB |
Output is correct |
65 |
Correct |
125 ms |
38736 KB |
Output is correct |
66 |
Correct |
208 ms |
32196 KB |
Output is correct |
67 |
Correct |
167 ms |
31948 KB |
Output is correct |
68 |
Correct |
172 ms |
32136 KB |
Output is correct |
69 |
Correct |
170 ms |
32252 KB |
Output is correct |
70 |
Correct |
164 ms |
32164 KB |
Output is correct |
71 |
Correct |
166 ms |
32080 KB |
Output is correct |
72 |
Correct |
165 ms |
32164 KB |
Output is correct |
73 |
Correct |
192 ms |
31688 KB |
Output is correct |
74 |
Correct |
171 ms |
32200 KB |
Output is correct |
75 |
Correct |
161 ms |
32136 KB |
Output is correct |
76 |
Correct |
259 ms |
31060 KB |
Output is correct |
77 |
Correct |
247 ms |
29320 KB |
Output is correct |
78 |
Correct |
327 ms |
33616 KB |
Output is correct |
79 |
Correct |
304 ms |
31576 KB |
Output is correct |
80 |
Correct |
340 ms |
37444 KB |
Output is correct |
81 |
Correct |
317 ms |
34616 KB |
Output is correct |
82 |
Correct |
322 ms |
34644 KB |
Output is correct |
83 |
Correct |
330 ms |
32292 KB |
Output is correct |
84 |
Correct |
404 ms |
36760 KB |
Output is correct |
85 |
Correct |
362 ms |
35412 KB |
Output is correct |
86 |
Correct |
359 ms |
31580 KB |
Output is correct |
87 |
Correct |
335 ms |
33104 KB |
Output is correct |
88 |
Correct |
298 ms |
34032 KB |
Output is correct |
89 |
Correct |
342 ms |
30844 KB |
Output is correct |
90 |
Correct |
293 ms |
30800 KB |
Output is correct |
91 |
Correct |
328 ms |
32720 KB |
Output is correct |
92 |
Correct |
304 ms |
31312 KB |
Output is correct |
93 |
Correct |
291 ms |
31384 KB |
Output is correct |
94 |
Correct |
284 ms |
30804 KB |
Output is correct |
95 |
Correct |
280 ms |
32084 KB |
Output is correct |
96 |
Correct |
283 ms |
30628 KB |
Output is correct |
97 |
Correct |
269 ms |
32604 KB |
Output is correct |