#include <bits/stdc++.h>
using i64 = long long;
using namespace std;
struct DSU {
std::vector<int> f, siz;
DSU(int n) : f(n), siz(n, 1) { std::iota(f.begin(), f.end(), 0); }
int leader(int x) {
while (x != f[x]) x = f[x] = f[f[x]];
return x;
}
bool same(int x, int y) { return leader(x) == leader(y); }
bool merge(int x, int y) {
x = leader(x);
y = leader(y);
if (x == y) return false;
siz[x] += siz[y];
f[y] = x;
return true;
}
int size(int x) { return siz[leader(x)]; }
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
vector<vector<int>> e(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
e[u].push_back(v);
e[v].push_back(u);
}
vector<int> s(n);
vector<int> L(k);
for (int i = 0; i < n; i++) {
cin >> s[i];
s[i]--;
L[s[i]] = i;
}
vector<int> in(n), out(n);
vector up(n, vector<int>(20));
int timeStamp = 0;
function<void(int, int)> dfs = [&](int u, int p) {
in[u] = timeStamp++;
up[u][0] = p;
for (int i = 1; i < 20; i++) {
up[u][i] = up[up[u][i - 1]][i - 1];
}
for (int v : e[u]) {
if (v != p) {
dfs(v, u);
}
}
out[u] = timeStamp;
};
dfs(0, 0);
cerr << "How ";
auto is = [&](int u, int v) {
return in[u] <= in[v] && out[u] >= out[v];
};
auto lca = [&](int u, int v) {
if (is(u, v)) return u;
if (is(v, u)) return v;
for (int i = 19; i >= 0; i--) {
if (!is(up[u][i], v)) {
u = up[u][i];
}
}
return up[u][0];
};
for (int i = 0; i < n; i++) {
L[s[i]] = lca(L[s[i]], i);
}
vector<int> cum(n);
for (int i = 0; i < n; i++) {
cum[i]++;
cum[L[s[i]]]--;
}
cerr << "are ";
DSU dsu(n);
function<int(int, int)> dfs2 = [&](int u, int p) {
int sum = cum[u];
for (int v : e[u]) {
if (v != p) {
sum += dfs2(v, u);
}
}
if (sum) {
dsu.merge(u, p);
}
return sum;
};
dfs2(0, -1);
cerr << "you ";
vector<vector<int>> adj(n);
for (int i = 0; i < n; i++) {
for (int j : e[i]) {
int u = dsu.leader(i);
int v = dsu.leader(j);
if (u != v) {
adj[u].push_back(v);
}
}
}
cerr << "doing?\n";
int cnt = 0;
for (int i = 0; i < n; i++) {
cnt += adj[i].size() == 1;
}
cout << (cnt + 1) / 2 << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
3 ms |
980 KB |
Output is correct |
27 |
Correct |
2 ms |
980 KB |
Output is correct |
28 |
Correct |
2 ms |
1108 KB |
Output is correct |
29 |
Correct |
3 ms |
980 KB |
Output is correct |
30 |
Correct |
2 ms |
980 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
2 ms |
1108 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
2 ms |
980 KB |
Output is correct |
35 |
Correct |
2 ms |
1004 KB |
Output is correct |
36 |
Correct |
3 ms |
980 KB |
Output is correct |
37 |
Correct |
2 ms |
980 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
2 ms |
980 KB |
Output is correct |
40 |
Correct |
2 ms |
980 KB |
Output is correct |
41 |
Correct |
2 ms |
980 KB |
Output is correct |
42 |
Correct |
2 ms |
980 KB |
Output is correct |
43 |
Correct |
4 ms |
1252 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
2 ms |
980 KB |
Output is correct |
46 |
Correct |
4 ms |
980 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
3 ms |
980 KB |
Output is correct |
49 |
Correct |
2 ms |
1032 KB |
Output is correct |
50 |
Correct |
3 ms |
1108 KB |
Output is correct |
51 |
Correct |
2 ms |
980 KB |
Output is correct |
52 |
Correct |
2 ms |
980 KB |
Output is correct |
53 |
Correct |
2 ms |
980 KB |
Output is correct |
54 |
Correct |
2 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
85 ms |
22692 KB |
Output is correct |
27 |
Correct |
111 ms |
22284 KB |
Output is correct |
28 |
Correct |
2 ms |
1012 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
124 ms |
22248 KB |
Output is correct |
32 |
Correct |
2 ms |
980 KB |
Output is correct |
33 |
Correct |
143 ms |
30932 KB |
Output is correct |
34 |
Correct |
105 ms |
22304 KB |
Output is correct |
35 |
Correct |
2 ms |
980 KB |
Output is correct |
36 |
Correct |
131 ms |
23212 KB |
Output is correct |
37 |
Correct |
2 ms |
980 KB |
Output is correct |
38 |
Correct |
3 ms |
980 KB |
Output is correct |
39 |
Correct |
75 ms |
22564 KB |
Output is correct |
40 |
Correct |
3 ms |
1236 KB |
Output is correct |
41 |
Correct |
105 ms |
22352 KB |
Output is correct |
42 |
Correct |
171 ms |
25032 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
139 ms |
31292 KB |
Output is correct |
45 |
Correct |
174 ms |
26828 KB |
Output is correct |
46 |
Correct |
2 ms |
980 KB |
Output is correct |
47 |
Correct |
3 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
22768 KB |
Output is correct |
2 |
Correct |
92 ms |
26612 KB |
Output is correct |
3 |
Correct |
3 ms |
1016 KB |
Output is correct |
4 |
Correct |
2 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
980 KB |
Output is correct |
8 |
Correct |
123 ms |
22888 KB |
Output is correct |
9 |
Correct |
2 ms |
980 KB |
Output is correct |
10 |
Correct |
121 ms |
22364 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
112 ms |
22232 KB |
Output is correct |
13 |
Correct |
123 ms |
23056 KB |
Output is correct |
14 |
Correct |
143 ms |
25996 KB |
Output is correct |
15 |
Correct |
85 ms |
22540 KB |
Output is correct |
16 |
Correct |
2 ms |
980 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
85 ms |
25740 KB |
Output is correct |
19 |
Correct |
140 ms |
30564 KB |
Output is correct |
20 |
Correct |
3 ms |
1024 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
121 ms |
23420 KB |
Output is correct |
23 |
Correct |
3 ms |
980 KB |
Output is correct |
24 |
Correct |
124 ms |
22496 KB |
Output is correct |
25 |
Correct |
201 ms |
28988 KB |
Output is correct |
26 |
Correct |
4 ms |
1108 KB |
Output is correct |
27 |
Correct |
3 ms |
1108 KB |
Output is correct |
28 |
Correct |
2 ms |
980 KB |
Output is correct |
29 |
Correct |
8 ms |
900 KB |
Output is correct |
30 |
Correct |
4 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
3 ms |
980 KB |
Output is correct |
27 |
Correct |
2 ms |
980 KB |
Output is correct |
28 |
Correct |
2 ms |
1108 KB |
Output is correct |
29 |
Correct |
3 ms |
980 KB |
Output is correct |
30 |
Correct |
2 ms |
980 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
2 ms |
1108 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
2 ms |
980 KB |
Output is correct |
35 |
Correct |
2 ms |
1004 KB |
Output is correct |
36 |
Correct |
3 ms |
980 KB |
Output is correct |
37 |
Correct |
2 ms |
980 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
2 ms |
980 KB |
Output is correct |
40 |
Correct |
2 ms |
980 KB |
Output is correct |
41 |
Correct |
2 ms |
980 KB |
Output is correct |
42 |
Correct |
2 ms |
980 KB |
Output is correct |
43 |
Correct |
4 ms |
1252 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
2 ms |
980 KB |
Output is correct |
46 |
Correct |
4 ms |
980 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
3 ms |
980 KB |
Output is correct |
49 |
Correct |
2 ms |
1032 KB |
Output is correct |
50 |
Correct |
3 ms |
1108 KB |
Output is correct |
51 |
Correct |
2 ms |
980 KB |
Output is correct |
52 |
Correct |
2 ms |
980 KB |
Output is correct |
53 |
Correct |
2 ms |
980 KB |
Output is correct |
54 |
Correct |
2 ms |
980 KB |
Output is correct |
55 |
Correct |
1 ms |
212 KB |
Output is correct |
56 |
Correct |
85 ms |
22692 KB |
Output is correct |
57 |
Correct |
111 ms |
22284 KB |
Output is correct |
58 |
Correct |
2 ms |
1012 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
0 ms |
212 KB |
Output is correct |
61 |
Correct |
124 ms |
22248 KB |
Output is correct |
62 |
Correct |
2 ms |
980 KB |
Output is correct |
63 |
Correct |
143 ms |
30932 KB |
Output is correct |
64 |
Correct |
105 ms |
22304 KB |
Output is correct |
65 |
Correct |
2 ms |
980 KB |
Output is correct |
66 |
Correct |
131 ms |
23212 KB |
Output is correct |
67 |
Correct |
2 ms |
980 KB |
Output is correct |
68 |
Correct |
3 ms |
980 KB |
Output is correct |
69 |
Correct |
75 ms |
22564 KB |
Output is correct |
70 |
Correct |
3 ms |
1236 KB |
Output is correct |
71 |
Correct |
105 ms |
22352 KB |
Output is correct |
72 |
Correct |
171 ms |
25032 KB |
Output is correct |
73 |
Correct |
1 ms |
340 KB |
Output is correct |
74 |
Correct |
139 ms |
31292 KB |
Output is correct |
75 |
Correct |
174 ms |
26828 KB |
Output is correct |
76 |
Correct |
2 ms |
980 KB |
Output is correct |
77 |
Correct |
3 ms |
980 KB |
Output is correct |
78 |
Correct |
85 ms |
22768 KB |
Output is correct |
79 |
Correct |
92 ms |
26612 KB |
Output is correct |
80 |
Correct |
3 ms |
1016 KB |
Output is correct |
81 |
Correct |
2 ms |
980 KB |
Output is correct |
82 |
Correct |
1 ms |
212 KB |
Output is correct |
83 |
Correct |
0 ms |
340 KB |
Output is correct |
84 |
Correct |
2 ms |
980 KB |
Output is correct |
85 |
Correct |
123 ms |
22888 KB |
Output is correct |
86 |
Correct |
2 ms |
980 KB |
Output is correct |
87 |
Correct |
121 ms |
22364 KB |
Output is correct |
88 |
Correct |
0 ms |
340 KB |
Output is correct |
89 |
Correct |
112 ms |
22232 KB |
Output is correct |
90 |
Correct |
123 ms |
23056 KB |
Output is correct |
91 |
Correct |
143 ms |
25996 KB |
Output is correct |
92 |
Correct |
85 ms |
22540 KB |
Output is correct |
93 |
Correct |
2 ms |
980 KB |
Output is correct |
94 |
Correct |
0 ms |
340 KB |
Output is correct |
95 |
Correct |
85 ms |
25740 KB |
Output is correct |
96 |
Correct |
140 ms |
30564 KB |
Output is correct |
97 |
Correct |
3 ms |
1024 KB |
Output is correct |
98 |
Correct |
1 ms |
340 KB |
Output is correct |
99 |
Correct |
121 ms |
23420 KB |
Output is correct |
100 |
Correct |
3 ms |
980 KB |
Output is correct |
101 |
Correct |
124 ms |
22496 KB |
Output is correct |
102 |
Correct |
201 ms |
28988 KB |
Output is correct |
103 |
Correct |
4 ms |
1108 KB |
Output is correct |
104 |
Correct |
3 ms |
1108 KB |
Output is correct |
105 |
Correct |
2 ms |
980 KB |
Output is correct |
106 |
Correct |
8 ms |
900 KB |
Output is correct |
107 |
Correct |
4 ms |
980 KB |
Output is correct |
108 |
Correct |
688 ms |
114204 KB |
Output is correct |
109 |
Correct |
1241 ms |
132076 KB |
Output is correct |
110 |
Correct |
1146 ms |
145516 KB |
Output is correct |
111 |
Correct |
1193 ms |
154572 KB |
Output is correct |
112 |
Correct |
1165 ms |
137904 KB |
Output is correct |
113 |
Correct |
604 ms |
127740 KB |
Output is correct |
114 |
Correct |
785 ms |
110452 KB |
Output is correct |
115 |
Correct |
783 ms |
110476 KB |
Output is correct |
116 |
Correct |
828 ms |
110900 KB |
Output is correct |
117 |
Correct |
774 ms |
128584 KB |
Output is correct |
118 |
Correct |
732 ms |
108296 KB |
Output is correct |
119 |
Correct |
819 ms |
128572 KB |
Output is correct |
120 |
Correct |
1109 ms |
144940 KB |
Output is correct |
121 |
Correct |
846 ms |
128476 KB |
Output is correct |
122 |
Correct |
844 ms |
114620 KB |
Output is correct |
123 |
Correct |
568 ms |
131392 KB |
Output is correct |
124 |
Correct |
984 ms |
120164 KB |
Output is correct |