#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld double
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define pb emplace_back
#define X first
#define Y second
const int N = 2e5 + 5;
typedef pair<int,int> ii;
int p[N];
int s[N];
int init(int n) {
iota(p + 1,p + 1 + n,1);
fill(s + 1,s + 1 + n,1);
return 1;
}
int lead(int x) {
return p[x] == x ? x : p[x] = lead(p[x]);
}
int join(int x,int y) {
x = lead(x);
y = lead(y);
if (x == y) return 0;
if (s[x] < s[y])
swap(x,y);
p[y] = x;
s[x] += s[y];
return 1;
}
int c[N];
int a[N];
int b[N];
vector<int> S[N];
vector<int> g[N];
int nCh[N], pos[N];
int led[N], par[N];
int arr[N], tot = 0;
void dfs(int u,int p) {
par[u] = p;
nCh[u] = 1;
for(int v : g[u]) if (v != p) {
dfs(v,u);
nCh[u] += nCh[v];
}
}
void hld(int u,int ok) {
if (ok) led[u] = u;
else led[u] = led[par[u]];
pos[u] = ++tot;
arr[tot] = u;
int B = 0;
for(int v : g[u]) if (v != par[u])
if (nCh[B] < nCh[v])
B = v;
if (B) hld(B,0);
for(int v : g[u]) if (v != par[u] && v != B)
hld(v,1);
}
bool is_anc(int u,int v) { return pos[u] <= pos[v] && pos[u] + nCh[u] >= pos[v] + nCh[v]; }
int lca(int u,int v) {
while (!is_anc(led[u],v)) u = par[led[u]];
while (!is_anc(led[v],u)) v = par[led[v]];
if (pos[u] > pos[v])
swap(u,v);
assert(is_anc(u,v));
return u;
}
int tr[N << 1];
int ok[N];
vector<int> vec[N];
bool have[N];
bool need[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n; cin >> n;
int k; cin >> k;
for(int i = 1 ; i < n ; ++i) {
cin >> a[i];
cin >> b[i];
}
for(int i = 1 ; i <= n ; ++i)
cin >> c[i];
init(n);
for(int i = 1 ; i < n ; ++i)
if (c[a[i]] == c[b[i]])
join(a[i],b[i]);
for(int i = 1 ; i < n ; ++i)
if (c[a[i]] != c[b[i]]) {
int x = lead(a[i]);
int y = lead(b[i]);
g[x].pb(y);
g[y].pb(x);
}
for(int i = 1 ; i <= n ; ++i)
if (i == p[i])
S[c[i]].pb(i);
dfs(lead(1),0);
hld(lead(1),1);
auto upd = [&](int p,int v) {
for(tr[p += tot - 1] = v ; p > 1 ; p >>= 1)
tr[p >> 1] = min(tr[p],tr[p ^ 1]);
};
auto get = [&](int l,int r) {
l += tot - 1;
r += tot;
int ans = 1e9;
for(; l < r ; l >>= 1, r >>= 1) {
if (l & 1) ans = min(ans,tr[l++]);
if (r & 1) ans = min(ans,tr[--r]);
}
return ans;
};
for(int i = 1 ; i <= tot ; ++i)
upd(i,1e9);
for(int i = 1 ; i <= k ; ++i)
sort(all(S[i]),[&](int x,int y) {
return pos[x] < pos[y];
});
vector<int> perms(k);
iota(all(perms),1);
sort(all(perms),[&](int x,int y) {
int a = lca(S[x][0],S[x].back());
int b = lca(S[y][0],S[y].back());
return pos[a] < pos[b];
});
for(int i : perms) {
int R = lca(S[i][0],S[i].back());
int P = pos[R];
for(int x : S[i]) {
while (!is_anc(led[x],R)) {
P = min(get(pos[led[x]],pos[x]),P);
x = par[led[x]];
}
P = min(get(pos[R],pos[x]),P);
}
for(int x : S[i])
upd(pos[x],P);
if (P == pos[S[i][0]])
ok[i] = 1;
}
int ans = 1e9;
for(int i = 1 ; i <= k ; ++i) if (ok[i]) {
need[i] = 1;
queue<int> qu;
stack<int> st;
for(int x : S[i])
qu.push(x),
have[x] = 1;
int cur = 0;
while (qu.size()) {
int u = qu.front();
qu.pop();
st.push(u);
if (get(pos[u],pos[u]) != pos[S[i][0]]) {
cur = 1e9;
while (qu.size()) {
st.push(qu.front());
qu.pop();
}
break;
}
if (u != S[i][0])
u = par[u];
if(!have[u]) {
have[u] = 1;
qu.push(u);
if(!need[c[u]]) {
need[c[u]] = 1; ++cur;
for(int x : S[c[u]])
if(!have[x]) {
have[x] = 1;
qu.push(x);
}
}
}
}
while (st.size()) {
int u = st.top(); st.pop();
have[u] = 0;
need[c[u]] = 0;
}
if (ans > cur)
ans = cur;
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
14592 KB |
Output is correct |
2 |
Correct |
12 ms |
14464 KB |
Output is correct |
3 |
Correct |
12 ms |
14464 KB |
Output is correct |
4 |
Correct |
13 ms |
14464 KB |
Output is correct |
5 |
Correct |
12 ms |
14464 KB |
Output is correct |
6 |
Correct |
12 ms |
14464 KB |
Output is correct |
7 |
Correct |
13 ms |
14464 KB |
Output is correct |
8 |
Correct |
12 ms |
14464 KB |
Output is correct |
9 |
Correct |
12 ms |
14464 KB |
Output is correct |
10 |
Correct |
12 ms |
14464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
14592 KB |
Output is correct |
2 |
Correct |
12 ms |
14464 KB |
Output is correct |
3 |
Correct |
12 ms |
14464 KB |
Output is correct |
4 |
Correct |
13 ms |
14464 KB |
Output is correct |
5 |
Correct |
12 ms |
14464 KB |
Output is correct |
6 |
Correct |
12 ms |
14464 KB |
Output is correct |
7 |
Correct |
13 ms |
14464 KB |
Output is correct |
8 |
Correct |
12 ms |
14464 KB |
Output is correct |
9 |
Correct |
12 ms |
14464 KB |
Output is correct |
10 |
Correct |
12 ms |
14464 KB |
Output is correct |
11 |
Correct |
13 ms |
14720 KB |
Output is correct |
12 |
Correct |
14 ms |
14720 KB |
Output is correct |
13 |
Correct |
13 ms |
14720 KB |
Output is correct |
14 |
Correct |
13 ms |
14720 KB |
Output is correct |
15 |
Correct |
13 ms |
14720 KB |
Output is correct |
16 |
Correct |
13 ms |
14720 KB |
Output is correct |
17 |
Correct |
14 ms |
14592 KB |
Output is correct |
18 |
Correct |
13 ms |
14720 KB |
Output is correct |
19 |
Correct |
13 ms |
14592 KB |
Output is correct |
20 |
Correct |
13 ms |
14720 KB |
Output is correct |
21 |
Correct |
13 ms |
14592 KB |
Output is correct |
22 |
Correct |
14 ms |
14848 KB |
Output is correct |
23 |
Correct |
14 ms |
14720 KB |
Output is correct |
24 |
Correct |
14 ms |
14720 KB |
Output is correct |
25 |
Correct |
13 ms |
14848 KB |
Output is correct |
26 |
Correct |
15 ms |
14848 KB |
Output is correct |
27 |
Correct |
13 ms |
14848 KB |
Output is correct |
28 |
Correct |
14 ms |
14720 KB |
Output is correct |
29 |
Correct |
14 ms |
14720 KB |
Output is correct |
30 |
Correct |
13 ms |
14720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
398 ms |
46456 KB |
Output is correct |
2 |
Correct |
192 ms |
46844 KB |
Output is correct |
3 |
Correct |
370 ms |
46200 KB |
Output is correct |
4 |
Correct |
199 ms |
46844 KB |
Output is correct |
5 |
Correct |
324 ms |
43104 KB |
Output is correct |
6 |
Correct |
200 ms |
46456 KB |
Output is correct |
7 |
Correct |
338 ms |
42620 KB |
Output is correct |
8 |
Correct |
168 ms |
41852 KB |
Output is correct |
9 |
Correct |
316 ms |
42232 KB |
Output is correct |
10 |
Correct |
294 ms |
40200 KB |
Output is correct |
11 |
Correct |
294 ms |
42488 KB |
Output is correct |
12 |
Correct |
303 ms |
44804 KB |
Output is correct |
13 |
Correct |
291 ms |
39968 KB |
Output is correct |
14 |
Correct |
293 ms |
44920 KB |
Output is correct |
15 |
Correct |
292 ms |
44664 KB |
Output is correct |
16 |
Correct |
285 ms |
40572 KB |
Output is correct |
17 |
Correct |
307 ms |
41224 KB |
Output is correct |
18 |
Correct |
291 ms |
41292 KB |
Output is correct |
19 |
Correct |
296 ms |
43896 KB |
Output is correct |
20 |
Correct |
292 ms |
45560 KB |
Output is correct |
21 |
Correct |
12 ms |
14464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
14592 KB |
Output is correct |
2 |
Correct |
12 ms |
14464 KB |
Output is correct |
3 |
Correct |
12 ms |
14464 KB |
Output is correct |
4 |
Correct |
13 ms |
14464 KB |
Output is correct |
5 |
Correct |
12 ms |
14464 KB |
Output is correct |
6 |
Correct |
12 ms |
14464 KB |
Output is correct |
7 |
Correct |
13 ms |
14464 KB |
Output is correct |
8 |
Correct |
12 ms |
14464 KB |
Output is correct |
9 |
Correct |
12 ms |
14464 KB |
Output is correct |
10 |
Correct |
12 ms |
14464 KB |
Output is correct |
11 |
Correct |
13 ms |
14720 KB |
Output is correct |
12 |
Correct |
14 ms |
14720 KB |
Output is correct |
13 |
Correct |
13 ms |
14720 KB |
Output is correct |
14 |
Correct |
13 ms |
14720 KB |
Output is correct |
15 |
Correct |
13 ms |
14720 KB |
Output is correct |
16 |
Correct |
13 ms |
14720 KB |
Output is correct |
17 |
Correct |
14 ms |
14592 KB |
Output is correct |
18 |
Correct |
13 ms |
14720 KB |
Output is correct |
19 |
Correct |
13 ms |
14592 KB |
Output is correct |
20 |
Correct |
13 ms |
14720 KB |
Output is correct |
21 |
Correct |
13 ms |
14592 KB |
Output is correct |
22 |
Correct |
14 ms |
14848 KB |
Output is correct |
23 |
Correct |
14 ms |
14720 KB |
Output is correct |
24 |
Correct |
14 ms |
14720 KB |
Output is correct |
25 |
Correct |
13 ms |
14848 KB |
Output is correct |
26 |
Correct |
15 ms |
14848 KB |
Output is correct |
27 |
Correct |
13 ms |
14848 KB |
Output is correct |
28 |
Correct |
14 ms |
14720 KB |
Output is correct |
29 |
Correct |
14 ms |
14720 KB |
Output is correct |
30 |
Correct |
13 ms |
14720 KB |
Output is correct |
31 |
Correct |
398 ms |
46456 KB |
Output is correct |
32 |
Correct |
192 ms |
46844 KB |
Output is correct |
33 |
Correct |
370 ms |
46200 KB |
Output is correct |
34 |
Correct |
199 ms |
46844 KB |
Output is correct |
35 |
Correct |
324 ms |
43104 KB |
Output is correct |
36 |
Correct |
200 ms |
46456 KB |
Output is correct |
37 |
Correct |
338 ms |
42620 KB |
Output is correct |
38 |
Correct |
168 ms |
41852 KB |
Output is correct |
39 |
Correct |
316 ms |
42232 KB |
Output is correct |
40 |
Correct |
294 ms |
40200 KB |
Output is correct |
41 |
Correct |
294 ms |
42488 KB |
Output is correct |
42 |
Correct |
303 ms |
44804 KB |
Output is correct |
43 |
Correct |
291 ms |
39968 KB |
Output is correct |
44 |
Correct |
293 ms |
44920 KB |
Output is correct |
45 |
Correct |
292 ms |
44664 KB |
Output is correct |
46 |
Correct |
285 ms |
40572 KB |
Output is correct |
47 |
Correct |
307 ms |
41224 KB |
Output is correct |
48 |
Correct |
291 ms |
41292 KB |
Output is correct |
49 |
Correct |
296 ms |
43896 KB |
Output is correct |
50 |
Correct |
292 ms |
45560 KB |
Output is correct |
51 |
Correct |
12 ms |
14464 KB |
Output is correct |
52 |
Correct |
325 ms |
33144 KB |
Output is correct |
53 |
Correct |
313 ms |
33016 KB |
Output is correct |
54 |
Correct |
329 ms |
32908 KB |
Output is correct |
55 |
Correct |
313 ms |
33016 KB |
Output is correct |
56 |
Correct |
317 ms |
33016 KB |
Output is correct |
57 |
Correct |
320 ms |
33148 KB |
Output is correct |
58 |
Correct |
295 ms |
35320 KB |
Output is correct |
59 |
Correct |
305 ms |
35964 KB |
Output is correct |
60 |
Correct |
376 ms |
35576 KB |
Output is correct |
61 |
Correct |
363 ms |
35192 KB |
Output is correct |
62 |
Correct |
196 ms |
46944 KB |
Output is correct |
63 |
Correct |
198 ms |
46840 KB |
Output is correct |
64 |
Correct |
171 ms |
43000 KB |
Output is correct |
65 |
Correct |
197 ms |
46556 KB |
Output is correct |
66 |
Correct |
159 ms |
28144 KB |
Output is correct |
67 |
Correct |
166 ms |
28016 KB |
Output is correct |
68 |
Correct |
158 ms |
28016 KB |
Output is correct |
69 |
Correct |
158 ms |
28116 KB |
Output is correct |
70 |
Correct |
156 ms |
28016 KB |
Output is correct |
71 |
Correct |
154 ms |
28016 KB |
Output is correct |
72 |
Correct |
167 ms |
28080 KB |
Output is correct |
73 |
Correct |
153 ms |
28016 KB |
Output is correct |
74 |
Correct |
166 ms |
28144 KB |
Output is correct |
75 |
Correct |
171 ms |
28156 KB |
Output is correct |
76 |
Correct |
387 ms |
38896 KB |
Output is correct |
77 |
Correct |
369 ms |
37368 KB |
Output is correct |
78 |
Correct |
286 ms |
41076 KB |
Output is correct |
79 |
Correct |
285 ms |
39504 KB |
Output is correct |
80 |
Correct |
314 ms |
45224 KB |
Output is correct |
81 |
Correct |
316 ms |
42344 KB |
Output is correct |
82 |
Correct |
290 ms |
42492 KB |
Output is correct |
83 |
Correct |
290 ms |
39800 KB |
Output is correct |
84 |
Correct |
302 ms |
44488 KB |
Output is correct |
85 |
Correct |
315 ms |
43640 KB |
Output is correct |
86 |
Correct |
285 ms |
39372 KB |
Output is correct |
87 |
Correct |
291 ms |
40824 KB |
Output is correct |
88 |
Correct |
296 ms |
41676 KB |
Output is correct |
89 |
Correct |
292 ms |
38436 KB |
Output is correct |
90 |
Correct |
292 ms |
38476 KB |
Output is correct |
91 |
Correct |
290 ms |
40312 KB |
Output is correct |
92 |
Correct |
298 ms |
39088 KB |
Output is correct |
93 |
Correct |
301 ms |
38904 KB |
Output is correct |
94 |
Correct |
291 ms |
38380 KB |
Output is correct |
95 |
Correct |
295 ms |
39700 KB |
Output is correct |
96 |
Correct |
295 ms |
38508 KB |
Output is correct |
97 |
Correct |
316 ms |
40184 KB |
Output is correct |