#include <bits/stdc++.h>
using namespace std;
template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl
using ll = long long;
const int maxn = 5e5 + 10;
int n, k;
vector<int> g[maxn];
int a[maxn];
const int LOG = 20;
int dep[maxn];
int par[LOG+1][maxn];
void dfs(int at, int p) {
for (int j=1; j<LOG; j++) {
par[j][at] = par[j-1][par[j-1][at]];
}
for (int to: g[at]) {
if (to == p) continue;
par[0][to] = at;
dep[to] = 1+dep[at];
dfs(to, at);
}
}
int lca(int u, int v) {
if (dep[u]>dep[v]) {
swap(u,v);
}
// u
// v
int dx = dep[v]-dep[u];
for (int j=LOG-1; j>=0; j--) {
if (dx>>j&1) {
v = par[j][v];
}
}
if (u==v) return u;
for (int j=LOG-1; j>=0; j--) {
if (par[j][u] != par[j][v]) {
u = par[j][u];
v = par[j][v];
}
}
return par[0][v];
}
vector<int> nodes[maxn];
int dp[maxn];
void dfs2(int at, int p) {
for (int to: g[at]) {
if (to == p) continue;
dfs2(to, at);
dp[at] += dp[to];
}
}
int deg[maxn];
int P[maxn];
void dfs3(int at, int p) {
if (dp[at]==0) {
if (p!=-1) {
// add edge
// p-->at
if (at != p) {
deg[at]++;
deg[P[p]]++;
}
P[at] = at;
}
} else {
P[at] = P[p];
}
for (int to: g[at]) {
if (to == p) continue;
dfs3(to, at);
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.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>>a[i];
--a[i];
nodes[a[i]].push_back(i);
}
dfs(0, -1);
for (int i=0; i<n; i++) {
dp[i] = 1;
}
for (int j=0; j<k; j++) {
int mid = nodes[j][0];
for (int x: nodes[j]) {
mid = lca(mid, x);
}
dp[mid] -= nodes[j].size();
}
dfs2(0, -1);
// for (int i=0; i<n; i++) {
// cout<<i<<": "<<dp[i]<<endl;
// }
dfs3(0, -1);
int leaves = 0;
for (int i=0; i<n; i++) {
if (deg[i]==1) leaves++;
}
cout<<(leaves+1)/2<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
24044 KB |
Output is correct |
2 |
Correct |
17 ms |
24044 KB |
Output is correct |
3 |
Correct |
16 ms |
24044 KB |
Output is correct |
4 |
Correct |
17 ms |
24044 KB |
Output is correct |
5 |
Correct |
18 ms |
24044 KB |
Output is correct |
6 |
Correct |
16 ms |
24044 KB |
Output is correct |
7 |
Correct |
17 ms |
24044 KB |
Output is correct |
8 |
Correct |
17 ms |
24044 KB |
Output is correct |
9 |
Correct |
17 ms |
24172 KB |
Output is correct |
10 |
Correct |
16 ms |
24044 KB |
Output is correct |
11 |
Correct |
17 ms |
24044 KB |
Output is correct |
12 |
Correct |
17 ms |
24044 KB |
Output is correct |
13 |
Correct |
16 ms |
24044 KB |
Output is correct |
14 |
Correct |
17 ms |
24300 KB |
Output is correct |
15 |
Correct |
16 ms |
24044 KB |
Output is correct |
16 |
Correct |
17 ms |
24044 KB |
Output is correct |
17 |
Correct |
16 ms |
24044 KB |
Output is correct |
18 |
Correct |
17 ms |
24064 KB |
Output is correct |
19 |
Correct |
16 ms |
24044 KB |
Output is correct |
20 |
Correct |
16 ms |
24044 KB |
Output is correct |
21 |
Correct |
17 ms |
24044 KB |
Output is correct |
22 |
Correct |
17 ms |
24044 KB |
Output is correct |
23 |
Correct |
16 ms |
24044 KB |
Output is correct |
24 |
Correct |
16 ms |
24044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
24044 KB |
Output is correct |
2 |
Correct |
17 ms |
24044 KB |
Output is correct |
3 |
Correct |
16 ms |
24044 KB |
Output is correct |
4 |
Correct |
17 ms |
24044 KB |
Output is correct |
5 |
Correct |
18 ms |
24044 KB |
Output is correct |
6 |
Correct |
16 ms |
24044 KB |
Output is correct |
7 |
Correct |
17 ms |
24044 KB |
Output is correct |
8 |
Correct |
17 ms |
24044 KB |
Output is correct |
9 |
Correct |
17 ms |
24172 KB |
Output is correct |
10 |
Correct |
16 ms |
24044 KB |
Output is correct |
11 |
Correct |
17 ms |
24044 KB |
Output is correct |
12 |
Correct |
17 ms |
24044 KB |
Output is correct |
13 |
Correct |
16 ms |
24044 KB |
Output is correct |
14 |
Correct |
17 ms |
24300 KB |
Output is correct |
15 |
Correct |
16 ms |
24044 KB |
Output is correct |
16 |
Correct |
17 ms |
24044 KB |
Output is correct |
17 |
Correct |
16 ms |
24044 KB |
Output is correct |
18 |
Correct |
17 ms |
24064 KB |
Output is correct |
19 |
Correct |
16 ms |
24044 KB |
Output is correct |
20 |
Correct |
16 ms |
24044 KB |
Output is correct |
21 |
Correct |
17 ms |
24044 KB |
Output is correct |
22 |
Correct |
17 ms |
24044 KB |
Output is correct |
23 |
Correct |
16 ms |
24044 KB |
Output is correct |
24 |
Correct |
16 ms |
24044 KB |
Output is correct |
25 |
Correct |
17 ms |
24044 KB |
Output is correct |
26 |
Correct |
19 ms |
24556 KB |
Output is correct |
27 |
Correct |
18 ms |
24428 KB |
Output is correct |
28 |
Correct |
19 ms |
24832 KB |
Output is correct |
29 |
Correct |
19 ms |
24556 KB |
Output is correct |
30 |
Correct |
19 ms |
24428 KB |
Output is correct |
31 |
Correct |
16 ms |
24064 KB |
Output is correct |
32 |
Correct |
19 ms |
24684 KB |
Output is correct |
33 |
Correct |
16 ms |
24044 KB |
Output is correct |
34 |
Correct |
18 ms |
24428 KB |
Output is correct |
35 |
Correct |
19 ms |
24556 KB |
Output is correct |
36 |
Correct |
19 ms |
24428 KB |
Output is correct |
37 |
Correct |
19 ms |
24428 KB |
Output is correct |
38 |
Correct |
17 ms |
24064 KB |
Output is correct |
39 |
Correct |
19 ms |
24428 KB |
Output is correct |
40 |
Correct |
19 ms |
24428 KB |
Output is correct |
41 |
Correct |
18 ms |
24428 KB |
Output is correct |
42 |
Correct |
19 ms |
24556 KB |
Output is correct |
43 |
Correct |
19 ms |
24684 KB |
Output is correct |
44 |
Correct |
16 ms |
24044 KB |
Output is correct |
45 |
Correct |
19 ms |
24556 KB |
Output is correct |
46 |
Correct |
20 ms |
24428 KB |
Output is correct |
47 |
Correct |
18 ms |
24044 KB |
Output is correct |
48 |
Correct |
19 ms |
24428 KB |
Output is correct |
49 |
Correct |
18 ms |
24556 KB |
Output is correct |
50 |
Correct |
19 ms |
24556 KB |
Output is correct |
51 |
Correct |
19 ms |
24428 KB |
Output is correct |
52 |
Correct |
18 ms |
24448 KB |
Output is correct |
53 |
Correct |
19 ms |
24556 KB |
Output is correct |
54 |
Correct |
18 ms |
24428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
24044 KB |
Output is correct |
2 |
Correct |
17 ms |
24044 KB |
Output is correct |
3 |
Correct |
16 ms |
24044 KB |
Output is correct |
4 |
Correct |
17 ms |
24044 KB |
Output is correct |
5 |
Correct |
18 ms |
24044 KB |
Output is correct |
6 |
Correct |
16 ms |
24044 KB |
Output is correct |
7 |
Correct |
17 ms |
24044 KB |
Output is correct |
8 |
Correct |
17 ms |
24044 KB |
Output is correct |
9 |
Correct |
17 ms |
24172 KB |
Output is correct |
10 |
Correct |
16 ms |
24044 KB |
Output is correct |
11 |
Correct |
17 ms |
24044 KB |
Output is correct |
12 |
Correct |
17 ms |
24044 KB |
Output is correct |
13 |
Correct |
16 ms |
24044 KB |
Output is correct |
14 |
Correct |
17 ms |
24300 KB |
Output is correct |
15 |
Correct |
16 ms |
24044 KB |
Output is correct |
16 |
Correct |
17 ms |
24044 KB |
Output is correct |
17 |
Correct |
16 ms |
24044 KB |
Output is correct |
18 |
Correct |
17 ms |
24064 KB |
Output is correct |
19 |
Correct |
16 ms |
24044 KB |
Output is correct |
20 |
Correct |
16 ms |
24044 KB |
Output is correct |
21 |
Correct |
17 ms |
24044 KB |
Output is correct |
22 |
Correct |
17 ms |
24044 KB |
Output is correct |
23 |
Correct |
16 ms |
24044 KB |
Output is correct |
24 |
Correct |
16 ms |
24044 KB |
Output is correct |
25 |
Correct |
16 ms |
24044 KB |
Output is correct |
26 |
Correct |
99 ms |
38884 KB |
Output is correct |
27 |
Correct |
143 ms |
38636 KB |
Output is correct |
28 |
Correct |
19 ms |
24448 KB |
Output is correct |
29 |
Correct |
17 ms |
24044 KB |
Output is correct |
30 |
Correct |
17 ms |
24044 KB |
Output is correct |
31 |
Correct |
143 ms |
38764 KB |
Output is correct |
32 |
Correct |
19 ms |
24428 KB |
Output is correct |
33 |
Correct |
191 ms |
45804 KB |
Output is correct |
34 |
Correct |
145 ms |
38636 KB |
Output is correct |
35 |
Correct |
27 ms |
24428 KB |
Output is correct |
36 |
Correct |
165 ms |
39532 KB |
Output is correct |
37 |
Correct |
19 ms |
24428 KB |
Output is correct |
38 |
Correct |
18 ms |
24428 KB |
Output is correct |
39 |
Correct |
100 ms |
38884 KB |
Output is correct |
40 |
Correct |
19 ms |
24684 KB |
Output is correct |
41 |
Correct |
133 ms |
38628 KB |
Output is correct |
42 |
Correct |
175 ms |
41068 KB |
Output is correct |
43 |
Correct |
18 ms |
24064 KB |
Output is correct |
44 |
Correct |
181 ms |
45936 KB |
Output is correct |
45 |
Correct |
181 ms |
42476 KB |
Output is correct |
46 |
Correct |
18 ms |
24428 KB |
Output is correct |
47 |
Correct |
18 ms |
24428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
98 ms |
38756 KB |
Output is correct |
2 |
Correct |
124 ms |
41700 KB |
Output is correct |
3 |
Correct |
19 ms |
24556 KB |
Output is correct |
4 |
Correct |
19 ms |
24556 KB |
Output is correct |
5 |
Correct |
17 ms |
24044 KB |
Output is correct |
6 |
Correct |
17 ms |
24044 KB |
Output is correct |
7 |
Correct |
18 ms |
24428 KB |
Output is correct |
8 |
Correct |
174 ms |
40428 KB |
Output is correct |
9 |
Correct |
19 ms |
24428 KB |
Output is correct |
10 |
Correct |
149 ms |
39276 KB |
Output is correct |
11 |
Correct |
16 ms |
24044 KB |
Output is correct |
12 |
Correct |
163 ms |
39148 KB |
Output is correct |
13 |
Correct |
172 ms |
40300 KB |
Output is correct |
14 |
Correct |
159 ms |
41964 KB |
Output is correct |
15 |
Correct |
104 ms |
38884 KB |
Output is correct |
16 |
Correct |
19 ms |
24556 KB |
Output is correct |
17 |
Correct |
16 ms |
24044 KB |
Output is correct |
18 |
Correct |
128 ms |
41828 KB |
Output is correct |
19 |
Correct |
195 ms |
45804 KB |
Output is correct |
20 |
Correct |
19 ms |
24428 KB |
Output is correct |
21 |
Correct |
16 ms |
24044 KB |
Output is correct |
22 |
Correct |
133 ms |
40548 KB |
Output is correct |
23 |
Correct |
18 ms |
24428 KB |
Output is correct |
24 |
Correct |
163 ms |
39532 KB |
Output is correct |
25 |
Correct |
199 ms |
44524 KB |
Output is correct |
26 |
Correct |
18 ms |
24556 KB |
Output is correct |
27 |
Correct |
18 ms |
24556 KB |
Output is correct |
28 |
Correct |
19 ms |
24428 KB |
Output is correct |
29 |
Correct |
18 ms |
24428 KB |
Output is correct |
30 |
Correct |
18 ms |
24556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
24044 KB |
Output is correct |
2 |
Correct |
17 ms |
24044 KB |
Output is correct |
3 |
Correct |
16 ms |
24044 KB |
Output is correct |
4 |
Correct |
17 ms |
24044 KB |
Output is correct |
5 |
Correct |
18 ms |
24044 KB |
Output is correct |
6 |
Correct |
16 ms |
24044 KB |
Output is correct |
7 |
Correct |
17 ms |
24044 KB |
Output is correct |
8 |
Correct |
17 ms |
24044 KB |
Output is correct |
9 |
Correct |
17 ms |
24172 KB |
Output is correct |
10 |
Correct |
16 ms |
24044 KB |
Output is correct |
11 |
Correct |
17 ms |
24044 KB |
Output is correct |
12 |
Correct |
17 ms |
24044 KB |
Output is correct |
13 |
Correct |
16 ms |
24044 KB |
Output is correct |
14 |
Correct |
17 ms |
24300 KB |
Output is correct |
15 |
Correct |
16 ms |
24044 KB |
Output is correct |
16 |
Correct |
17 ms |
24044 KB |
Output is correct |
17 |
Correct |
16 ms |
24044 KB |
Output is correct |
18 |
Correct |
17 ms |
24064 KB |
Output is correct |
19 |
Correct |
16 ms |
24044 KB |
Output is correct |
20 |
Correct |
16 ms |
24044 KB |
Output is correct |
21 |
Correct |
17 ms |
24044 KB |
Output is correct |
22 |
Correct |
17 ms |
24044 KB |
Output is correct |
23 |
Correct |
16 ms |
24044 KB |
Output is correct |
24 |
Correct |
16 ms |
24044 KB |
Output is correct |
25 |
Correct |
17 ms |
24044 KB |
Output is correct |
26 |
Correct |
19 ms |
24556 KB |
Output is correct |
27 |
Correct |
18 ms |
24428 KB |
Output is correct |
28 |
Correct |
19 ms |
24832 KB |
Output is correct |
29 |
Correct |
19 ms |
24556 KB |
Output is correct |
30 |
Correct |
19 ms |
24428 KB |
Output is correct |
31 |
Correct |
16 ms |
24064 KB |
Output is correct |
32 |
Correct |
19 ms |
24684 KB |
Output is correct |
33 |
Correct |
16 ms |
24044 KB |
Output is correct |
34 |
Correct |
18 ms |
24428 KB |
Output is correct |
35 |
Correct |
19 ms |
24556 KB |
Output is correct |
36 |
Correct |
19 ms |
24428 KB |
Output is correct |
37 |
Correct |
19 ms |
24428 KB |
Output is correct |
38 |
Correct |
17 ms |
24064 KB |
Output is correct |
39 |
Correct |
19 ms |
24428 KB |
Output is correct |
40 |
Correct |
19 ms |
24428 KB |
Output is correct |
41 |
Correct |
18 ms |
24428 KB |
Output is correct |
42 |
Correct |
19 ms |
24556 KB |
Output is correct |
43 |
Correct |
19 ms |
24684 KB |
Output is correct |
44 |
Correct |
16 ms |
24044 KB |
Output is correct |
45 |
Correct |
19 ms |
24556 KB |
Output is correct |
46 |
Correct |
20 ms |
24428 KB |
Output is correct |
47 |
Correct |
18 ms |
24044 KB |
Output is correct |
48 |
Correct |
19 ms |
24428 KB |
Output is correct |
49 |
Correct |
18 ms |
24556 KB |
Output is correct |
50 |
Correct |
19 ms |
24556 KB |
Output is correct |
51 |
Correct |
19 ms |
24428 KB |
Output is correct |
52 |
Correct |
18 ms |
24448 KB |
Output is correct |
53 |
Correct |
19 ms |
24556 KB |
Output is correct |
54 |
Correct |
18 ms |
24428 KB |
Output is correct |
55 |
Correct |
16 ms |
24044 KB |
Output is correct |
56 |
Correct |
99 ms |
38884 KB |
Output is correct |
57 |
Correct |
143 ms |
38636 KB |
Output is correct |
58 |
Correct |
19 ms |
24448 KB |
Output is correct |
59 |
Correct |
17 ms |
24044 KB |
Output is correct |
60 |
Correct |
17 ms |
24044 KB |
Output is correct |
61 |
Correct |
143 ms |
38764 KB |
Output is correct |
62 |
Correct |
19 ms |
24428 KB |
Output is correct |
63 |
Correct |
191 ms |
45804 KB |
Output is correct |
64 |
Correct |
145 ms |
38636 KB |
Output is correct |
65 |
Correct |
27 ms |
24428 KB |
Output is correct |
66 |
Correct |
165 ms |
39532 KB |
Output is correct |
67 |
Correct |
19 ms |
24428 KB |
Output is correct |
68 |
Correct |
18 ms |
24428 KB |
Output is correct |
69 |
Correct |
100 ms |
38884 KB |
Output is correct |
70 |
Correct |
19 ms |
24684 KB |
Output is correct |
71 |
Correct |
133 ms |
38628 KB |
Output is correct |
72 |
Correct |
175 ms |
41068 KB |
Output is correct |
73 |
Correct |
18 ms |
24064 KB |
Output is correct |
74 |
Correct |
181 ms |
45936 KB |
Output is correct |
75 |
Correct |
181 ms |
42476 KB |
Output is correct |
76 |
Correct |
18 ms |
24428 KB |
Output is correct |
77 |
Correct |
18 ms |
24428 KB |
Output is correct |
78 |
Correct |
98 ms |
38756 KB |
Output is correct |
79 |
Correct |
124 ms |
41700 KB |
Output is correct |
80 |
Correct |
19 ms |
24556 KB |
Output is correct |
81 |
Correct |
19 ms |
24556 KB |
Output is correct |
82 |
Correct |
17 ms |
24044 KB |
Output is correct |
83 |
Correct |
17 ms |
24044 KB |
Output is correct |
84 |
Correct |
18 ms |
24428 KB |
Output is correct |
85 |
Correct |
174 ms |
40428 KB |
Output is correct |
86 |
Correct |
19 ms |
24428 KB |
Output is correct |
87 |
Correct |
149 ms |
39276 KB |
Output is correct |
88 |
Correct |
16 ms |
24044 KB |
Output is correct |
89 |
Correct |
163 ms |
39148 KB |
Output is correct |
90 |
Correct |
172 ms |
40300 KB |
Output is correct |
91 |
Correct |
159 ms |
41964 KB |
Output is correct |
92 |
Correct |
104 ms |
38884 KB |
Output is correct |
93 |
Correct |
19 ms |
24556 KB |
Output is correct |
94 |
Correct |
16 ms |
24044 KB |
Output is correct |
95 |
Correct |
128 ms |
41828 KB |
Output is correct |
96 |
Correct |
195 ms |
45804 KB |
Output is correct |
97 |
Correct |
19 ms |
24428 KB |
Output is correct |
98 |
Correct |
16 ms |
24044 KB |
Output is correct |
99 |
Correct |
133 ms |
40548 KB |
Output is correct |
100 |
Correct |
18 ms |
24428 KB |
Output is correct |
101 |
Correct |
163 ms |
39532 KB |
Output is correct |
102 |
Correct |
199 ms |
44524 KB |
Output is correct |
103 |
Correct |
18 ms |
24556 KB |
Output is correct |
104 |
Correct |
18 ms |
24556 KB |
Output is correct |
105 |
Correct |
19 ms |
24428 KB |
Output is correct |
106 |
Correct |
18 ms |
24428 KB |
Output is correct |
107 |
Correct |
18 ms |
24556 KB |
Output is correct |
108 |
Correct |
836 ms |
106716 KB |
Output is correct |
109 |
Correct |
1346 ms |
114816 KB |
Output is correct |
110 |
Correct |
1385 ms |
126172 KB |
Output is correct |
111 |
Correct |
1498 ms |
137580 KB |
Output is correct |
112 |
Correct |
1426 ms |
128108 KB |
Output is correct |
113 |
Correct |
818 ms |
114644 KB |
Output is correct |
114 |
Correct |
860 ms |
96776 KB |
Output is correct |
115 |
Correct |
864 ms |
97244 KB |
Output is correct |
116 |
Correct |
1112 ms |
103404 KB |
Output is correct |
117 |
Correct |
1089 ms |
114668 KB |
Output is correct |
118 |
Correct |
889 ms |
98924 KB |
Output is correct |
119 |
Correct |
1089 ms |
114540 KB |
Output is correct |
120 |
Correct |
1402 ms |
128492 KB |
Output is correct |
121 |
Correct |
1052 ms |
114572 KB |
Output is correct |
122 |
Correct |
1166 ms |
108304 KB |
Output is correct |
123 |
Correct |
776 ms |
116316 KB |
Output is correct |
124 |
Correct |
1187 ms |
112876 KB |
Output is correct |