#include<bits/stdc++.h>
using namespace std;
const int N = 5e5+1, logN = 19+1;
int n, k;
vector<int> g[N];
int s[N];
int p[logN][N], h[N];
void dfs_init(int v=0, int pr=-1){
for(int u : g[v]) if(u != pr){
p[0][u] = v;
h[u] = h[v]+1;
dfs_init(u, v);
}
}
void init(){
p[0][0] = 0;
h[0] = 0;
dfs_init();
for(int d = 1; d < logN; ++d)
for(int i = 0; i < n; ++i)
p[d][i] = p[d-1][p[d-1][i]];
}
int lca(int u, int v){
if(h[u] < h[v])
swap(u, v);
int d = h[u] - h[v];
for(int i = 0; i < logN; ++i)
if((d&(1<<i)))
u = p[i][u];
for(int i = logN-1; i >= 0; --i)
if(p[i][u] != p[i][v]){
u = p[i][u];
v = p[i][v];
}
return (u == v ? u : p[0][u]);
}
int color_lca[N];
int ans;
pair<int, int> dfs(int v=0, int pr=-1){
int w = color_lca[s[v]];
int deg = 0;
for(int u : g[v]) if(u != pr){
pair<int, int> l = dfs(u, v);
w = lca(l.first, w);
deg += l.second;
}
if(v == w){
if(v) ++deg;
if(deg == 1)
++ans;
return {w, 1};
}
return {w, deg};
}
int main(){
ios_base::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
cin >> n >> k;
for(int i = 0; i < n-1; ++i){
int u, v;
cin >> u >> v;
--u; --v;
g[u].push_back(v);
g[v].push_back(u);
}
for(int i = 0; i < n; ++i){
cin >> s[i];
--s[i];
}
init();
for(int i = 0; i < k; ++i)
color_lca[i] = -1;
for(int i = 0; i < n; ++i){
if(color_lca[s[i]] == -1)
color_lca[s[i]] = i;
color_lca[s[i]] = lca(i, color_lca[s[i]]);
}
dfs();
cout << (ans+1)/2 << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12268 KB |
Output is correct |
2 |
Correct |
10 ms |
12268 KB |
Output is correct |
3 |
Correct |
9 ms |
12268 KB |
Output is correct |
4 |
Correct |
9 ms |
12268 KB |
Output is correct |
5 |
Correct |
10 ms |
12268 KB |
Output is correct |
6 |
Correct |
9 ms |
12268 KB |
Output is correct |
7 |
Correct |
9 ms |
12268 KB |
Output is correct |
8 |
Correct |
9 ms |
12268 KB |
Output is correct |
9 |
Correct |
9 ms |
12268 KB |
Output is correct |
10 |
Correct |
9 ms |
12268 KB |
Output is correct |
11 |
Correct |
10 ms |
12300 KB |
Output is correct |
12 |
Correct |
9 ms |
12268 KB |
Output is correct |
13 |
Correct |
9 ms |
12268 KB |
Output is correct |
14 |
Correct |
9 ms |
12268 KB |
Output is correct |
15 |
Correct |
10 ms |
12396 KB |
Output is correct |
16 |
Correct |
9 ms |
12268 KB |
Output is correct |
17 |
Correct |
9 ms |
12268 KB |
Output is correct |
18 |
Correct |
9 ms |
12268 KB |
Output is correct |
19 |
Correct |
9 ms |
12268 KB |
Output is correct |
20 |
Correct |
10 ms |
12268 KB |
Output is correct |
21 |
Correct |
9 ms |
12268 KB |
Output is correct |
22 |
Correct |
9 ms |
12268 KB |
Output is correct |
23 |
Correct |
10 ms |
12268 KB |
Output is correct |
24 |
Correct |
9 ms |
12268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12268 KB |
Output is correct |
2 |
Correct |
10 ms |
12268 KB |
Output is correct |
3 |
Correct |
9 ms |
12268 KB |
Output is correct |
4 |
Correct |
9 ms |
12268 KB |
Output is correct |
5 |
Correct |
10 ms |
12268 KB |
Output is correct |
6 |
Correct |
9 ms |
12268 KB |
Output is correct |
7 |
Correct |
9 ms |
12268 KB |
Output is correct |
8 |
Correct |
9 ms |
12268 KB |
Output is correct |
9 |
Correct |
9 ms |
12268 KB |
Output is correct |
10 |
Correct |
9 ms |
12268 KB |
Output is correct |
11 |
Correct |
10 ms |
12300 KB |
Output is correct |
12 |
Correct |
9 ms |
12268 KB |
Output is correct |
13 |
Correct |
9 ms |
12268 KB |
Output is correct |
14 |
Correct |
9 ms |
12268 KB |
Output is correct |
15 |
Correct |
10 ms |
12396 KB |
Output is correct |
16 |
Correct |
9 ms |
12268 KB |
Output is correct |
17 |
Correct |
9 ms |
12268 KB |
Output is correct |
18 |
Correct |
9 ms |
12268 KB |
Output is correct |
19 |
Correct |
9 ms |
12268 KB |
Output is correct |
20 |
Correct |
10 ms |
12268 KB |
Output is correct |
21 |
Correct |
9 ms |
12268 KB |
Output is correct |
22 |
Correct |
9 ms |
12268 KB |
Output is correct |
23 |
Correct |
10 ms |
12268 KB |
Output is correct |
24 |
Correct |
9 ms |
12268 KB |
Output is correct |
25 |
Correct |
9 ms |
12268 KB |
Output is correct |
26 |
Correct |
14 ms |
12728 KB |
Output is correct |
27 |
Correct |
11 ms |
12652 KB |
Output is correct |
28 |
Correct |
11 ms |
12780 KB |
Output is correct |
29 |
Correct |
12 ms |
12652 KB |
Output is correct |
30 |
Correct |
12 ms |
12652 KB |
Output is correct |
31 |
Correct |
10 ms |
12268 KB |
Output is correct |
32 |
Correct |
12 ms |
12780 KB |
Output is correct |
33 |
Correct |
10 ms |
12268 KB |
Output is correct |
34 |
Correct |
11 ms |
12652 KB |
Output is correct |
35 |
Correct |
11 ms |
12652 KB |
Output is correct |
36 |
Correct |
11 ms |
12652 KB |
Output is correct |
37 |
Correct |
11 ms |
12652 KB |
Output is correct |
38 |
Correct |
11 ms |
12268 KB |
Output is correct |
39 |
Correct |
12 ms |
12652 KB |
Output is correct |
40 |
Correct |
11 ms |
12652 KB |
Output is correct |
41 |
Correct |
13 ms |
12652 KB |
Output is correct |
42 |
Correct |
11 ms |
12652 KB |
Output is correct |
43 |
Correct |
16 ms |
12780 KB |
Output is correct |
44 |
Correct |
9 ms |
12268 KB |
Output is correct |
45 |
Correct |
11 ms |
12780 KB |
Output is correct |
46 |
Correct |
12 ms |
12652 KB |
Output is correct |
47 |
Correct |
10 ms |
12268 KB |
Output is correct |
48 |
Correct |
11 ms |
12652 KB |
Output is correct |
49 |
Correct |
11 ms |
12652 KB |
Output is correct |
50 |
Correct |
11 ms |
12652 KB |
Output is correct |
51 |
Correct |
12 ms |
12652 KB |
Output is correct |
52 |
Correct |
11 ms |
12652 KB |
Output is correct |
53 |
Correct |
11 ms |
12652 KB |
Output is correct |
54 |
Correct |
11 ms |
12652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12268 KB |
Output is correct |
2 |
Correct |
10 ms |
12268 KB |
Output is correct |
3 |
Correct |
9 ms |
12268 KB |
Output is correct |
4 |
Correct |
9 ms |
12268 KB |
Output is correct |
5 |
Correct |
10 ms |
12268 KB |
Output is correct |
6 |
Correct |
9 ms |
12268 KB |
Output is correct |
7 |
Correct |
9 ms |
12268 KB |
Output is correct |
8 |
Correct |
9 ms |
12268 KB |
Output is correct |
9 |
Correct |
9 ms |
12268 KB |
Output is correct |
10 |
Correct |
9 ms |
12268 KB |
Output is correct |
11 |
Correct |
10 ms |
12300 KB |
Output is correct |
12 |
Correct |
9 ms |
12268 KB |
Output is correct |
13 |
Correct |
9 ms |
12268 KB |
Output is correct |
14 |
Correct |
9 ms |
12268 KB |
Output is correct |
15 |
Correct |
10 ms |
12396 KB |
Output is correct |
16 |
Correct |
9 ms |
12268 KB |
Output is correct |
17 |
Correct |
9 ms |
12268 KB |
Output is correct |
18 |
Correct |
9 ms |
12268 KB |
Output is correct |
19 |
Correct |
9 ms |
12268 KB |
Output is correct |
20 |
Correct |
10 ms |
12268 KB |
Output is correct |
21 |
Correct |
9 ms |
12268 KB |
Output is correct |
22 |
Correct |
9 ms |
12268 KB |
Output is correct |
23 |
Correct |
10 ms |
12268 KB |
Output is correct |
24 |
Correct |
9 ms |
12268 KB |
Output is correct |
25 |
Correct |
10 ms |
12268 KB |
Output is correct |
26 |
Correct |
96 ms |
25316 KB |
Output is correct |
27 |
Correct |
142 ms |
25068 KB |
Output is correct |
28 |
Correct |
12 ms |
12652 KB |
Output is correct |
29 |
Correct |
9 ms |
12268 KB |
Output is correct |
30 |
Correct |
12 ms |
12268 KB |
Output is correct |
31 |
Correct |
139 ms |
25096 KB |
Output is correct |
32 |
Correct |
13 ms |
12652 KB |
Output is correct |
33 |
Correct |
183 ms |
30220 KB |
Output is correct |
34 |
Correct |
140 ms |
24684 KB |
Output is correct |
35 |
Correct |
12 ms |
12652 KB |
Output is correct |
36 |
Correct |
162 ms |
25068 KB |
Output is correct |
37 |
Correct |
11 ms |
12652 KB |
Output is correct |
38 |
Correct |
12 ms |
12652 KB |
Output is correct |
39 |
Correct |
86 ms |
24804 KB |
Output is correct |
40 |
Correct |
11 ms |
12780 KB |
Output is correct |
41 |
Correct |
132 ms |
24684 KB |
Output is correct |
42 |
Correct |
162 ms |
26476 KB |
Output is correct |
43 |
Correct |
9 ms |
12268 KB |
Output is correct |
44 |
Correct |
160 ms |
31340 KB |
Output is correct |
45 |
Correct |
146 ms |
28268 KB |
Output is correct |
46 |
Correct |
11 ms |
12652 KB |
Output is correct |
47 |
Correct |
13 ms |
12652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
25572 KB |
Output is correct |
2 |
Correct |
96 ms |
25956 KB |
Output is correct |
3 |
Correct |
11 ms |
12652 KB |
Output is correct |
4 |
Correct |
11 ms |
12652 KB |
Output is correct |
5 |
Correct |
9 ms |
12268 KB |
Output is correct |
6 |
Correct |
9 ms |
12268 KB |
Output is correct |
7 |
Correct |
11 ms |
12652 KB |
Output is correct |
8 |
Correct |
244 ms |
24768 KB |
Output is correct |
9 |
Correct |
11 ms |
12652 KB |
Output is correct |
10 |
Correct |
197 ms |
25196 KB |
Output is correct |
11 |
Correct |
9 ms |
12396 KB |
Output is correct |
12 |
Correct |
241 ms |
24556 KB |
Output is correct |
13 |
Correct |
245 ms |
24696 KB |
Output is correct |
14 |
Correct |
245 ms |
25708 KB |
Output is correct |
15 |
Correct |
89 ms |
25828 KB |
Output is correct |
16 |
Correct |
12 ms |
12652 KB |
Output is correct |
17 |
Correct |
9 ms |
12268 KB |
Output is correct |
18 |
Correct |
108 ms |
25956 KB |
Output is correct |
19 |
Correct |
317 ms |
28908 KB |
Output is correct |
20 |
Correct |
11 ms |
12652 KB |
Output is correct |
21 |
Correct |
9 ms |
12268 KB |
Output is correct |
22 |
Correct |
136 ms |
25828 KB |
Output is correct |
23 |
Correct |
11 ms |
12652 KB |
Output is correct |
24 |
Correct |
264 ms |
25324 KB |
Output is correct |
25 |
Correct |
292 ms |
27884 KB |
Output is correct |
26 |
Correct |
11 ms |
12652 KB |
Output is correct |
27 |
Correct |
11 ms |
12764 KB |
Output is correct |
28 |
Correct |
13 ms |
12652 KB |
Output is correct |
29 |
Correct |
12 ms |
12652 KB |
Output is correct |
30 |
Correct |
12 ms |
12652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12268 KB |
Output is correct |
2 |
Correct |
10 ms |
12268 KB |
Output is correct |
3 |
Correct |
9 ms |
12268 KB |
Output is correct |
4 |
Correct |
9 ms |
12268 KB |
Output is correct |
5 |
Correct |
10 ms |
12268 KB |
Output is correct |
6 |
Correct |
9 ms |
12268 KB |
Output is correct |
7 |
Correct |
9 ms |
12268 KB |
Output is correct |
8 |
Correct |
9 ms |
12268 KB |
Output is correct |
9 |
Correct |
9 ms |
12268 KB |
Output is correct |
10 |
Correct |
9 ms |
12268 KB |
Output is correct |
11 |
Correct |
10 ms |
12300 KB |
Output is correct |
12 |
Correct |
9 ms |
12268 KB |
Output is correct |
13 |
Correct |
9 ms |
12268 KB |
Output is correct |
14 |
Correct |
9 ms |
12268 KB |
Output is correct |
15 |
Correct |
10 ms |
12396 KB |
Output is correct |
16 |
Correct |
9 ms |
12268 KB |
Output is correct |
17 |
Correct |
9 ms |
12268 KB |
Output is correct |
18 |
Correct |
9 ms |
12268 KB |
Output is correct |
19 |
Correct |
9 ms |
12268 KB |
Output is correct |
20 |
Correct |
10 ms |
12268 KB |
Output is correct |
21 |
Correct |
9 ms |
12268 KB |
Output is correct |
22 |
Correct |
9 ms |
12268 KB |
Output is correct |
23 |
Correct |
10 ms |
12268 KB |
Output is correct |
24 |
Correct |
9 ms |
12268 KB |
Output is correct |
25 |
Correct |
9 ms |
12268 KB |
Output is correct |
26 |
Correct |
14 ms |
12728 KB |
Output is correct |
27 |
Correct |
11 ms |
12652 KB |
Output is correct |
28 |
Correct |
11 ms |
12780 KB |
Output is correct |
29 |
Correct |
12 ms |
12652 KB |
Output is correct |
30 |
Correct |
12 ms |
12652 KB |
Output is correct |
31 |
Correct |
10 ms |
12268 KB |
Output is correct |
32 |
Correct |
12 ms |
12780 KB |
Output is correct |
33 |
Correct |
10 ms |
12268 KB |
Output is correct |
34 |
Correct |
11 ms |
12652 KB |
Output is correct |
35 |
Correct |
11 ms |
12652 KB |
Output is correct |
36 |
Correct |
11 ms |
12652 KB |
Output is correct |
37 |
Correct |
11 ms |
12652 KB |
Output is correct |
38 |
Correct |
11 ms |
12268 KB |
Output is correct |
39 |
Correct |
12 ms |
12652 KB |
Output is correct |
40 |
Correct |
11 ms |
12652 KB |
Output is correct |
41 |
Correct |
13 ms |
12652 KB |
Output is correct |
42 |
Correct |
11 ms |
12652 KB |
Output is correct |
43 |
Correct |
16 ms |
12780 KB |
Output is correct |
44 |
Correct |
9 ms |
12268 KB |
Output is correct |
45 |
Correct |
11 ms |
12780 KB |
Output is correct |
46 |
Correct |
12 ms |
12652 KB |
Output is correct |
47 |
Correct |
10 ms |
12268 KB |
Output is correct |
48 |
Correct |
11 ms |
12652 KB |
Output is correct |
49 |
Correct |
11 ms |
12652 KB |
Output is correct |
50 |
Correct |
11 ms |
12652 KB |
Output is correct |
51 |
Correct |
12 ms |
12652 KB |
Output is correct |
52 |
Correct |
11 ms |
12652 KB |
Output is correct |
53 |
Correct |
11 ms |
12652 KB |
Output is correct |
54 |
Correct |
11 ms |
12652 KB |
Output is correct |
55 |
Correct |
10 ms |
12268 KB |
Output is correct |
56 |
Correct |
96 ms |
25316 KB |
Output is correct |
57 |
Correct |
142 ms |
25068 KB |
Output is correct |
58 |
Correct |
12 ms |
12652 KB |
Output is correct |
59 |
Correct |
9 ms |
12268 KB |
Output is correct |
60 |
Correct |
12 ms |
12268 KB |
Output is correct |
61 |
Correct |
139 ms |
25096 KB |
Output is correct |
62 |
Correct |
13 ms |
12652 KB |
Output is correct |
63 |
Correct |
183 ms |
30220 KB |
Output is correct |
64 |
Correct |
140 ms |
24684 KB |
Output is correct |
65 |
Correct |
12 ms |
12652 KB |
Output is correct |
66 |
Correct |
162 ms |
25068 KB |
Output is correct |
67 |
Correct |
11 ms |
12652 KB |
Output is correct |
68 |
Correct |
12 ms |
12652 KB |
Output is correct |
69 |
Correct |
86 ms |
24804 KB |
Output is correct |
70 |
Correct |
11 ms |
12780 KB |
Output is correct |
71 |
Correct |
132 ms |
24684 KB |
Output is correct |
72 |
Correct |
162 ms |
26476 KB |
Output is correct |
73 |
Correct |
9 ms |
12268 KB |
Output is correct |
74 |
Correct |
160 ms |
31340 KB |
Output is correct |
75 |
Correct |
146 ms |
28268 KB |
Output is correct |
76 |
Correct |
11 ms |
12652 KB |
Output is correct |
77 |
Correct |
13 ms |
12652 KB |
Output is correct |
78 |
Correct |
85 ms |
25572 KB |
Output is correct |
79 |
Correct |
96 ms |
25956 KB |
Output is correct |
80 |
Correct |
11 ms |
12652 KB |
Output is correct |
81 |
Correct |
11 ms |
12652 KB |
Output is correct |
82 |
Correct |
9 ms |
12268 KB |
Output is correct |
83 |
Correct |
9 ms |
12268 KB |
Output is correct |
84 |
Correct |
11 ms |
12652 KB |
Output is correct |
85 |
Correct |
244 ms |
24768 KB |
Output is correct |
86 |
Correct |
11 ms |
12652 KB |
Output is correct |
87 |
Correct |
197 ms |
25196 KB |
Output is correct |
88 |
Correct |
9 ms |
12396 KB |
Output is correct |
89 |
Correct |
241 ms |
24556 KB |
Output is correct |
90 |
Correct |
245 ms |
24696 KB |
Output is correct |
91 |
Correct |
245 ms |
25708 KB |
Output is correct |
92 |
Correct |
89 ms |
25828 KB |
Output is correct |
93 |
Correct |
12 ms |
12652 KB |
Output is correct |
94 |
Correct |
9 ms |
12268 KB |
Output is correct |
95 |
Correct |
108 ms |
25956 KB |
Output is correct |
96 |
Correct |
317 ms |
28908 KB |
Output is correct |
97 |
Correct |
11 ms |
12652 KB |
Output is correct |
98 |
Correct |
9 ms |
12268 KB |
Output is correct |
99 |
Correct |
136 ms |
25828 KB |
Output is correct |
100 |
Correct |
11 ms |
12652 KB |
Output is correct |
101 |
Correct |
264 ms |
25324 KB |
Output is correct |
102 |
Correct |
292 ms |
27884 KB |
Output is correct |
103 |
Correct |
11 ms |
12652 KB |
Output is correct |
104 |
Correct |
11 ms |
12764 KB |
Output is correct |
105 |
Correct |
13 ms |
12652 KB |
Output is correct |
106 |
Correct |
12 ms |
12652 KB |
Output is correct |
107 |
Correct |
12 ms |
12652 KB |
Output is correct |
108 |
Correct |
944 ms |
74852 KB |
Output is correct |
109 |
Correct |
1043 ms |
87020 KB |
Output is correct |
110 |
Correct |
1035 ms |
96112 KB |
Output is correct |
111 |
Correct |
1994 ms |
93164 KB |
Output is correct |
112 |
Correct |
1200 ms |
85100 KB |
Output is correct |
113 |
Correct |
712 ms |
75860 KB |
Output is correct |
114 |
Correct |
837 ms |
72724 KB |
Output is correct |
115 |
Correct |
805 ms |
72812 KB |
Output is correct |
116 |
Correct |
1390 ms |
72940 KB |
Output is correct |
117 |
Correct |
1393 ms |
74372 KB |
Output is correct |
118 |
Correct |
1124 ms |
71404 KB |
Output is correct |
119 |
Correct |
1387 ms |
74348 KB |
Output is correct |
120 |
Correct |
1642 ms |
85216 KB |
Output is correct |
121 |
Correct |
1373 ms |
74092 KB |
Output is correct |
122 |
Correct |
1320 ms |
73452 KB |
Output is correct |
123 |
Correct |
633 ms |
75484 KB |
Output is correct |
124 |
Correct |
1617 ms |
72796 KB |
Output is correct |