#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<int, int>
#define f first
#define s second
const int maxn = 200000, k = 18;
int n, m;
int a[maxn], d[maxn], lc[maxn], id[maxn];
int p[k][maxn];
bool used[maxn];
vector<int> graph[maxn], scc[maxn];
vector<int> g[2][maxn];
stack<int> stk;
int s;
void dfs(int c){
for(int i : graph[c]){
if(i == p[0][c]) continue;
d[i] = d[c] + 1;
p[0][i] = c;
dfs(i);
}
}
void dfs2(int c, int t){
used[c] = !t;
for(int i : g[t][c]){
if(used[i] ^ t) continue;
dfs2(i, t);
}
if(t) scc[s].push_back(c), id[c] = s;
else stk.push(c);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for(int i = 0; i < n - 1; i++){
int u, v;
cin >> u >> v;
u--, v--;
graph[u].push_back(v);
graph[v].push_back(u);
}
dfs(0);
for(int i = 1; i < k; i++)
for(int j = 0; j < n; j++){
p[i][j] = p[i - 1][j] == -1 ? -1 : p[i - 1][p[i - 1][j]];
}
for(int i = 0; i < m; i++) lc[i] = -1;
for(int i = 0; i < n; i++){
cin >> a[i];
a[i]--;
if(lc[a[i]] == -1){
lc[a[i]] = i;
}else{
int x = lc[a[i]], y = i;
if(d[x] < d[y]) swap(x, y);
for(int j = k - 1; j >= 0; j--) if(p[j][x] != -1 && d[p[j][x]] >= d[y]) x = p[j][x];
for(int j = k - 1; j >= 0; j--) if(p[j][x] != p[j][y]) x = p[j][x], y = p[j][y];
lc[a[i]] = x == y ? x : p[0][x];
}
}
for(int i = 0; i < n; i++){
if(i != lc[a[i]] && a[p[0][i]] != a[i]){
g[0][a[p[0][i]]].push_back(a[i]);
g[1][a[i]].push_back(a[p[0][i]]);
}
}
for(int i = 0; i < m; i++)
for(int t = 0; t < 2; t++){
sort(g[t][i].begin(), g[t][i].end());
g[t][i].erase(unique(g[t][i].begin(), g[t][i].end()), g[t][i].end());
}
for(int i = 0; i < m; i++) if(!used[i]) dfs2(i, 0);
while(!stk.empty()){
int c = stk.top();
stk.pop();
if(used[c]) dfs2(c, 1), s++;
}
int ret = m;
for(int i = 0; i < s; i++){
bool t = 1;
for(int j : scc[i])
for(int l : g[1][j]){
t &= id[l] == i;
}
if(t) ret = min(ret, (int)scc[i].size() - 1);
}
cout << ret << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
19328 KB |
Output is correct |
2 |
Correct |
15 ms |
19200 KB |
Output is correct |
3 |
Correct |
16 ms |
19200 KB |
Output is correct |
4 |
Correct |
15 ms |
19200 KB |
Output is correct |
5 |
Correct |
15 ms |
19200 KB |
Output is correct |
6 |
Correct |
16 ms |
19328 KB |
Output is correct |
7 |
Correct |
20 ms |
19200 KB |
Output is correct |
8 |
Correct |
16 ms |
19328 KB |
Output is correct |
9 |
Correct |
18 ms |
19328 KB |
Output is correct |
10 |
Correct |
18 ms |
19200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
19328 KB |
Output is correct |
2 |
Correct |
15 ms |
19200 KB |
Output is correct |
3 |
Correct |
16 ms |
19200 KB |
Output is correct |
4 |
Correct |
15 ms |
19200 KB |
Output is correct |
5 |
Correct |
15 ms |
19200 KB |
Output is correct |
6 |
Correct |
16 ms |
19328 KB |
Output is correct |
7 |
Correct |
20 ms |
19200 KB |
Output is correct |
8 |
Correct |
16 ms |
19328 KB |
Output is correct |
9 |
Correct |
18 ms |
19328 KB |
Output is correct |
10 |
Correct |
18 ms |
19200 KB |
Output is correct |
11 |
Correct |
16 ms |
19584 KB |
Output is correct |
12 |
Correct |
20 ms |
19584 KB |
Output is correct |
13 |
Correct |
20 ms |
19584 KB |
Output is correct |
14 |
Correct |
22 ms |
19584 KB |
Output is correct |
15 |
Correct |
20 ms |
19584 KB |
Output is correct |
16 |
Correct |
17 ms |
19584 KB |
Output is correct |
17 |
Correct |
16 ms |
19584 KB |
Output is correct |
18 |
Correct |
17 ms |
19584 KB |
Output is correct |
19 |
Correct |
18 ms |
19584 KB |
Output is correct |
20 |
Correct |
17 ms |
19584 KB |
Output is correct |
21 |
Correct |
17 ms |
19584 KB |
Output is correct |
22 |
Correct |
19 ms |
19712 KB |
Output is correct |
23 |
Correct |
20 ms |
19584 KB |
Output is correct |
24 |
Correct |
17 ms |
19584 KB |
Output is correct |
25 |
Correct |
19 ms |
19584 KB |
Output is correct |
26 |
Correct |
17 ms |
19688 KB |
Output is correct |
27 |
Correct |
17 ms |
19640 KB |
Output is correct |
28 |
Correct |
17 ms |
19584 KB |
Output is correct |
29 |
Correct |
17 ms |
19584 KB |
Output is correct |
30 |
Correct |
19 ms |
19584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
553 ms |
64188 KB |
Output is correct |
2 |
Correct |
178 ms |
64376 KB |
Output is correct |
3 |
Correct |
506 ms |
63992 KB |
Output is correct |
4 |
Correct |
167 ms |
64376 KB |
Output is correct |
5 |
Correct |
521 ms |
61540 KB |
Output is correct |
6 |
Correct |
169 ms |
64356 KB |
Output is correct |
7 |
Correct |
553 ms |
61168 KB |
Output is correct |
8 |
Correct |
165 ms |
62584 KB |
Output is correct |
9 |
Correct |
369 ms |
56440 KB |
Output is correct |
10 |
Correct |
357 ms |
54900 KB |
Output is correct |
11 |
Correct |
365 ms |
56744 KB |
Output is correct |
12 |
Correct |
372 ms |
58212 KB |
Output is correct |
13 |
Correct |
371 ms |
54600 KB |
Output is correct |
14 |
Correct |
425 ms |
58464 KB |
Output is correct |
15 |
Correct |
391 ms |
58232 KB |
Output is correct |
16 |
Correct |
361 ms |
55160 KB |
Output is correct |
17 |
Correct |
386 ms |
55672 KB |
Output is correct |
18 |
Correct |
368 ms |
55672 KB |
Output is correct |
19 |
Correct |
385 ms |
57652 KB |
Output is correct |
20 |
Correct |
359 ms |
58872 KB |
Output is correct |
21 |
Correct |
15 ms |
19200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
19328 KB |
Output is correct |
2 |
Correct |
15 ms |
19200 KB |
Output is correct |
3 |
Correct |
16 ms |
19200 KB |
Output is correct |
4 |
Correct |
15 ms |
19200 KB |
Output is correct |
5 |
Correct |
15 ms |
19200 KB |
Output is correct |
6 |
Correct |
16 ms |
19328 KB |
Output is correct |
7 |
Correct |
20 ms |
19200 KB |
Output is correct |
8 |
Correct |
16 ms |
19328 KB |
Output is correct |
9 |
Correct |
18 ms |
19328 KB |
Output is correct |
10 |
Correct |
18 ms |
19200 KB |
Output is correct |
11 |
Correct |
16 ms |
19584 KB |
Output is correct |
12 |
Correct |
20 ms |
19584 KB |
Output is correct |
13 |
Correct |
20 ms |
19584 KB |
Output is correct |
14 |
Correct |
22 ms |
19584 KB |
Output is correct |
15 |
Correct |
20 ms |
19584 KB |
Output is correct |
16 |
Correct |
17 ms |
19584 KB |
Output is correct |
17 |
Correct |
16 ms |
19584 KB |
Output is correct |
18 |
Correct |
17 ms |
19584 KB |
Output is correct |
19 |
Correct |
18 ms |
19584 KB |
Output is correct |
20 |
Correct |
17 ms |
19584 KB |
Output is correct |
21 |
Correct |
17 ms |
19584 KB |
Output is correct |
22 |
Correct |
19 ms |
19712 KB |
Output is correct |
23 |
Correct |
20 ms |
19584 KB |
Output is correct |
24 |
Correct |
17 ms |
19584 KB |
Output is correct |
25 |
Correct |
19 ms |
19584 KB |
Output is correct |
26 |
Correct |
17 ms |
19688 KB |
Output is correct |
27 |
Correct |
17 ms |
19640 KB |
Output is correct |
28 |
Correct |
17 ms |
19584 KB |
Output is correct |
29 |
Correct |
17 ms |
19584 KB |
Output is correct |
30 |
Correct |
19 ms |
19584 KB |
Output is correct |
31 |
Correct |
553 ms |
64188 KB |
Output is correct |
32 |
Correct |
178 ms |
64376 KB |
Output is correct |
33 |
Correct |
506 ms |
63992 KB |
Output is correct |
34 |
Correct |
167 ms |
64376 KB |
Output is correct |
35 |
Correct |
521 ms |
61540 KB |
Output is correct |
36 |
Correct |
169 ms |
64356 KB |
Output is correct |
37 |
Correct |
553 ms |
61168 KB |
Output is correct |
38 |
Correct |
165 ms |
62584 KB |
Output is correct |
39 |
Correct |
369 ms |
56440 KB |
Output is correct |
40 |
Correct |
357 ms |
54900 KB |
Output is correct |
41 |
Correct |
365 ms |
56744 KB |
Output is correct |
42 |
Correct |
372 ms |
58212 KB |
Output is correct |
43 |
Correct |
371 ms |
54600 KB |
Output is correct |
44 |
Correct |
425 ms |
58464 KB |
Output is correct |
45 |
Correct |
391 ms |
58232 KB |
Output is correct |
46 |
Correct |
361 ms |
55160 KB |
Output is correct |
47 |
Correct |
386 ms |
55672 KB |
Output is correct |
48 |
Correct |
368 ms |
55672 KB |
Output is correct |
49 |
Correct |
385 ms |
57652 KB |
Output is correct |
50 |
Correct |
359 ms |
58872 KB |
Output is correct |
51 |
Correct |
15 ms |
19200 KB |
Output is correct |
52 |
Correct |
258 ms |
49180 KB |
Output is correct |
53 |
Correct |
267 ms |
49220 KB |
Output is correct |
54 |
Correct |
261 ms |
49272 KB |
Output is correct |
55 |
Correct |
266 ms |
49260 KB |
Output is correct |
56 |
Correct |
270 ms |
49272 KB |
Output is correct |
57 |
Correct |
272 ms |
49272 KB |
Output is correct |
58 |
Correct |
472 ms |
53624 KB |
Output is correct |
59 |
Correct |
528 ms |
54264 KB |
Output is correct |
60 |
Correct |
540 ms |
53628 KB |
Output is correct |
61 |
Correct |
512 ms |
53496 KB |
Output is correct |
62 |
Correct |
179 ms |
64504 KB |
Output is correct |
63 |
Correct |
177 ms |
64376 KB |
Output is correct |
64 |
Correct |
170 ms |
62968 KB |
Output is correct |
65 |
Correct |
171 ms |
64248 KB |
Output is correct |
66 |
Correct |
370 ms |
53812 KB |
Output is correct |
67 |
Correct |
284 ms |
53744 KB |
Output is correct |
68 |
Correct |
321 ms |
53872 KB |
Output is correct |
69 |
Correct |
321 ms |
53900 KB |
Output is correct |
70 |
Correct |
387 ms |
54000 KB |
Output is correct |
71 |
Correct |
317 ms |
53740 KB |
Output is correct |
72 |
Correct |
339 ms |
53744 KB |
Output is correct |
73 |
Correct |
264 ms |
53232 KB |
Output is correct |
74 |
Correct |
315 ms |
53820 KB |
Output is correct |
75 |
Correct |
361 ms |
53872 KB |
Output is correct |
76 |
Correct |
567 ms |
58488 KB |
Output is correct |
77 |
Correct |
459 ms |
57336 KB |
Output is correct |
78 |
Correct |
361 ms |
55800 KB |
Output is correct |
79 |
Correct |
350 ms |
54368 KB |
Output is correct |
80 |
Correct |
349 ms |
58616 KB |
Output is correct |
81 |
Correct |
356 ms |
56440 KB |
Output is correct |
82 |
Correct |
392 ms |
56676 KB |
Output is correct |
83 |
Correct |
362 ms |
54596 KB |
Output is correct |
84 |
Correct |
366 ms |
58232 KB |
Output is correct |
85 |
Correct |
352 ms |
57080 KB |
Output is correct |
86 |
Correct |
378 ms |
54256 KB |
Output is correct |
87 |
Correct |
363 ms |
55412 KB |
Output is correct |
88 |
Correct |
501 ms |
56056 KB |
Output is correct |
89 |
Correct |
399 ms |
53624 KB |
Output is correct |
90 |
Correct |
373 ms |
53488 KB |
Output is correct |
91 |
Correct |
344 ms |
55008 KB |
Output is correct |
92 |
Correct |
387 ms |
54136 KB |
Output is correct |
93 |
Correct |
381 ms |
53880 KB |
Output is correct |
94 |
Correct |
381 ms |
53752 KB |
Output is correct |
95 |
Correct |
357 ms |
54400 KB |
Output is correct |
96 |
Correct |
375 ms |
53764 KB |
Output is correct |
97 |
Correct |
388 ms |
54904 KB |
Output is correct |