#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
int n, k;
const int MAX_N = 500'000;
const int MAX_K = 500'000;
const int INF = 1e18;
int res = 0;
vector<int> adj[MAX_N];
vector<int> mem[MAX_N];
int group[MAX_N];
int anc[MAX_N];
int group_lca[MAX_K];
int depth[MAX_N];
void dfs(int u, int a, int d){
//cout<<"dfs "<<u<<endl;
anc[u] = a;
depth[u] = d;
for(auto v: adj[u]){
if(v!=a){
dfs(v, u, d+1);
}
}
}
int bl[19][MAX_N];
void build_bl(){
for(int i = 0; i<n; i++){
bl[0][i] =anc[i];
}
for(int i = 1; i<19; i++){
for(int j = 0; j<n; j++){
bl[i][j] = bl[i-1][bl[i-1][j]];
}
}
}
int lca(int a, int b){
if(a==-1){
return b;
}
if(b==-1){
return a;
}
if(depth[a]>depth[b]){
return lca(b, a);
}
for(int step = 18; step>=0; step--){
if(depth[bl[step][b]]>=depth[a]){
b = bl[step][b];
}
}
if(a==b){
return a;
}
for(int step = 18; step>=0; step--){
if(bl[step][a] != bl[step][b]){
a = bl[step][a];
b= bl[step][b];
}
}
return bl[0][a];
}
bool is_free[MAX_N];
int nbf = 0;
int dfs2(int u){
int min_d = INF;
min_d= min(min_d, depth[group_lca[group[u]]]);
for(auto v: adj[u]){
if(v!= anc[u]){
min_d= min(min_d, dfs2(v));
}
}
if(min_d >= depth[u]){
is_free[u] = true;
nbf++;
}
return min_d;
}
pii dfs3(int u){
pii s= pii(0, 0);
for(auto v: adj[u]){
if(v!=anc[u]){
auto r=dfs3(v);
s.first += r.first;
s.second += r.second;
}
}
if((s.second == nbf-1|| s.first==0) && is_free[u]){
res++;
}
//cout<<3<<" "<<u<<" "<<s<<endl;
if(is_free[u]){
s.first = 1;
s.second++;
}
return s;
}
signed main(){
cin>>n>>k;
fill(group_lca, group_lca + MAX_K, -1);
for(int i = 0; i<n-1; i++){
int a, b;
cin>>a>>b;
adj[a-1].push_back(b-1);
adj[b-1].push_back(a-1);
}
dfs(0, 0, 0);
build_bl();
for(int i = 0; i<n; i++){
cin>>group[i];
group[i]--;
}
for(int i = 0; i<n; i++){
group_lca[group[i]] = lca(group_lca[group[i]], i);
}
dfs2(0);
is_free[0] = false;
nbf--;
dfs3(0);
cout<<(res+1)/2<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
73048 KB |
Output is correct |
2 |
Correct |
11 ms |
73052 KB |
Output is correct |
3 |
Correct |
12 ms |
73048 KB |
Output is correct |
4 |
Correct |
10 ms |
73052 KB |
Output is correct |
5 |
Correct |
11 ms |
73116 KB |
Output is correct |
6 |
Correct |
11 ms |
73052 KB |
Output is correct |
7 |
Correct |
11 ms |
73052 KB |
Output is correct |
8 |
Correct |
11 ms |
73052 KB |
Output is correct |
9 |
Correct |
11 ms |
73052 KB |
Output is correct |
10 |
Correct |
10 ms |
73052 KB |
Output is correct |
11 |
Correct |
11 ms |
73048 KB |
Output is correct |
12 |
Correct |
11 ms |
73052 KB |
Output is correct |
13 |
Correct |
10 ms |
73052 KB |
Output is correct |
14 |
Correct |
11 ms |
73048 KB |
Output is correct |
15 |
Correct |
11 ms |
73052 KB |
Output is correct |
16 |
Correct |
11 ms |
73052 KB |
Output is correct |
17 |
Correct |
10 ms |
73048 KB |
Output is correct |
18 |
Correct |
11 ms |
73052 KB |
Output is correct |
19 |
Correct |
11 ms |
73052 KB |
Output is correct |
20 |
Correct |
11 ms |
73304 KB |
Output is correct |
21 |
Correct |
11 ms |
73184 KB |
Output is correct |
22 |
Correct |
12 ms |
73052 KB |
Output is correct |
23 |
Correct |
10 ms |
73052 KB |
Output is correct |
24 |
Correct |
10 ms |
73052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
73048 KB |
Output is correct |
2 |
Correct |
11 ms |
73052 KB |
Output is correct |
3 |
Correct |
12 ms |
73048 KB |
Output is correct |
4 |
Correct |
10 ms |
73052 KB |
Output is correct |
5 |
Correct |
11 ms |
73116 KB |
Output is correct |
6 |
Correct |
11 ms |
73052 KB |
Output is correct |
7 |
Correct |
11 ms |
73052 KB |
Output is correct |
8 |
Correct |
11 ms |
73052 KB |
Output is correct |
9 |
Correct |
11 ms |
73052 KB |
Output is correct |
10 |
Correct |
10 ms |
73052 KB |
Output is correct |
11 |
Correct |
11 ms |
73048 KB |
Output is correct |
12 |
Correct |
11 ms |
73052 KB |
Output is correct |
13 |
Correct |
10 ms |
73052 KB |
Output is correct |
14 |
Correct |
11 ms |
73048 KB |
Output is correct |
15 |
Correct |
11 ms |
73052 KB |
Output is correct |
16 |
Correct |
11 ms |
73052 KB |
Output is correct |
17 |
Correct |
10 ms |
73048 KB |
Output is correct |
18 |
Correct |
11 ms |
73052 KB |
Output is correct |
19 |
Correct |
11 ms |
73052 KB |
Output is correct |
20 |
Correct |
11 ms |
73304 KB |
Output is correct |
21 |
Correct |
11 ms |
73184 KB |
Output is correct |
22 |
Correct |
12 ms |
73052 KB |
Output is correct |
23 |
Correct |
10 ms |
73052 KB |
Output is correct |
24 |
Correct |
10 ms |
73052 KB |
Output is correct |
25 |
Correct |
11 ms |
73204 KB |
Output is correct |
26 |
Correct |
13 ms |
73360 KB |
Output is correct |
27 |
Correct |
13 ms |
73308 KB |
Output is correct |
28 |
Correct |
13 ms |
73416 KB |
Output is correct |
29 |
Correct |
14 ms |
73308 KB |
Output is correct |
30 |
Correct |
12 ms |
73308 KB |
Output is correct |
31 |
Correct |
11 ms |
73052 KB |
Output is correct |
32 |
Correct |
13 ms |
73304 KB |
Output is correct |
33 |
Correct |
10 ms |
73048 KB |
Output is correct |
34 |
Correct |
12 ms |
73308 KB |
Output is correct |
35 |
Correct |
13 ms |
73308 KB |
Output is correct |
36 |
Correct |
14 ms |
73308 KB |
Output is correct |
37 |
Correct |
13 ms |
73304 KB |
Output is correct |
38 |
Correct |
12 ms |
73052 KB |
Output is correct |
39 |
Correct |
13 ms |
73308 KB |
Output is correct |
40 |
Correct |
14 ms |
73308 KB |
Output is correct |
41 |
Correct |
13 ms |
73312 KB |
Output is correct |
42 |
Correct |
12 ms |
73304 KB |
Output is correct |
43 |
Correct |
13 ms |
73304 KB |
Output is correct |
44 |
Correct |
11 ms |
73052 KB |
Output is correct |
45 |
Correct |
13 ms |
73308 KB |
Output is correct |
46 |
Correct |
13 ms |
73308 KB |
Output is correct |
47 |
Correct |
11 ms |
73052 KB |
Output is correct |
48 |
Correct |
13 ms |
73308 KB |
Output is correct |
49 |
Correct |
12 ms |
73308 KB |
Output is correct |
50 |
Correct |
12 ms |
73308 KB |
Output is correct |
51 |
Correct |
15 ms |
73140 KB |
Output is correct |
52 |
Correct |
12 ms |
73140 KB |
Output is correct |
53 |
Correct |
12 ms |
73308 KB |
Output is correct |
54 |
Correct |
13 ms |
73304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
73048 KB |
Output is correct |
2 |
Correct |
11 ms |
73052 KB |
Output is correct |
3 |
Correct |
12 ms |
73048 KB |
Output is correct |
4 |
Correct |
10 ms |
73052 KB |
Output is correct |
5 |
Correct |
11 ms |
73116 KB |
Output is correct |
6 |
Correct |
11 ms |
73052 KB |
Output is correct |
7 |
Correct |
11 ms |
73052 KB |
Output is correct |
8 |
Correct |
11 ms |
73052 KB |
Output is correct |
9 |
Correct |
11 ms |
73052 KB |
Output is correct |
10 |
Correct |
10 ms |
73052 KB |
Output is correct |
11 |
Correct |
11 ms |
73048 KB |
Output is correct |
12 |
Correct |
11 ms |
73052 KB |
Output is correct |
13 |
Correct |
10 ms |
73052 KB |
Output is correct |
14 |
Correct |
11 ms |
73048 KB |
Output is correct |
15 |
Correct |
11 ms |
73052 KB |
Output is correct |
16 |
Correct |
11 ms |
73052 KB |
Output is correct |
17 |
Correct |
10 ms |
73048 KB |
Output is correct |
18 |
Correct |
11 ms |
73052 KB |
Output is correct |
19 |
Correct |
11 ms |
73052 KB |
Output is correct |
20 |
Correct |
11 ms |
73304 KB |
Output is correct |
21 |
Correct |
11 ms |
73184 KB |
Output is correct |
22 |
Correct |
12 ms |
73052 KB |
Output is correct |
23 |
Correct |
10 ms |
73052 KB |
Output is correct |
24 |
Correct |
10 ms |
73052 KB |
Output is correct |
25 |
Correct |
11 ms |
73052 KB |
Output is correct |
26 |
Correct |
80 ms |
94748 KB |
Output is correct |
27 |
Correct |
96 ms |
94632 KB |
Output is correct |
28 |
Correct |
14 ms |
73304 KB |
Output is correct |
29 |
Correct |
11 ms |
73048 KB |
Output is correct |
30 |
Correct |
11 ms |
73304 KB |
Output is correct |
31 |
Correct |
102 ms |
94804 KB |
Output is correct |
32 |
Correct |
13 ms |
73304 KB |
Output is correct |
33 |
Correct |
122 ms |
99924 KB |
Output is correct |
34 |
Correct |
98 ms |
94556 KB |
Output is correct |
35 |
Correct |
13 ms |
73308 KB |
Output is correct |
36 |
Correct |
119 ms |
94804 KB |
Output is correct |
37 |
Correct |
14 ms |
73304 KB |
Output is correct |
38 |
Correct |
13 ms |
73304 KB |
Output is correct |
39 |
Correct |
80 ms |
94788 KB |
Output is correct |
40 |
Correct |
13 ms |
73304 KB |
Output is correct |
41 |
Correct |
94 ms |
94548 KB |
Output is correct |
42 |
Correct |
104 ms |
95884 KB |
Output is correct |
43 |
Correct |
10 ms |
73048 KB |
Output is correct |
44 |
Correct |
100 ms |
99916 KB |
Output is correct |
45 |
Correct |
107 ms |
98552 KB |
Output is correct |
46 |
Correct |
14 ms |
73308 KB |
Output is correct |
47 |
Correct |
13 ms |
73308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
94844 KB |
Output is correct |
2 |
Correct |
85 ms |
95176 KB |
Output is correct |
3 |
Correct |
13 ms |
73308 KB |
Output is correct |
4 |
Correct |
12 ms |
73308 KB |
Output is correct |
5 |
Correct |
13 ms |
73156 KB |
Output is correct |
6 |
Correct |
11 ms |
73048 KB |
Output is correct |
7 |
Correct |
14 ms |
73308 KB |
Output is correct |
8 |
Correct |
131 ms |
94952 KB |
Output is correct |
9 |
Correct |
13 ms |
73304 KB |
Output is correct |
10 |
Correct |
114 ms |
94800 KB |
Output is correct |
11 |
Correct |
10 ms |
73052 KB |
Output is correct |
12 |
Correct |
115 ms |
94384 KB |
Output is correct |
13 |
Correct |
173 ms |
94840 KB |
Output is correct |
14 |
Correct |
102 ms |
95140 KB |
Output is correct |
15 |
Correct |
86 ms |
94788 KB |
Output is correct |
16 |
Correct |
13 ms |
73308 KB |
Output is correct |
17 |
Correct |
10 ms |
73116 KB |
Output is correct |
18 |
Correct |
102 ms |
95244 KB |
Output is correct |
19 |
Correct |
107 ms |
97720 KB |
Output is correct |
20 |
Correct |
13 ms |
73304 KB |
Output is correct |
21 |
Correct |
11 ms |
73052 KB |
Output is correct |
22 |
Correct |
106 ms |
95264 KB |
Output is correct |
23 |
Correct |
13 ms |
73308 KB |
Output is correct |
24 |
Correct |
121 ms |
94644 KB |
Output is correct |
25 |
Correct |
98 ms |
97692 KB |
Output is correct |
26 |
Correct |
12 ms |
73308 KB |
Output is correct |
27 |
Correct |
13 ms |
73304 KB |
Output is correct |
28 |
Correct |
13 ms |
73308 KB |
Output is correct |
29 |
Correct |
13 ms |
73304 KB |
Output is correct |
30 |
Correct |
13 ms |
73304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
73048 KB |
Output is correct |
2 |
Correct |
11 ms |
73052 KB |
Output is correct |
3 |
Correct |
12 ms |
73048 KB |
Output is correct |
4 |
Correct |
10 ms |
73052 KB |
Output is correct |
5 |
Correct |
11 ms |
73116 KB |
Output is correct |
6 |
Correct |
11 ms |
73052 KB |
Output is correct |
7 |
Correct |
11 ms |
73052 KB |
Output is correct |
8 |
Correct |
11 ms |
73052 KB |
Output is correct |
9 |
Correct |
11 ms |
73052 KB |
Output is correct |
10 |
Correct |
10 ms |
73052 KB |
Output is correct |
11 |
Correct |
11 ms |
73048 KB |
Output is correct |
12 |
Correct |
11 ms |
73052 KB |
Output is correct |
13 |
Correct |
10 ms |
73052 KB |
Output is correct |
14 |
Correct |
11 ms |
73048 KB |
Output is correct |
15 |
Correct |
11 ms |
73052 KB |
Output is correct |
16 |
Correct |
11 ms |
73052 KB |
Output is correct |
17 |
Correct |
10 ms |
73048 KB |
Output is correct |
18 |
Correct |
11 ms |
73052 KB |
Output is correct |
19 |
Correct |
11 ms |
73052 KB |
Output is correct |
20 |
Correct |
11 ms |
73304 KB |
Output is correct |
21 |
Correct |
11 ms |
73184 KB |
Output is correct |
22 |
Correct |
12 ms |
73052 KB |
Output is correct |
23 |
Correct |
10 ms |
73052 KB |
Output is correct |
24 |
Correct |
10 ms |
73052 KB |
Output is correct |
25 |
Correct |
11 ms |
73204 KB |
Output is correct |
26 |
Correct |
13 ms |
73360 KB |
Output is correct |
27 |
Correct |
13 ms |
73308 KB |
Output is correct |
28 |
Correct |
13 ms |
73416 KB |
Output is correct |
29 |
Correct |
14 ms |
73308 KB |
Output is correct |
30 |
Correct |
12 ms |
73308 KB |
Output is correct |
31 |
Correct |
11 ms |
73052 KB |
Output is correct |
32 |
Correct |
13 ms |
73304 KB |
Output is correct |
33 |
Correct |
10 ms |
73048 KB |
Output is correct |
34 |
Correct |
12 ms |
73308 KB |
Output is correct |
35 |
Correct |
13 ms |
73308 KB |
Output is correct |
36 |
Correct |
14 ms |
73308 KB |
Output is correct |
37 |
Correct |
13 ms |
73304 KB |
Output is correct |
38 |
Correct |
12 ms |
73052 KB |
Output is correct |
39 |
Correct |
13 ms |
73308 KB |
Output is correct |
40 |
Correct |
14 ms |
73308 KB |
Output is correct |
41 |
Correct |
13 ms |
73312 KB |
Output is correct |
42 |
Correct |
12 ms |
73304 KB |
Output is correct |
43 |
Correct |
13 ms |
73304 KB |
Output is correct |
44 |
Correct |
11 ms |
73052 KB |
Output is correct |
45 |
Correct |
13 ms |
73308 KB |
Output is correct |
46 |
Correct |
13 ms |
73308 KB |
Output is correct |
47 |
Correct |
11 ms |
73052 KB |
Output is correct |
48 |
Correct |
13 ms |
73308 KB |
Output is correct |
49 |
Correct |
12 ms |
73308 KB |
Output is correct |
50 |
Correct |
12 ms |
73308 KB |
Output is correct |
51 |
Correct |
15 ms |
73140 KB |
Output is correct |
52 |
Correct |
12 ms |
73140 KB |
Output is correct |
53 |
Correct |
12 ms |
73308 KB |
Output is correct |
54 |
Correct |
13 ms |
73304 KB |
Output is correct |
55 |
Correct |
11 ms |
73052 KB |
Output is correct |
56 |
Correct |
80 ms |
94748 KB |
Output is correct |
57 |
Correct |
96 ms |
94632 KB |
Output is correct |
58 |
Correct |
14 ms |
73304 KB |
Output is correct |
59 |
Correct |
11 ms |
73048 KB |
Output is correct |
60 |
Correct |
11 ms |
73304 KB |
Output is correct |
61 |
Correct |
102 ms |
94804 KB |
Output is correct |
62 |
Correct |
13 ms |
73304 KB |
Output is correct |
63 |
Correct |
122 ms |
99924 KB |
Output is correct |
64 |
Correct |
98 ms |
94556 KB |
Output is correct |
65 |
Correct |
13 ms |
73308 KB |
Output is correct |
66 |
Correct |
119 ms |
94804 KB |
Output is correct |
67 |
Correct |
14 ms |
73304 KB |
Output is correct |
68 |
Correct |
13 ms |
73304 KB |
Output is correct |
69 |
Correct |
80 ms |
94788 KB |
Output is correct |
70 |
Correct |
13 ms |
73304 KB |
Output is correct |
71 |
Correct |
94 ms |
94548 KB |
Output is correct |
72 |
Correct |
104 ms |
95884 KB |
Output is correct |
73 |
Correct |
10 ms |
73048 KB |
Output is correct |
74 |
Correct |
100 ms |
99916 KB |
Output is correct |
75 |
Correct |
107 ms |
98552 KB |
Output is correct |
76 |
Correct |
14 ms |
73308 KB |
Output is correct |
77 |
Correct |
13 ms |
73308 KB |
Output is correct |
78 |
Correct |
80 ms |
94844 KB |
Output is correct |
79 |
Correct |
85 ms |
95176 KB |
Output is correct |
80 |
Correct |
13 ms |
73308 KB |
Output is correct |
81 |
Correct |
12 ms |
73308 KB |
Output is correct |
82 |
Correct |
13 ms |
73156 KB |
Output is correct |
83 |
Correct |
11 ms |
73048 KB |
Output is correct |
84 |
Correct |
14 ms |
73308 KB |
Output is correct |
85 |
Correct |
131 ms |
94952 KB |
Output is correct |
86 |
Correct |
13 ms |
73304 KB |
Output is correct |
87 |
Correct |
114 ms |
94800 KB |
Output is correct |
88 |
Correct |
10 ms |
73052 KB |
Output is correct |
89 |
Correct |
115 ms |
94384 KB |
Output is correct |
90 |
Correct |
173 ms |
94840 KB |
Output is correct |
91 |
Correct |
102 ms |
95140 KB |
Output is correct |
92 |
Correct |
86 ms |
94788 KB |
Output is correct |
93 |
Correct |
13 ms |
73308 KB |
Output is correct |
94 |
Correct |
10 ms |
73116 KB |
Output is correct |
95 |
Correct |
102 ms |
95244 KB |
Output is correct |
96 |
Correct |
107 ms |
97720 KB |
Output is correct |
97 |
Correct |
13 ms |
73304 KB |
Output is correct |
98 |
Correct |
11 ms |
73052 KB |
Output is correct |
99 |
Correct |
106 ms |
95264 KB |
Output is correct |
100 |
Correct |
13 ms |
73308 KB |
Output is correct |
101 |
Correct |
121 ms |
94644 KB |
Output is correct |
102 |
Correct |
98 ms |
97692 KB |
Output is correct |
103 |
Correct |
12 ms |
73308 KB |
Output is correct |
104 |
Correct |
13 ms |
73304 KB |
Output is correct |
105 |
Correct |
13 ms |
73308 KB |
Output is correct |
106 |
Correct |
13 ms |
73304 KB |
Output is correct |
107 |
Correct |
13 ms |
73304 KB |
Output is correct |
108 |
Correct |
816 ms |
143968 KB |
Output is correct |
109 |
Correct |
925 ms |
152336 KB |
Output is correct |
110 |
Correct |
844 ms |
161220 KB |
Output is correct |
111 |
Correct |
848 ms |
158892 KB |
Output is correct |
112 |
Correct |
734 ms |
156792 KB |
Output is correct |
113 |
Correct |
604 ms |
144220 KB |
Output is correct |
114 |
Correct |
627 ms |
140388 KB |
Output is correct |
115 |
Correct |
639 ms |
140564 KB |
Output is correct |
116 |
Correct |
1063 ms |
142592 KB |
Output is correct |
117 |
Correct |
645 ms |
143068 KB |
Output is correct |
118 |
Correct |
715 ms |
141020 KB |
Output is correct |
119 |
Correct |
657 ms |
143144 KB |
Output is correct |
120 |
Correct |
771 ms |
157460 KB |
Output is correct |
121 |
Correct |
722 ms |
143112 KB |
Output is correct |
122 |
Correct |
1078 ms |
142968 KB |
Output is correct |
123 |
Correct |
460 ms |
144052 KB |
Output is correct |
124 |
Correct |
776 ms |
139764 KB |
Output is correct |