#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 7;
vector <int> g[N],gr[N],rg[N],t;
int c[N],sz[N],cmp[N],bad[N];
bool used[N],ok[N];
void add(int u, int v) {
gr[u].push_back(v);
rg[v].push_back(u);
}
int tin[N], tout[N];
int timer = 0;
void init(int u, int p) {
tin[u] = timer++;
for (auto v : g[u]) {
if (v != p) {
init(v, u);
}
}
tout[u] = timer++;
}
bool anc(int a, int b) {
return tin[a] <= tin[b] && tout[b] <= tout[a];
}
void dfs(int u, int p = -1) {
if (u != bad[c[u]]) {
add(c[u], c[p]);
}
for (auto v : g[u]) {
if (v != p) {
dfs(v, u);
}
}
}
void get_topsort(int u) {
used[u] = true;
for (auto v : gr[u]) {
if (!used[v]) {
get_topsort(v);
}
}
t.push_back(u);
}
int cnt = 0;
void jhfs(int u) {
used[u] = true;
cmp[u] = cnt;
++sz[cnt];
for (auto v : rg[u]) {
if (!used[v]) {
jhfs(v);
}
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
int n, k; cin >> n >> k;
for (int i = 0; i + 1 < n; ++i) {
int u, v;
cin >> u >> v;
--u, --v;
g[u].push_back(v);
g[v].push_back(u);
}
vector <vector <int>> who(k);
for (int i = 0; i < n; ++i) {
cin >> c[i];
--c[i];
who[c[i]].push_back(i);
}
init(0, -1);
for (int i = 0; i < k; ++i) {
int v = who[i].front();
for (auto w : who[i])
if (anc(w, v))
v = w;
bool ok = true;
for (auto w : who[i]) ok &= anc(v, w);
if (ok) bad[i] = v;
else bad[i] = -1;
}
dfs(0);
for (int i = 0; i < k; ++i) {
if (!used[i]) get_topsort(i);
}
reverse(t.begin(), t.end());
fill(used, used + k, false);
for (int u : t) {
if (!used[u]) {
jhfs(u);
++cnt;
}
}
fill(ok, ok + cnt, true);
for (int i = 0; i < k; ++i) {
for (int j : gr[i]) {
if (cmp[i] != cmp[j]) {
ok[cmp[i]] = false;
}
}
}
int ans = n;
for (int i = 0; i < cnt; ++i) {
if (ok[i]) ans = min(ans, sz[i]);
}
cout << ans - 1 << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
11 ms |
14412 KB |
Output is correct |
4 |
Correct |
10 ms |
14424 KB |
Output is correct |
5 |
Correct |
9 ms |
14460 KB |
Output is correct |
6 |
Correct |
8 ms |
14464 KB |
Output is correct |
7 |
Correct |
8 ms |
14420 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
8 ms |
14424 KB |
Output is correct |
10 |
Correct |
27 ms |
14360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
11 ms |
14412 KB |
Output is correct |
4 |
Correct |
10 ms |
14424 KB |
Output is correct |
5 |
Correct |
9 ms |
14460 KB |
Output is correct |
6 |
Correct |
8 ms |
14464 KB |
Output is correct |
7 |
Correct |
8 ms |
14420 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
8 ms |
14424 KB |
Output is correct |
10 |
Correct |
27 ms |
14360 KB |
Output is correct |
11 |
Correct |
10 ms |
14544 KB |
Output is correct |
12 |
Correct |
12 ms |
14668 KB |
Output is correct |
13 |
Correct |
11 ms |
14616 KB |
Output is correct |
14 |
Correct |
9 ms |
14524 KB |
Output is correct |
15 |
Correct |
10 ms |
14708 KB |
Output is correct |
16 |
Correct |
9 ms |
14676 KB |
Output is correct |
17 |
Correct |
9 ms |
14676 KB |
Output is correct |
18 |
Correct |
9 ms |
14676 KB |
Output is correct |
19 |
Correct |
9 ms |
14676 KB |
Output is correct |
20 |
Correct |
11 ms |
14648 KB |
Output is correct |
21 |
Correct |
8 ms |
14676 KB |
Output is correct |
22 |
Correct |
10 ms |
14820 KB |
Output is correct |
23 |
Correct |
10 ms |
14704 KB |
Output is correct |
24 |
Correct |
12 ms |
14676 KB |
Output is correct |
25 |
Correct |
12 ms |
14696 KB |
Output is correct |
26 |
Correct |
11 ms |
14676 KB |
Output is correct |
27 |
Correct |
13 ms |
14676 KB |
Output is correct |
28 |
Correct |
9 ms |
14676 KB |
Output is correct |
29 |
Correct |
11 ms |
14696 KB |
Output is correct |
30 |
Correct |
9 ms |
14760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
203 ms |
52516 KB |
Output is correct |
2 |
Correct |
119 ms |
52748 KB |
Output is correct |
3 |
Correct |
227 ms |
52228 KB |
Output is correct |
4 |
Correct |
114 ms |
52824 KB |
Output is correct |
5 |
Correct |
205 ms |
49020 KB |
Output is correct |
6 |
Correct |
111 ms |
52676 KB |
Output is correct |
7 |
Correct |
204 ms |
48772 KB |
Output is correct |
8 |
Correct |
161 ms |
51024 KB |
Output is correct |
9 |
Correct |
189 ms |
43692 KB |
Output is correct |
10 |
Correct |
213 ms |
41600 KB |
Output is correct |
11 |
Correct |
237 ms |
44024 KB |
Output is correct |
12 |
Correct |
176 ms |
46152 KB |
Output is correct |
13 |
Correct |
187 ms |
41364 KB |
Output is correct |
14 |
Correct |
168 ms |
46396 KB |
Output is correct |
15 |
Correct |
201 ms |
46156 KB |
Output is correct |
16 |
Correct |
235 ms |
42096 KB |
Output is correct |
17 |
Correct |
245 ms |
42568 KB |
Output is correct |
18 |
Correct |
220 ms |
42816 KB |
Output is correct |
19 |
Correct |
191 ms |
45352 KB |
Output is correct |
20 |
Correct |
171 ms |
47048 KB |
Output is correct |
21 |
Correct |
8 ms |
14420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
11 ms |
14412 KB |
Output is correct |
4 |
Correct |
10 ms |
14424 KB |
Output is correct |
5 |
Correct |
9 ms |
14460 KB |
Output is correct |
6 |
Correct |
8 ms |
14464 KB |
Output is correct |
7 |
Correct |
8 ms |
14420 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
8 ms |
14424 KB |
Output is correct |
10 |
Correct |
27 ms |
14360 KB |
Output is correct |
11 |
Correct |
10 ms |
14544 KB |
Output is correct |
12 |
Correct |
12 ms |
14668 KB |
Output is correct |
13 |
Correct |
11 ms |
14616 KB |
Output is correct |
14 |
Correct |
9 ms |
14524 KB |
Output is correct |
15 |
Correct |
10 ms |
14708 KB |
Output is correct |
16 |
Correct |
9 ms |
14676 KB |
Output is correct |
17 |
Correct |
9 ms |
14676 KB |
Output is correct |
18 |
Correct |
9 ms |
14676 KB |
Output is correct |
19 |
Correct |
9 ms |
14676 KB |
Output is correct |
20 |
Correct |
11 ms |
14648 KB |
Output is correct |
21 |
Correct |
8 ms |
14676 KB |
Output is correct |
22 |
Correct |
10 ms |
14820 KB |
Output is correct |
23 |
Correct |
10 ms |
14704 KB |
Output is correct |
24 |
Correct |
12 ms |
14676 KB |
Output is correct |
25 |
Correct |
12 ms |
14696 KB |
Output is correct |
26 |
Correct |
11 ms |
14676 KB |
Output is correct |
27 |
Correct |
13 ms |
14676 KB |
Output is correct |
28 |
Correct |
9 ms |
14676 KB |
Output is correct |
29 |
Correct |
11 ms |
14696 KB |
Output is correct |
30 |
Correct |
9 ms |
14760 KB |
Output is correct |
31 |
Correct |
203 ms |
52516 KB |
Output is correct |
32 |
Correct |
119 ms |
52748 KB |
Output is correct |
33 |
Correct |
227 ms |
52228 KB |
Output is correct |
34 |
Correct |
114 ms |
52824 KB |
Output is correct |
35 |
Correct |
205 ms |
49020 KB |
Output is correct |
36 |
Correct |
111 ms |
52676 KB |
Output is correct |
37 |
Correct |
204 ms |
48772 KB |
Output is correct |
38 |
Correct |
161 ms |
51024 KB |
Output is correct |
39 |
Correct |
189 ms |
43692 KB |
Output is correct |
40 |
Correct |
213 ms |
41600 KB |
Output is correct |
41 |
Correct |
237 ms |
44024 KB |
Output is correct |
42 |
Correct |
176 ms |
46152 KB |
Output is correct |
43 |
Correct |
187 ms |
41364 KB |
Output is correct |
44 |
Correct |
168 ms |
46396 KB |
Output is correct |
45 |
Correct |
201 ms |
46156 KB |
Output is correct |
46 |
Correct |
235 ms |
42096 KB |
Output is correct |
47 |
Correct |
245 ms |
42568 KB |
Output is correct |
48 |
Correct |
220 ms |
42816 KB |
Output is correct |
49 |
Correct |
191 ms |
45352 KB |
Output is correct |
50 |
Correct |
171 ms |
47048 KB |
Output is correct |
51 |
Correct |
8 ms |
14420 KB |
Output is correct |
52 |
Correct |
211 ms |
32784 KB |
Output is correct |
53 |
Correct |
212 ms |
32644 KB |
Output is correct |
54 |
Correct |
222 ms |
32652 KB |
Output is correct |
55 |
Correct |
216 ms |
32780 KB |
Output is correct |
56 |
Correct |
195 ms |
32760 KB |
Output is correct |
57 |
Correct |
249 ms |
32716 KB |
Output is correct |
58 |
Correct |
212 ms |
38984 KB |
Output is correct |
59 |
Correct |
201 ms |
39636 KB |
Output is correct |
60 |
Correct |
205 ms |
39268 KB |
Output is correct |
61 |
Correct |
227 ms |
39096 KB |
Output is correct |
62 |
Correct |
108 ms |
52804 KB |
Output is correct |
63 |
Correct |
115 ms |
52748 KB |
Output is correct |
64 |
Correct |
150 ms |
51340 KB |
Output is correct |
65 |
Correct |
149 ms |
52748 KB |
Output is correct |
66 |
Correct |
168 ms |
40464 KB |
Output is correct |
67 |
Correct |
157 ms |
40384 KB |
Output is correct |
68 |
Correct |
155 ms |
40332 KB |
Output is correct |
69 |
Correct |
143 ms |
40376 KB |
Output is correct |
70 |
Correct |
163 ms |
40264 KB |
Output is correct |
71 |
Correct |
188 ms |
40336 KB |
Output is correct |
72 |
Correct |
168 ms |
40364 KB |
Output is correct |
73 |
Correct |
181 ms |
39784 KB |
Output is correct |
74 |
Correct |
141 ms |
40440 KB |
Output is correct |
75 |
Correct |
151 ms |
40312 KB |
Output is correct |
76 |
Correct |
208 ms |
44868 KB |
Output is correct |
77 |
Correct |
200 ms |
43368 KB |
Output is correct |
78 |
Correct |
185 ms |
42620 KB |
Output is correct |
79 |
Correct |
177 ms |
40848 KB |
Output is correct |
80 |
Correct |
193 ms |
46520 KB |
Output is correct |
81 |
Correct |
218 ms |
43796 KB |
Output is correct |
82 |
Correct |
201 ms |
43932 KB |
Output is correct |
83 |
Correct |
243 ms |
41240 KB |
Output is correct |
84 |
Correct |
215 ms |
46048 KB |
Output is correct |
85 |
Correct |
228 ms |
44728 KB |
Output is correct |
86 |
Correct |
209 ms |
40904 KB |
Output is correct |
87 |
Correct |
190 ms |
42288 KB |
Output is correct |
88 |
Correct |
209 ms |
42908 KB |
Output is correct |
89 |
Correct |
192 ms |
39760 KB |
Output is correct |
90 |
Correct |
172 ms |
39528 KB |
Output is correct |
91 |
Correct |
221 ms |
41380 KB |
Output is correct |
92 |
Correct |
184 ms |
40528 KB |
Output is correct |
93 |
Correct |
166 ms |
40076 KB |
Output is correct |
94 |
Correct |
168 ms |
39548 KB |
Output is correct |
95 |
Correct |
179 ms |
40820 KB |
Output is correct |
96 |
Correct |
175 ms |
39824 KB |
Output is correct |
97 |
Correct |
172 ms |
41500 KB |
Output is correct |