#include <bits/stdc++.h>
using namespace std;
const int N = 500005;
int n, k;
vector<int> states[N];
vector<int> adj[N];
int tin[N], tout[N], tid;
int up[N][20], dif[N];
int cnt[N], ans, huh[N];
void dfs1(int v, int p){
up[v][0] = p;
for(int i = 1; i<20; ++i)
up[v][i] = up[up[v][i-1]][i-1];
tin[v] = ++tid;
for(auto u : adj[v])
if(u!=p) dfs1(u, v);
tout[v] = tid;
}
bool is_fa(int u, int v){
return tin[u]<=tin[v] && tout[v]<=tout[u];
}
int lca(int u, int v){
if(is_fa(u, v)) return u;
if(is_fa(v, u)) return v;
for(int i = 19; i>=0; --i)
if(!is_fa(up[u][i], v))
u = up[u][i];
return up[u][0];
}
int dfs2(int v, int p){
int sum = dif[v];
for(auto u : adj[v]){
if(u==p) continue;
sum += dfs2(u, v);
cnt[v] += cnt[u];
}
huh[v] = sum;
if(v==0) return sum;
cnt[v] += (sum==0);
if(sum==0 && cnt[v]==1)
++ans;
return sum;
}
int main(){
// freopen("a.in", "r", stdin);
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
for(int i = 0; i<n-1; ++i){
int u, v; cin >> u >> v;
--u; --v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for(int i = 0; i<n; ++i){
int x; cin >> x; --x;
states[x].push_back(i);
}
dfs1(0, 0);
for(int i = 0; i<k; ++i){
int top = states[i][0];
for(auto v : states[i])
top = lca(top, v);
for(auto v : states[i])
++dif[v], --dif[top];
}
dfs2(0, 0);
for(int i = 1; i<n; ++i)
if(huh[i]==0 && cnt[i]==cnt[0])
++ans;
cout << (ans+1)/2 << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23756 KB |
Output is correct |
2 |
Correct |
13 ms |
23812 KB |
Output is correct |
3 |
Correct |
14 ms |
23816 KB |
Output is correct |
4 |
Correct |
13 ms |
23756 KB |
Output is correct |
5 |
Correct |
12 ms |
23808 KB |
Output is correct |
6 |
Correct |
12 ms |
23812 KB |
Output is correct |
7 |
Correct |
12 ms |
23756 KB |
Output is correct |
8 |
Correct |
12 ms |
23804 KB |
Output is correct |
9 |
Correct |
12 ms |
23744 KB |
Output is correct |
10 |
Correct |
11 ms |
23756 KB |
Output is correct |
11 |
Correct |
11 ms |
23788 KB |
Output is correct |
12 |
Correct |
11 ms |
23796 KB |
Output is correct |
13 |
Correct |
12 ms |
23756 KB |
Output is correct |
14 |
Correct |
13 ms |
23808 KB |
Output is correct |
15 |
Correct |
12 ms |
23808 KB |
Output is correct |
16 |
Correct |
13 ms |
23756 KB |
Output is correct |
17 |
Correct |
13 ms |
23852 KB |
Output is correct |
18 |
Correct |
13 ms |
23808 KB |
Output is correct |
19 |
Correct |
18 ms |
23724 KB |
Output is correct |
20 |
Correct |
16 ms |
23808 KB |
Output is correct |
21 |
Correct |
12 ms |
23756 KB |
Output is correct |
22 |
Correct |
12 ms |
23756 KB |
Output is correct |
23 |
Correct |
13 ms |
23848 KB |
Output is correct |
24 |
Correct |
13 ms |
24004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23756 KB |
Output is correct |
2 |
Correct |
13 ms |
23812 KB |
Output is correct |
3 |
Correct |
14 ms |
23816 KB |
Output is correct |
4 |
Correct |
13 ms |
23756 KB |
Output is correct |
5 |
Correct |
12 ms |
23808 KB |
Output is correct |
6 |
Correct |
12 ms |
23812 KB |
Output is correct |
7 |
Correct |
12 ms |
23756 KB |
Output is correct |
8 |
Correct |
12 ms |
23804 KB |
Output is correct |
9 |
Correct |
12 ms |
23744 KB |
Output is correct |
10 |
Correct |
11 ms |
23756 KB |
Output is correct |
11 |
Correct |
11 ms |
23788 KB |
Output is correct |
12 |
Correct |
11 ms |
23796 KB |
Output is correct |
13 |
Correct |
12 ms |
23756 KB |
Output is correct |
14 |
Correct |
13 ms |
23808 KB |
Output is correct |
15 |
Correct |
12 ms |
23808 KB |
Output is correct |
16 |
Correct |
13 ms |
23756 KB |
Output is correct |
17 |
Correct |
13 ms |
23852 KB |
Output is correct |
18 |
Correct |
13 ms |
23808 KB |
Output is correct |
19 |
Correct |
18 ms |
23724 KB |
Output is correct |
20 |
Correct |
16 ms |
23808 KB |
Output is correct |
21 |
Correct |
12 ms |
23756 KB |
Output is correct |
22 |
Correct |
12 ms |
23756 KB |
Output is correct |
23 |
Correct |
13 ms |
23848 KB |
Output is correct |
24 |
Correct |
13 ms |
24004 KB |
Output is correct |
25 |
Correct |
12 ms |
23756 KB |
Output is correct |
26 |
Correct |
14 ms |
24268 KB |
Output is correct |
27 |
Correct |
12 ms |
24272 KB |
Output is correct |
28 |
Correct |
13 ms |
24404 KB |
Output is correct |
29 |
Correct |
13 ms |
24332 KB |
Output is correct |
30 |
Correct |
14 ms |
24268 KB |
Output is correct |
31 |
Correct |
12 ms |
23756 KB |
Output is correct |
32 |
Correct |
15 ms |
24520 KB |
Output is correct |
33 |
Correct |
12 ms |
23756 KB |
Output is correct |
34 |
Correct |
16 ms |
24200 KB |
Output is correct |
35 |
Correct |
14 ms |
24308 KB |
Output is correct |
36 |
Correct |
16 ms |
24192 KB |
Output is correct |
37 |
Correct |
14 ms |
24208 KB |
Output is correct |
38 |
Correct |
12 ms |
23784 KB |
Output is correct |
39 |
Correct |
14 ms |
24268 KB |
Output is correct |
40 |
Correct |
13 ms |
24208 KB |
Output is correct |
41 |
Correct |
13 ms |
24252 KB |
Output is correct |
42 |
Correct |
13 ms |
24256 KB |
Output is correct |
43 |
Correct |
15 ms |
24524 KB |
Output is correct |
44 |
Correct |
12 ms |
23828 KB |
Output is correct |
45 |
Correct |
14 ms |
24332 KB |
Output is correct |
46 |
Correct |
14 ms |
24280 KB |
Output is correct |
47 |
Correct |
12 ms |
23800 KB |
Output is correct |
48 |
Correct |
14 ms |
24268 KB |
Output is correct |
49 |
Correct |
14 ms |
24320 KB |
Output is correct |
50 |
Correct |
14 ms |
24324 KB |
Output is correct |
51 |
Correct |
16 ms |
24268 KB |
Output is correct |
52 |
Correct |
13 ms |
24308 KB |
Output is correct |
53 |
Correct |
13 ms |
24336 KB |
Output is correct |
54 |
Correct |
14 ms |
24204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23756 KB |
Output is correct |
2 |
Correct |
13 ms |
23812 KB |
Output is correct |
3 |
Correct |
14 ms |
23816 KB |
Output is correct |
4 |
Correct |
13 ms |
23756 KB |
Output is correct |
5 |
Correct |
12 ms |
23808 KB |
Output is correct |
6 |
Correct |
12 ms |
23812 KB |
Output is correct |
7 |
Correct |
12 ms |
23756 KB |
Output is correct |
8 |
Correct |
12 ms |
23804 KB |
Output is correct |
9 |
Correct |
12 ms |
23744 KB |
Output is correct |
10 |
Correct |
11 ms |
23756 KB |
Output is correct |
11 |
Correct |
11 ms |
23788 KB |
Output is correct |
12 |
Correct |
11 ms |
23796 KB |
Output is correct |
13 |
Correct |
12 ms |
23756 KB |
Output is correct |
14 |
Correct |
13 ms |
23808 KB |
Output is correct |
15 |
Correct |
12 ms |
23808 KB |
Output is correct |
16 |
Correct |
13 ms |
23756 KB |
Output is correct |
17 |
Correct |
13 ms |
23852 KB |
Output is correct |
18 |
Correct |
13 ms |
23808 KB |
Output is correct |
19 |
Correct |
18 ms |
23724 KB |
Output is correct |
20 |
Correct |
16 ms |
23808 KB |
Output is correct |
21 |
Correct |
12 ms |
23756 KB |
Output is correct |
22 |
Correct |
12 ms |
23756 KB |
Output is correct |
23 |
Correct |
13 ms |
23848 KB |
Output is correct |
24 |
Correct |
13 ms |
24004 KB |
Output is correct |
25 |
Correct |
12 ms |
23768 KB |
Output is correct |
26 |
Correct |
77 ms |
39096 KB |
Output is correct |
27 |
Correct |
88 ms |
38712 KB |
Output is correct |
28 |
Correct |
14 ms |
24260 KB |
Output is correct |
29 |
Correct |
12 ms |
23756 KB |
Output is correct |
30 |
Correct |
13 ms |
23776 KB |
Output is correct |
31 |
Correct |
101 ms |
38776 KB |
Output is correct |
32 |
Correct |
14 ms |
24260 KB |
Output is correct |
33 |
Correct |
127 ms |
45860 KB |
Output is correct |
34 |
Correct |
102 ms |
38828 KB |
Output is correct |
35 |
Correct |
13 ms |
24204 KB |
Output is correct |
36 |
Correct |
104 ms |
39508 KB |
Output is correct |
37 |
Correct |
14 ms |
24268 KB |
Output is correct |
38 |
Correct |
14 ms |
24260 KB |
Output is correct |
39 |
Correct |
68 ms |
39000 KB |
Output is correct |
40 |
Correct |
14 ms |
24460 KB |
Output is correct |
41 |
Correct |
87 ms |
38784 KB |
Output is correct |
42 |
Correct |
124 ms |
41156 KB |
Output is correct |
43 |
Correct |
12 ms |
23832 KB |
Output is correct |
44 |
Correct |
110 ms |
46192 KB |
Output is correct |
45 |
Correct |
111 ms |
42544 KB |
Output is correct |
46 |
Correct |
14 ms |
24204 KB |
Output is correct |
47 |
Correct |
14 ms |
24236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
39152 KB |
Output is correct |
2 |
Correct |
84 ms |
41968 KB |
Output is correct |
3 |
Correct |
14 ms |
24312 KB |
Output is correct |
4 |
Correct |
14 ms |
24268 KB |
Output is correct |
5 |
Correct |
12 ms |
23808 KB |
Output is correct |
6 |
Correct |
16 ms |
23804 KB |
Output is correct |
7 |
Correct |
14 ms |
24256 KB |
Output is correct |
8 |
Correct |
113 ms |
40112 KB |
Output is correct |
9 |
Correct |
18 ms |
24268 KB |
Output is correct |
10 |
Correct |
95 ms |
38916 KB |
Output is correct |
11 |
Correct |
12 ms |
23796 KB |
Output is correct |
12 |
Correct |
120 ms |
38908 KB |
Output is correct |
13 |
Correct |
108 ms |
39964 KB |
Output is correct |
14 |
Correct |
112 ms |
41608 KB |
Output is correct |
15 |
Correct |
66 ms |
39020 KB |
Output is correct |
16 |
Correct |
14 ms |
24200 KB |
Output is correct |
17 |
Correct |
12 ms |
23816 KB |
Output is correct |
18 |
Correct |
84 ms |
41700 KB |
Output is correct |
19 |
Correct |
135 ms |
45628 KB |
Output is correct |
20 |
Correct |
14 ms |
24260 KB |
Output is correct |
21 |
Correct |
12 ms |
23756 KB |
Output is correct |
22 |
Correct |
82 ms |
40320 KB |
Output is correct |
23 |
Correct |
14 ms |
24260 KB |
Output is correct |
24 |
Correct |
111 ms |
39300 KB |
Output is correct |
25 |
Correct |
114 ms |
44224 KB |
Output is correct |
26 |
Correct |
15 ms |
24268 KB |
Output is correct |
27 |
Correct |
16 ms |
24324 KB |
Output is correct |
28 |
Correct |
19 ms |
24196 KB |
Output is correct |
29 |
Correct |
13 ms |
24256 KB |
Output is correct |
30 |
Correct |
14 ms |
24284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23756 KB |
Output is correct |
2 |
Correct |
13 ms |
23812 KB |
Output is correct |
3 |
Correct |
14 ms |
23816 KB |
Output is correct |
4 |
Correct |
13 ms |
23756 KB |
Output is correct |
5 |
Correct |
12 ms |
23808 KB |
Output is correct |
6 |
Correct |
12 ms |
23812 KB |
Output is correct |
7 |
Correct |
12 ms |
23756 KB |
Output is correct |
8 |
Correct |
12 ms |
23804 KB |
Output is correct |
9 |
Correct |
12 ms |
23744 KB |
Output is correct |
10 |
Correct |
11 ms |
23756 KB |
Output is correct |
11 |
Correct |
11 ms |
23788 KB |
Output is correct |
12 |
Correct |
11 ms |
23796 KB |
Output is correct |
13 |
Correct |
12 ms |
23756 KB |
Output is correct |
14 |
Correct |
13 ms |
23808 KB |
Output is correct |
15 |
Correct |
12 ms |
23808 KB |
Output is correct |
16 |
Correct |
13 ms |
23756 KB |
Output is correct |
17 |
Correct |
13 ms |
23852 KB |
Output is correct |
18 |
Correct |
13 ms |
23808 KB |
Output is correct |
19 |
Correct |
18 ms |
23724 KB |
Output is correct |
20 |
Correct |
16 ms |
23808 KB |
Output is correct |
21 |
Correct |
12 ms |
23756 KB |
Output is correct |
22 |
Correct |
12 ms |
23756 KB |
Output is correct |
23 |
Correct |
13 ms |
23848 KB |
Output is correct |
24 |
Correct |
13 ms |
24004 KB |
Output is correct |
25 |
Correct |
12 ms |
23756 KB |
Output is correct |
26 |
Correct |
14 ms |
24268 KB |
Output is correct |
27 |
Correct |
12 ms |
24272 KB |
Output is correct |
28 |
Correct |
13 ms |
24404 KB |
Output is correct |
29 |
Correct |
13 ms |
24332 KB |
Output is correct |
30 |
Correct |
14 ms |
24268 KB |
Output is correct |
31 |
Correct |
12 ms |
23756 KB |
Output is correct |
32 |
Correct |
15 ms |
24520 KB |
Output is correct |
33 |
Correct |
12 ms |
23756 KB |
Output is correct |
34 |
Correct |
16 ms |
24200 KB |
Output is correct |
35 |
Correct |
14 ms |
24308 KB |
Output is correct |
36 |
Correct |
16 ms |
24192 KB |
Output is correct |
37 |
Correct |
14 ms |
24208 KB |
Output is correct |
38 |
Correct |
12 ms |
23784 KB |
Output is correct |
39 |
Correct |
14 ms |
24268 KB |
Output is correct |
40 |
Correct |
13 ms |
24208 KB |
Output is correct |
41 |
Correct |
13 ms |
24252 KB |
Output is correct |
42 |
Correct |
13 ms |
24256 KB |
Output is correct |
43 |
Correct |
15 ms |
24524 KB |
Output is correct |
44 |
Correct |
12 ms |
23828 KB |
Output is correct |
45 |
Correct |
14 ms |
24332 KB |
Output is correct |
46 |
Correct |
14 ms |
24280 KB |
Output is correct |
47 |
Correct |
12 ms |
23800 KB |
Output is correct |
48 |
Correct |
14 ms |
24268 KB |
Output is correct |
49 |
Correct |
14 ms |
24320 KB |
Output is correct |
50 |
Correct |
14 ms |
24324 KB |
Output is correct |
51 |
Correct |
16 ms |
24268 KB |
Output is correct |
52 |
Correct |
13 ms |
24308 KB |
Output is correct |
53 |
Correct |
13 ms |
24336 KB |
Output is correct |
54 |
Correct |
14 ms |
24204 KB |
Output is correct |
55 |
Correct |
12 ms |
23768 KB |
Output is correct |
56 |
Correct |
77 ms |
39096 KB |
Output is correct |
57 |
Correct |
88 ms |
38712 KB |
Output is correct |
58 |
Correct |
14 ms |
24260 KB |
Output is correct |
59 |
Correct |
12 ms |
23756 KB |
Output is correct |
60 |
Correct |
13 ms |
23776 KB |
Output is correct |
61 |
Correct |
101 ms |
38776 KB |
Output is correct |
62 |
Correct |
14 ms |
24260 KB |
Output is correct |
63 |
Correct |
127 ms |
45860 KB |
Output is correct |
64 |
Correct |
102 ms |
38828 KB |
Output is correct |
65 |
Correct |
13 ms |
24204 KB |
Output is correct |
66 |
Correct |
104 ms |
39508 KB |
Output is correct |
67 |
Correct |
14 ms |
24268 KB |
Output is correct |
68 |
Correct |
14 ms |
24260 KB |
Output is correct |
69 |
Correct |
68 ms |
39000 KB |
Output is correct |
70 |
Correct |
14 ms |
24460 KB |
Output is correct |
71 |
Correct |
87 ms |
38784 KB |
Output is correct |
72 |
Correct |
124 ms |
41156 KB |
Output is correct |
73 |
Correct |
12 ms |
23832 KB |
Output is correct |
74 |
Correct |
110 ms |
46192 KB |
Output is correct |
75 |
Correct |
111 ms |
42544 KB |
Output is correct |
76 |
Correct |
14 ms |
24204 KB |
Output is correct |
77 |
Correct |
14 ms |
24236 KB |
Output is correct |
78 |
Correct |
74 ms |
39152 KB |
Output is correct |
79 |
Correct |
84 ms |
41968 KB |
Output is correct |
80 |
Correct |
14 ms |
24312 KB |
Output is correct |
81 |
Correct |
14 ms |
24268 KB |
Output is correct |
82 |
Correct |
12 ms |
23808 KB |
Output is correct |
83 |
Correct |
16 ms |
23804 KB |
Output is correct |
84 |
Correct |
14 ms |
24256 KB |
Output is correct |
85 |
Correct |
113 ms |
40112 KB |
Output is correct |
86 |
Correct |
18 ms |
24268 KB |
Output is correct |
87 |
Correct |
95 ms |
38916 KB |
Output is correct |
88 |
Correct |
12 ms |
23796 KB |
Output is correct |
89 |
Correct |
120 ms |
38908 KB |
Output is correct |
90 |
Correct |
108 ms |
39964 KB |
Output is correct |
91 |
Correct |
112 ms |
41608 KB |
Output is correct |
92 |
Correct |
66 ms |
39020 KB |
Output is correct |
93 |
Correct |
14 ms |
24200 KB |
Output is correct |
94 |
Correct |
12 ms |
23816 KB |
Output is correct |
95 |
Correct |
84 ms |
41700 KB |
Output is correct |
96 |
Correct |
135 ms |
45628 KB |
Output is correct |
97 |
Correct |
14 ms |
24260 KB |
Output is correct |
98 |
Correct |
12 ms |
23756 KB |
Output is correct |
99 |
Correct |
82 ms |
40320 KB |
Output is correct |
100 |
Correct |
14 ms |
24260 KB |
Output is correct |
101 |
Correct |
111 ms |
39300 KB |
Output is correct |
102 |
Correct |
114 ms |
44224 KB |
Output is correct |
103 |
Correct |
15 ms |
24268 KB |
Output is correct |
104 |
Correct |
16 ms |
24324 KB |
Output is correct |
105 |
Correct |
19 ms |
24196 KB |
Output is correct |
106 |
Correct |
13 ms |
24256 KB |
Output is correct |
107 |
Correct |
14 ms |
24284 KB |
Output is correct |
108 |
Correct |
533 ms |
106628 KB |
Output is correct |
109 |
Correct |
748 ms |
116516 KB |
Output is correct |
110 |
Correct |
795 ms |
127880 KB |
Output is correct |
111 |
Correct |
922 ms |
137568 KB |
Output is correct |
112 |
Correct |
873 ms |
127792 KB |
Output is correct |
113 |
Correct |
551 ms |
114384 KB |
Output is correct |
114 |
Correct |
581 ms |
98608 KB |
Output is correct |
115 |
Correct |
574 ms |
98580 KB |
Output is correct |
116 |
Correct |
771 ms |
103344 KB |
Output is correct |
117 |
Correct |
768 ms |
114496 KB |
Output is correct |
118 |
Correct |
649 ms |
99056 KB |
Output is correct |
119 |
Correct |
780 ms |
114580 KB |
Output is correct |
120 |
Correct |
907 ms |
128452 KB |
Output is correct |
121 |
Correct |
776 ms |
114476 KB |
Output is correct |
122 |
Correct |
803 ms |
108208 KB |
Output is correct |
123 |
Correct |
540 ms |
115964 KB |
Output is correct |
124 |
Correct |
867 ms |
112700 KB |
Output is correct |