#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tt;
#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())
const int mn = 3e5 + 5;
int up[mn][19], depth[mn], curLCA[mn], deg[mn];
vector<int> state[mn], adj[mn];
bool mark[mn], vis[mn];
vector<pii> edge;
void preDfs (int u, int p, int d) {
up[u][0] = p, depth[u] = d;
for (int i = 1; i < 19; i++)
up[u][i] = up[up[u][i - 1]][i - 1];
for (int v : adj[u])
if (v != p) preDfs(v, u, d + 1);
}
int goUp (int a, int k) {
for (int i = 0; i < 19; i++)
if (k & (1 << i)) a = up[a][i];
return a;
}
int lca (int a, int b) {
if (depth[b] > depth[a]) swap(a, b);
a = goUp(a, depth[a] - depth[b]);
if (a == b) return a;
for (int i = 18; i >= 0; i--)
if (up[a][i] != up[b][i])
a = up[a][i], b = up[b][i];
return up[a][0];
}
struct DSU {
vector<int> lab;
DSU (int sz) : lab(sz + 1, -1) {}
int get (int u) {
if (lab[u] < 0) return u;
return lab[u] = get(lab[u]);
}
void unite (int a, int b) {
a = get(a), b = get(b);
if (a == b) return;
if (-lab[a] < -lab[b]) swap(a, b);
lab[a] += lab[b], lab[b] = a;
}
} dsu(mn);
void dfs (int u, int p) {
for (int v : adj[u]) {
if (v == p) continue;
dfs(v, u);
curLCA[u] = lca(curLCA[u], curLCA[v]);
}
if (curLCA[u] == u && u != p) {
// for edge that separates the tree into groups
// create an edge in the new graph
edge.push_back({u, p});
}
else if (u != p) {
// compress nodes for other edges
dsu.unite(u, p);
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, k; cin >> n >> k;
for (int i = 1; i < n; i++) {
int a, b; cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
preDfs(1, 1, 1);
for (int i = 1; i <= n; i++) {
int j; cin >> j;
state[j].push_back(i);
}
for (int i = 1; i <= k; i++) {
int cur = 0;
for (int u : state[i]) {
if (!cur) cur = u;
else cur = lca(cur, u);
}
for (int u : state[i]) curLCA[u] = cur;
}
dfs(1, 1);
for (pii it : edge) {
int a, b; tie(a, b) = it;
a = dsu.get(a), b = dsu.get(b);
deg[a]++, deg[b]++;
}
int ans = 0;
for (int i = 1; i <= n; i++) {
int cur = dsu.get(i);
if (vis[cur]) continue;
vis[cur] = 1, ans += (deg[cur] == 1 ? 1 : 0);
// greedy: every pair of leaves (in the new graph)
// can be merged to satisfy the constraints
}
cout << (ans + 1) / 2;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
20056 KB |
Output is correct |
2 |
Correct |
7 ms |
20060 KB |
Output is correct |
3 |
Correct |
5 ms |
20056 KB |
Output is correct |
4 |
Correct |
4 ms |
20056 KB |
Output is correct |
5 |
Correct |
6 ms |
20060 KB |
Output is correct |
6 |
Correct |
5 ms |
19904 KB |
Output is correct |
7 |
Correct |
5 ms |
20060 KB |
Output is correct |
8 |
Correct |
5 ms |
19916 KB |
Output is correct |
9 |
Correct |
4 ms |
20056 KB |
Output is correct |
10 |
Correct |
6 ms |
20064 KB |
Output is correct |
11 |
Correct |
4 ms |
20060 KB |
Output is correct |
12 |
Correct |
4 ms |
20060 KB |
Output is correct |
13 |
Correct |
4 ms |
20056 KB |
Output is correct |
14 |
Correct |
4 ms |
20060 KB |
Output is correct |
15 |
Correct |
5 ms |
20060 KB |
Output is correct |
16 |
Correct |
5 ms |
20060 KB |
Output is correct |
17 |
Correct |
5 ms |
20116 KB |
Output is correct |
18 |
Correct |
4 ms |
19876 KB |
Output is correct |
19 |
Correct |
4 ms |
20056 KB |
Output is correct |
20 |
Correct |
5 ms |
20056 KB |
Output is correct |
21 |
Correct |
4 ms |
20060 KB |
Output is correct |
22 |
Correct |
4 ms |
20056 KB |
Output is correct |
23 |
Correct |
4 ms |
20060 KB |
Output is correct |
24 |
Correct |
5 ms |
20060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
20056 KB |
Output is correct |
2 |
Correct |
7 ms |
20060 KB |
Output is correct |
3 |
Correct |
5 ms |
20056 KB |
Output is correct |
4 |
Correct |
4 ms |
20056 KB |
Output is correct |
5 |
Correct |
6 ms |
20060 KB |
Output is correct |
6 |
Correct |
5 ms |
19904 KB |
Output is correct |
7 |
Correct |
5 ms |
20060 KB |
Output is correct |
8 |
Correct |
5 ms |
19916 KB |
Output is correct |
9 |
Correct |
4 ms |
20056 KB |
Output is correct |
10 |
Correct |
6 ms |
20064 KB |
Output is correct |
11 |
Correct |
4 ms |
20060 KB |
Output is correct |
12 |
Correct |
4 ms |
20060 KB |
Output is correct |
13 |
Correct |
4 ms |
20056 KB |
Output is correct |
14 |
Correct |
4 ms |
20060 KB |
Output is correct |
15 |
Correct |
5 ms |
20060 KB |
Output is correct |
16 |
Correct |
5 ms |
20060 KB |
Output is correct |
17 |
Correct |
5 ms |
20116 KB |
Output is correct |
18 |
Correct |
4 ms |
19876 KB |
Output is correct |
19 |
Correct |
4 ms |
20056 KB |
Output is correct |
20 |
Correct |
5 ms |
20056 KB |
Output is correct |
21 |
Correct |
4 ms |
20060 KB |
Output is correct |
22 |
Correct |
4 ms |
20056 KB |
Output is correct |
23 |
Correct |
4 ms |
20060 KB |
Output is correct |
24 |
Correct |
5 ms |
20060 KB |
Output is correct |
25 |
Correct |
4 ms |
20060 KB |
Output is correct |
26 |
Correct |
7 ms |
20316 KB |
Output is correct |
27 |
Correct |
6 ms |
20252 KB |
Output is correct |
28 |
Correct |
6 ms |
20316 KB |
Output is correct |
29 |
Correct |
8 ms |
20312 KB |
Output is correct |
30 |
Correct |
6 ms |
20060 KB |
Output is correct |
31 |
Correct |
5 ms |
20060 KB |
Output is correct |
32 |
Correct |
6 ms |
20488 KB |
Output is correct |
33 |
Correct |
5 ms |
20060 KB |
Output is correct |
34 |
Correct |
6 ms |
20124 KB |
Output is correct |
35 |
Correct |
6 ms |
20312 KB |
Output is correct |
36 |
Correct |
7 ms |
20060 KB |
Output is correct |
37 |
Correct |
6 ms |
20316 KB |
Output is correct |
38 |
Correct |
5 ms |
20060 KB |
Output is correct |
39 |
Correct |
7 ms |
20320 KB |
Output is correct |
40 |
Correct |
6 ms |
20056 KB |
Output is correct |
41 |
Correct |
6 ms |
20060 KB |
Output is correct |
42 |
Correct |
6 ms |
20316 KB |
Output is correct |
43 |
Correct |
6 ms |
20572 KB |
Output is correct |
44 |
Correct |
5 ms |
20056 KB |
Output is correct |
45 |
Correct |
6 ms |
20316 KB |
Output is correct |
46 |
Correct |
6 ms |
20132 KB |
Output is correct |
47 |
Correct |
4 ms |
20060 KB |
Output is correct |
48 |
Correct |
6 ms |
20060 KB |
Output is correct |
49 |
Correct |
6 ms |
20320 KB |
Output is correct |
50 |
Correct |
6 ms |
20400 KB |
Output is correct |
51 |
Correct |
6 ms |
20060 KB |
Output is correct |
52 |
Correct |
6 ms |
20060 KB |
Output is correct |
53 |
Correct |
6 ms |
20464 KB |
Output is correct |
54 |
Correct |
6 ms |
20060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
20056 KB |
Output is correct |
2 |
Correct |
7 ms |
20060 KB |
Output is correct |
3 |
Correct |
5 ms |
20056 KB |
Output is correct |
4 |
Correct |
4 ms |
20056 KB |
Output is correct |
5 |
Correct |
6 ms |
20060 KB |
Output is correct |
6 |
Correct |
5 ms |
19904 KB |
Output is correct |
7 |
Correct |
5 ms |
20060 KB |
Output is correct |
8 |
Correct |
5 ms |
19916 KB |
Output is correct |
9 |
Correct |
4 ms |
20056 KB |
Output is correct |
10 |
Correct |
6 ms |
20064 KB |
Output is correct |
11 |
Correct |
4 ms |
20060 KB |
Output is correct |
12 |
Correct |
4 ms |
20060 KB |
Output is correct |
13 |
Correct |
4 ms |
20056 KB |
Output is correct |
14 |
Correct |
4 ms |
20060 KB |
Output is correct |
15 |
Correct |
5 ms |
20060 KB |
Output is correct |
16 |
Correct |
5 ms |
20060 KB |
Output is correct |
17 |
Correct |
5 ms |
20116 KB |
Output is correct |
18 |
Correct |
4 ms |
19876 KB |
Output is correct |
19 |
Correct |
4 ms |
20056 KB |
Output is correct |
20 |
Correct |
5 ms |
20056 KB |
Output is correct |
21 |
Correct |
4 ms |
20060 KB |
Output is correct |
22 |
Correct |
4 ms |
20056 KB |
Output is correct |
23 |
Correct |
4 ms |
20060 KB |
Output is correct |
24 |
Correct |
5 ms |
20060 KB |
Output is correct |
25 |
Correct |
4 ms |
20060 KB |
Output is correct |
26 |
Correct |
51 ms |
34048 KB |
Output is correct |
27 |
Correct |
83 ms |
33620 KB |
Output is correct |
28 |
Correct |
6 ms |
20056 KB |
Output is correct |
29 |
Correct |
5 ms |
20060 KB |
Output is correct |
30 |
Correct |
5 ms |
20060 KB |
Output is correct |
31 |
Correct |
72 ms |
33452 KB |
Output is correct |
32 |
Correct |
5 ms |
20060 KB |
Output is correct |
33 |
Correct |
118 ms |
43544 KB |
Output is correct |
34 |
Correct |
65 ms |
33464 KB |
Output is correct |
35 |
Correct |
5 ms |
20232 KB |
Output is correct |
36 |
Correct |
75 ms |
34456 KB |
Output is correct |
37 |
Correct |
6 ms |
20060 KB |
Output is correct |
38 |
Correct |
6 ms |
20164 KB |
Output is correct |
39 |
Correct |
50 ms |
33608 KB |
Output is correct |
40 |
Correct |
7 ms |
20568 KB |
Output is correct |
41 |
Correct |
81 ms |
33548 KB |
Output is correct |
42 |
Correct |
91 ms |
36592 KB |
Output is correct |
43 |
Correct |
5 ms |
20056 KB |
Output is correct |
44 |
Correct |
89 ms |
43792 KB |
Output is correct |
45 |
Correct |
82 ms |
38740 KB |
Output is correct |
46 |
Correct |
7 ms |
20316 KB |
Output is correct |
47 |
Correct |
5 ms |
20056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
33740 KB |
Output is correct |
2 |
Correct |
65 ms |
37688 KB |
Output is correct |
3 |
Correct |
6 ms |
20312 KB |
Output is correct |
4 |
Correct |
6 ms |
20060 KB |
Output is correct |
5 |
Correct |
5 ms |
20060 KB |
Output is correct |
6 |
Correct |
4 ms |
20060 KB |
Output is correct |
7 |
Correct |
5 ms |
20060 KB |
Output is correct |
8 |
Correct |
95 ms |
34932 KB |
Output is correct |
9 |
Correct |
6 ms |
20060 KB |
Output is correct |
10 |
Correct |
94 ms |
33668 KB |
Output is correct |
11 |
Correct |
5 ms |
20060 KB |
Output is correct |
12 |
Correct |
79 ms |
33584 KB |
Output is correct |
13 |
Correct |
84 ms |
35160 KB |
Output is correct |
14 |
Correct |
87 ms |
37576 KB |
Output is correct |
15 |
Correct |
53 ms |
33612 KB |
Output is correct |
16 |
Correct |
6 ms |
20316 KB |
Output is correct |
17 |
Correct |
4 ms |
20060 KB |
Output is correct |
18 |
Correct |
78 ms |
37632 KB |
Output is correct |
19 |
Correct |
97 ms |
42956 KB |
Output is correct |
20 |
Correct |
7 ms |
20320 KB |
Output is correct |
21 |
Correct |
4 ms |
20060 KB |
Output is correct |
22 |
Correct |
76 ms |
35296 KB |
Output is correct |
23 |
Correct |
6 ms |
20060 KB |
Output is correct |
24 |
Correct |
86 ms |
34132 KB |
Output is correct |
25 |
Correct |
92 ms |
41164 KB |
Output is correct |
26 |
Correct |
6 ms |
20316 KB |
Output is correct |
27 |
Correct |
6 ms |
20316 KB |
Output is correct |
28 |
Correct |
7 ms |
20128 KB |
Output is correct |
29 |
Correct |
5 ms |
20060 KB |
Output is correct |
30 |
Correct |
6 ms |
20316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
20056 KB |
Output is correct |
2 |
Correct |
7 ms |
20060 KB |
Output is correct |
3 |
Correct |
5 ms |
20056 KB |
Output is correct |
4 |
Correct |
4 ms |
20056 KB |
Output is correct |
5 |
Correct |
6 ms |
20060 KB |
Output is correct |
6 |
Correct |
5 ms |
19904 KB |
Output is correct |
7 |
Correct |
5 ms |
20060 KB |
Output is correct |
8 |
Correct |
5 ms |
19916 KB |
Output is correct |
9 |
Correct |
4 ms |
20056 KB |
Output is correct |
10 |
Correct |
6 ms |
20064 KB |
Output is correct |
11 |
Correct |
4 ms |
20060 KB |
Output is correct |
12 |
Correct |
4 ms |
20060 KB |
Output is correct |
13 |
Correct |
4 ms |
20056 KB |
Output is correct |
14 |
Correct |
4 ms |
20060 KB |
Output is correct |
15 |
Correct |
5 ms |
20060 KB |
Output is correct |
16 |
Correct |
5 ms |
20060 KB |
Output is correct |
17 |
Correct |
5 ms |
20116 KB |
Output is correct |
18 |
Correct |
4 ms |
19876 KB |
Output is correct |
19 |
Correct |
4 ms |
20056 KB |
Output is correct |
20 |
Correct |
5 ms |
20056 KB |
Output is correct |
21 |
Correct |
4 ms |
20060 KB |
Output is correct |
22 |
Correct |
4 ms |
20056 KB |
Output is correct |
23 |
Correct |
4 ms |
20060 KB |
Output is correct |
24 |
Correct |
5 ms |
20060 KB |
Output is correct |
25 |
Correct |
4 ms |
20060 KB |
Output is correct |
26 |
Correct |
7 ms |
20316 KB |
Output is correct |
27 |
Correct |
6 ms |
20252 KB |
Output is correct |
28 |
Correct |
6 ms |
20316 KB |
Output is correct |
29 |
Correct |
8 ms |
20312 KB |
Output is correct |
30 |
Correct |
6 ms |
20060 KB |
Output is correct |
31 |
Correct |
5 ms |
20060 KB |
Output is correct |
32 |
Correct |
6 ms |
20488 KB |
Output is correct |
33 |
Correct |
5 ms |
20060 KB |
Output is correct |
34 |
Correct |
6 ms |
20124 KB |
Output is correct |
35 |
Correct |
6 ms |
20312 KB |
Output is correct |
36 |
Correct |
7 ms |
20060 KB |
Output is correct |
37 |
Correct |
6 ms |
20316 KB |
Output is correct |
38 |
Correct |
5 ms |
20060 KB |
Output is correct |
39 |
Correct |
7 ms |
20320 KB |
Output is correct |
40 |
Correct |
6 ms |
20056 KB |
Output is correct |
41 |
Correct |
6 ms |
20060 KB |
Output is correct |
42 |
Correct |
6 ms |
20316 KB |
Output is correct |
43 |
Correct |
6 ms |
20572 KB |
Output is correct |
44 |
Correct |
5 ms |
20056 KB |
Output is correct |
45 |
Correct |
6 ms |
20316 KB |
Output is correct |
46 |
Correct |
6 ms |
20132 KB |
Output is correct |
47 |
Correct |
4 ms |
20060 KB |
Output is correct |
48 |
Correct |
6 ms |
20060 KB |
Output is correct |
49 |
Correct |
6 ms |
20320 KB |
Output is correct |
50 |
Correct |
6 ms |
20400 KB |
Output is correct |
51 |
Correct |
6 ms |
20060 KB |
Output is correct |
52 |
Correct |
6 ms |
20060 KB |
Output is correct |
53 |
Correct |
6 ms |
20464 KB |
Output is correct |
54 |
Correct |
6 ms |
20060 KB |
Output is correct |
55 |
Correct |
4 ms |
20060 KB |
Output is correct |
56 |
Correct |
51 ms |
34048 KB |
Output is correct |
57 |
Correct |
83 ms |
33620 KB |
Output is correct |
58 |
Correct |
6 ms |
20056 KB |
Output is correct |
59 |
Correct |
5 ms |
20060 KB |
Output is correct |
60 |
Correct |
5 ms |
20060 KB |
Output is correct |
61 |
Correct |
72 ms |
33452 KB |
Output is correct |
62 |
Correct |
5 ms |
20060 KB |
Output is correct |
63 |
Correct |
118 ms |
43544 KB |
Output is correct |
64 |
Correct |
65 ms |
33464 KB |
Output is correct |
65 |
Correct |
5 ms |
20232 KB |
Output is correct |
66 |
Correct |
75 ms |
34456 KB |
Output is correct |
67 |
Correct |
6 ms |
20060 KB |
Output is correct |
68 |
Correct |
6 ms |
20164 KB |
Output is correct |
69 |
Correct |
50 ms |
33608 KB |
Output is correct |
70 |
Correct |
7 ms |
20568 KB |
Output is correct |
71 |
Correct |
81 ms |
33548 KB |
Output is correct |
72 |
Correct |
91 ms |
36592 KB |
Output is correct |
73 |
Correct |
5 ms |
20056 KB |
Output is correct |
74 |
Correct |
89 ms |
43792 KB |
Output is correct |
75 |
Correct |
82 ms |
38740 KB |
Output is correct |
76 |
Correct |
7 ms |
20316 KB |
Output is correct |
77 |
Correct |
5 ms |
20056 KB |
Output is correct |
78 |
Correct |
50 ms |
33740 KB |
Output is correct |
79 |
Correct |
65 ms |
37688 KB |
Output is correct |
80 |
Correct |
6 ms |
20312 KB |
Output is correct |
81 |
Correct |
6 ms |
20060 KB |
Output is correct |
82 |
Correct |
5 ms |
20060 KB |
Output is correct |
83 |
Correct |
4 ms |
20060 KB |
Output is correct |
84 |
Correct |
5 ms |
20060 KB |
Output is correct |
85 |
Correct |
95 ms |
34932 KB |
Output is correct |
86 |
Correct |
6 ms |
20060 KB |
Output is correct |
87 |
Correct |
94 ms |
33668 KB |
Output is correct |
88 |
Correct |
5 ms |
20060 KB |
Output is correct |
89 |
Correct |
79 ms |
33584 KB |
Output is correct |
90 |
Correct |
84 ms |
35160 KB |
Output is correct |
91 |
Correct |
87 ms |
37576 KB |
Output is correct |
92 |
Correct |
53 ms |
33612 KB |
Output is correct |
93 |
Correct |
6 ms |
20316 KB |
Output is correct |
94 |
Correct |
4 ms |
20060 KB |
Output is correct |
95 |
Correct |
78 ms |
37632 KB |
Output is correct |
96 |
Correct |
97 ms |
42956 KB |
Output is correct |
97 |
Correct |
7 ms |
20320 KB |
Output is correct |
98 |
Correct |
4 ms |
20060 KB |
Output is correct |
99 |
Correct |
76 ms |
35296 KB |
Output is correct |
100 |
Correct |
6 ms |
20060 KB |
Output is correct |
101 |
Correct |
86 ms |
34132 KB |
Output is correct |
102 |
Correct |
92 ms |
41164 KB |
Output is correct |
103 |
Correct |
6 ms |
20316 KB |
Output is correct |
104 |
Correct |
6 ms |
20316 KB |
Output is correct |
105 |
Correct |
7 ms |
20128 KB |
Output is correct |
106 |
Correct |
5 ms |
20060 KB |
Output is correct |
107 |
Correct |
6 ms |
20316 KB |
Output is correct |
108 |
Runtime error |
165 ms |
82956 KB |
Execution killed with signal 11 |
109 |
Halted |
0 ms |
0 KB |
- |