#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 = 5e5 + 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);
// i hate greedy: every pair of leaves (in the new graph)
// can be merged to satisfy the constraints
}
cout << (ans + 1) / 2;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
33112 KB |
Output is correct |
2 |
Correct |
10 ms |
33116 KB |
Output is correct |
3 |
Correct |
9 ms |
33116 KB |
Output is correct |
4 |
Correct |
8 ms |
33116 KB |
Output is correct |
5 |
Correct |
7 ms |
33116 KB |
Output is correct |
6 |
Correct |
8 ms |
33116 KB |
Output is correct |
7 |
Correct |
7 ms |
33116 KB |
Output is correct |
8 |
Correct |
8 ms |
33116 KB |
Output is correct |
9 |
Correct |
8 ms |
33116 KB |
Output is correct |
10 |
Correct |
7 ms |
33116 KB |
Output is correct |
11 |
Correct |
8 ms |
33116 KB |
Output is correct |
12 |
Correct |
7 ms |
33116 KB |
Output is correct |
13 |
Correct |
7 ms |
33112 KB |
Output is correct |
14 |
Correct |
7 ms |
33204 KB |
Output is correct |
15 |
Correct |
8 ms |
33112 KB |
Output is correct |
16 |
Correct |
7 ms |
33116 KB |
Output is correct |
17 |
Correct |
8 ms |
33116 KB |
Output is correct |
18 |
Correct |
8 ms |
33116 KB |
Output is correct |
19 |
Correct |
8 ms |
33116 KB |
Output is correct |
20 |
Correct |
7 ms |
33116 KB |
Output is correct |
21 |
Correct |
7 ms |
33116 KB |
Output is correct |
22 |
Correct |
7 ms |
33116 KB |
Output is correct |
23 |
Correct |
7 ms |
33116 KB |
Output is correct |
24 |
Correct |
7 ms |
33112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
33112 KB |
Output is correct |
2 |
Correct |
10 ms |
33116 KB |
Output is correct |
3 |
Correct |
9 ms |
33116 KB |
Output is correct |
4 |
Correct |
8 ms |
33116 KB |
Output is correct |
5 |
Correct |
7 ms |
33116 KB |
Output is correct |
6 |
Correct |
8 ms |
33116 KB |
Output is correct |
7 |
Correct |
7 ms |
33116 KB |
Output is correct |
8 |
Correct |
8 ms |
33116 KB |
Output is correct |
9 |
Correct |
8 ms |
33116 KB |
Output is correct |
10 |
Correct |
7 ms |
33116 KB |
Output is correct |
11 |
Correct |
8 ms |
33116 KB |
Output is correct |
12 |
Correct |
7 ms |
33116 KB |
Output is correct |
13 |
Correct |
7 ms |
33112 KB |
Output is correct |
14 |
Correct |
7 ms |
33204 KB |
Output is correct |
15 |
Correct |
8 ms |
33112 KB |
Output is correct |
16 |
Correct |
7 ms |
33116 KB |
Output is correct |
17 |
Correct |
8 ms |
33116 KB |
Output is correct |
18 |
Correct |
8 ms |
33116 KB |
Output is correct |
19 |
Correct |
8 ms |
33116 KB |
Output is correct |
20 |
Correct |
7 ms |
33116 KB |
Output is correct |
21 |
Correct |
7 ms |
33116 KB |
Output is correct |
22 |
Correct |
7 ms |
33116 KB |
Output is correct |
23 |
Correct |
7 ms |
33116 KB |
Output is correct |
24 |
Correct |
7 ms |
33112 KB |
Output is correct |
25 |
Correct |
7 ms |
33116 KB |
Output is correct |
26 |
Correct |
9 ms |
33372 KB |
Output is correct |
27 |
Correct |
8 ms |
33232 KB |
Output is correct |
28 |
Correct |
9 ms |
33372 KB |
Output is correct |
29 |
Correct |
10 ms |
33372 KB |
Output is correct |
30 |
Correct |
10 ms |
33116 KB |
Output is correct |
31 |
Correct |
7 ms |
33112 KB |
Output is correct |
32 |
Correct |
9 ms |
33556 KB |
Output is correct |
33 |
Correct |
7 ms |
33116 KB |
Output is correct |
34 |
Correct |
8 ms |
33116 KB |
Output is correct |
35 |
Correct |
9 ms |
33372 KB |
Output is correct |
36 |
Correct |
9 ms |
33116 KB |
Output is correct |
37 |
Correct |
9 ms |
33252 KB |
Output is correct |
38 |
Correct |
7 ms |
33112 KB |
Output is correct |
39 |
Correct |
9 ms |
33116 KB |
Output is correct |
40 |
Correct |
9 ms |
33300 KB |
Output is correct |
41 |
Correct |
9 ms |
33116 KB |
Output is correct |
42 |
Correct |
9 ms |
33208 KB |
Output is correct |
43 |
Correct |
9 ms |
33628 KB |
Output is correct |
44 |
Correct |
7 ms |
33116 KB |
Output is correct |
45 |
Correct |
9 ms |
33372 KB |
Output is correct |
46 |
Correct |
10 ms |
33372 KB |
Output is correct |
47 |
Correct |
7 ms |
33116 KB |
Output is correct |
48 |
Correct |
10 ms |
33116 KB |
Output is correct |
49 |
Correct |
8 ms |
33372 KB |
Output is correct |
50 |
Correct |
9 ms |
33372 KB |
Output is correct |
51 |
Correct |
9 ms |
33220 KB |
Output is correct |
52 |
Correct |
8 ms |
33116 KB |
Output is correct |
53 |
Correct |
8 ms |
33372 KB |
Output is correct |
54 |
Correct |
9 ms |
33116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
33112 KB |
Output is correct |
2 |
Correct |
10 ms |
33116 KB |
Output is correct |
3 |
Correct |
9 ms |
33116 KB |
Output is correct |
4 |
Correct |
8 ms |
33116 KB |
Output is correct |
5 |
Correct |
7 ms |
33116 KB |
Output is correct |
6 |
Correct |
8 ms |
33116 KB |
Output is correct |
7 |
Correct |
7 ms |
33116 KB |
Output is correct |
8 |
Correct |
8 ms |
33116 KB |
Output is correct |
9 |
Correct |
8 ms |
33116 KB |
Output is correct |
10 |
Correct |
7 ms |
33116 KB |
Output is correct |
11 |
Correct |
8 ms |
33116 KB |
Output is correct |
12 |
Correct |
7 ms |
33116 KB |
Output is correct |
13 |
Correct |
7 ms |
33112 KB |
Output is correct |
14 |
Correct |
7 ms |
33204 KB |
Output is correct |
15 |
Correct |
8 ms |
33112 KB |
Output is correct |
16 |
Correct |
7 ms |
33116 KB |
Output is correct |
17 |
Correct |
8 ms |
33116 KB |
Output is correct |
18 |
Correct |
8 ms |
33116 KB |
Output is correct |
19 |
Correct |
8 ms |
33116 KB |
Output is correct |
20 |
Correct |
7 ms |
33116 KB |
Output is correct |
21 |
Correct |
7 ms |
33116 KB |
Output is correct |
22 |
Correct |
7 ms |
33116 KB |
Output is correct |
23 |
Correct |
7 ms |
33116 KB |
Output is correct |
24 |
Correct |
7 ms |
33112 KB |
Output is correct |
25 |
Correct |
7 ms |
33112 KB |
Output is correct |
26 |
Correct |
56 ms |
45440 KB |
Output is correct |
27 |
Correct |
88 ms |
45168 KB |
Output is correct |
28 |
Correct |
9 ms |
33116 KB |
Output is correct |
29 |
Correct |
9 ms |
33116 KB |
Output is correct |
30 |
Correct |
7 ms |
33116 KB |
Output is correct |
31 |
Correct |
85 ms |
45144 KB |
Output is correct |
32 |
Correct |
8 ms |
33300 KB |
Output is correct |
33 |
Correct |
102 ms |
55128 KB |
Output is correct |
34 |
Correct |
75 ms |
45392 KB |
Output is correct |
35 |
Correct |
8 ms |
33116 KB |
Output is correct |
36 |
Correct |
79 ms |
46248 KB |
Output is correct |
37 |
Correct |
8 ms |
33112 KB |
Output is correct |
38 |
Correct |
9 ms |
33116 KB |
Output is correct |
39 |
Correct |
53 ms |
45344 KB |
Output is correct |
40 |
Correct |
10 ms |
33628 KB |
Output is correct |
41 |
Correct |
81 ms |
45264 KB |
Output is correct |
42 |
Correct |
100 ms |
48280 KB |
Output is correct |
43 |
Correct |
7 ms |
33116 KB |
Output is correct |
44 |
Correct |
92 ms |
55644 KB |
Output is correct |
45 |
Correct |
91 ms |
50396 KB |
Output is correct |
46 |
Correct |
8 ms |
33116 KB |
Output is correct |
47 |
Correct |
9 ms |
33116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
45404 KB |
Output is correct |
2 |
Correct |
66 ms |
49164 KB |
Output is correct |
3 |
Correct |
8 ms |
33368 KB |
Output is correct |
4 |
Correct |
9 ms |
33116 KB |
Output is correct |
5 |
Correct |
8 ms |
32972 KB |
Output is correct |
6 |
Correct |
8 ms |
33112 KB |
Output is correct |
7 |
Correct |
9 ms |
33116 KB |
Output is correct |
8 |
Correct |
88 ms |
46316 KB |
Output is correct |
9 |
Correct |
8 ms |
33116 KB |
Output is correct |
10 |
Correct |
77 ms |
45116 KB |
Output is correct |
11 |
Correct |
7 ms |
33116 KB |
Output is correct |
12 |
Correct |
78 ms |
45140 KB |
Output is correct |
13 |
Correct |
109 ms |
46720 KB |
Output is correct |
14 |
Correct |
82 ms |
48708 KB |
Output is correct |
15 |
Correct |
52 ms |
45384 KB |
Output is correct |
16 |
Correct |
9 ms |
33372 KB |
Output is correct |
17 |
Correct |
7 ms |
33116 KB |
Output is correct |
18 |
Correct |
72 ms |
49020 KB |
Output is correct |
19 |
Correct |
105 ms |
54316 KB |
Output is correct |
20 |
Correct |
9 ms |
33368 KB |
Output is correct |
21 |
Correct |
7 ms |
33116 KB |
Output is correct |
22 |
Correct |
59 ms |
46928 KB |
Output is correct |
23 |
Correct |
8 ms |
33116 KB |
Output is correct |
24 |
Correct |
75 ms |
45648 KB |
Output is correct |
25 |
Correct |
96 ms |
52428 KB |
Output is correct |
26 |
Correct |
8 ms |
33368 KB |
Output is correct |
27 |
Correct |
8 ms |
33496 KB |
Output is correct |
28 |
Correct |
9 ms |
33116 KB |
Output is correct |
29 |
Correct |
8 ms |
33240 KB |
Output is correct |
30 |
Correct |
10 ms |
33372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
33112 KB |
Output is correct |
2 |
Correct |
10 ms |
33116 KB |
Output is correct |
3 |
Correct |
9 ms |
33116 KB |
Output is correct |
4 |
Correct |
8 ms |
33116 KB |
Output is correct |
5 |
Correct |
7 ms |
33116 KB |
Output is correct |
6 |
Correct |
8 ms |
33116 KB |
Output is correct |
7 |
Correct |
7 ms |
33116 KB |
Output is correct |
8 |
Correct |
8 ms |
33116 KB |
Output is correct |
9 |
Correct |
8 ms |
33116 KB |
Output is correct |
10 |
Correct |
7 ms |
33116 KB |
Output is correct |
11 |
Correct |
8 ms |
33116 KB |
Output is correct |
12 |
Correct |
7 ms |
33116 KB |
Output is correct |
13 |
Correct |
7 ms |
33112 KB |
Output is correct |
14 |
Correct |
7 ms |
33204 KB |
Output is correct |
15 |
Correct |
8 ms |
33112 KB |
Output is correct |
16 |
Correct |
7 ms |
33116 KB |
Output is correct |
17 |
Correct |
8 ms |
33116 KB |
Output is correct |
18 |
Correct |
8 ms |
33116 KB |
Output is correct |
19 |
Correct |
8 ms |
33116 KB |
Output is correct |
20 |
Correct |
7 ms |
33116 KB |
Output is correct |
21 |
Correct |
7 ms |
33116 KB |
Output is correct |
22 |
Correct |
7 ms |
33116 KB |
Output is correct |
23 |
Correct |
7 ms |
33116 KB |
Output is correct |
24 |
Correct |
7 ms |
33112 KB |
Output is correct |
25 |
Correct |
7 ms |
33116 KB |
Output is correct |
26 |
Correct |
9 ms |
33372 KB |
Output is correct |
27 |
Correct |
8 ms |
33232 KB |
Output is correct |
28 |
Correct |
9 ms |
33372 KB |
Output is correct |
29 |
Correct |
10 ms |
33372 KB |
Output is correct |
30 |
Correct |
10 ms |
33116 KB |
Output is correct |
31 |
Correct |
7 ms |
33112 KB |
Output is correct |
32 |
Correct |
9 ms |
33556 KB |
Output is correct |
33 |
Correct |
7 ms |
33116 KB |
Output is correct |
34 |
Correct |
8 ms |
33116 KB |
Output is correct |
35 |
Correct |
9 ms |
33372 KB |
Output is correct |
36 |
Correct |
9 ms |
33116 KB |
Output is correct |
37 |
Correct |
9 ms |
33252 KB |
Output is correct |
38 |
Correct |
7 ms |
33112 KB |
Output is correct |
39 |
Correct |
9 ms |
33116 KB |
Output is correct |
40 |
Correct |
9 ms |
33300 KB |
Output is correct |
41 |
Correct |
9 ms |
33116 KB |
Output is correct |
42 |
Correct |
9 ms |
33208 KB |
Output is correct |
43 |
Correct |
9 ms |
33628 KB |
Output is correct |
44 |
Correct |
7 ms |
33116 KB |
Output is correct |
45 |
Correct |
9 ms |
33372 KB |
Output is correct |
46 |
Correct |
10 ms |
33372 KB |
Output is correct |
47 |
Correct |
7 ms |
33116 KB |
Output is correct |
48 |
Correct |
10 ms |
33116 KB |
Output is correct |
49 |
Correct |
8 ms |
33372 KB |
Output is correct |
50 |
Correct |
9 ms |
33372 KB |
Output is correct |
51 |
Correct |
9 ms |
33220 KB |
Output is correct |
52 |
Correct |
8 ms |
33116 KB |
Output is correct |
53 |
Correct |
8 ms |
33372 KB |
Output is correct |
54 |
Correct |
9 ms |
33116 KB |
Output is correct |
55 |
Correct |
7 ms |
33112 KB |
Output is correct |
56 |
Correct |
56 ms |
45440 KB |
Output is correct |
57 |
Correct |
88 ms |
45168 KB |
Output is correct |
58 |
Correct |
9 ms |
33116 KB |
Output is correct |
59 |
Correct |
9 ms |
33116 KB |
Output is correct |
60 |
Correct |
7 ms |
33116 KB |
Output is correct |
61 |
Correct |
85 ms |
45144 KB |
Output is correct |
62 |
Correct |
8 ms |
33300 KB |
Output is correct |
63 |
Correct |
102 ms |
55128 KB |
Output is correct |
64 |
Correct |
75 ms |
45392 KB |
Output is correct |
65 |
Correct |
8 ms |
33116 KB |
Output is correct |
66 |
Correct |
79 ms |
46248 KB |
Output is correct |
67 |
Correct |
8 ms |
33112 KB |
Output is correct |
68 |
Correct |
9 ms |
33116 KB |
Output is correct |
69 |
Correct |
53 ms |
45344 KB |
Output is correct |
70 |
Correct |
10 ms |
33628 KB |
Output is correct |
71 |
Correct |
81 ms |
45264 KB |
Output is correct |
72 |
Correct |
100 ms |
48280 KB |
Output is correct |
73 |
Correct |
7 ms |
33116 KB |
Output is correct |
74 |
Correct |
92 ms |
55644 KB |
Output is correct |
75 |
Correct |
91 ms |
50396 KB |
Output is correct |
76 |
Correct |
8 ms |
33116 KB |
Output is correct |
77 |
Correct |
9 ms |
33116 KB |
Output is correct |
78 |
Correct |
54 ms |
45404 KB |
Output is correct |
79 |
Correct |
66 ms |
49164 KB |
Output is correct |
80 |
Correct |
8 ms |
33368 KB |
Output is correct |
81 |
Correct |
9 ms |
33116 KB |
Output is correct |
82 |
Correct |
8 ms |
32972 KB |
Output is correct |
83 |
Correct |
8 ms |
33112 KB |
Output is correct |
84 |
Correct |
9 ms |
33116 KB |
Output is correct |
85 |
Correct |
88 ms |
46316 KB |
Output is correct |
86 |
Correct |
8 ms |
33116 KB |
Output is correct |
87 |
Correct |
77 ms |
45116 KB |
Output is correct |
88 |
Correct |
7 ms |
33116 KB |
Output is correct |
89 |
Correct |
78 ms |
45140 KB |
Output is correct |
90 |
Correct |
109 ms |
46720 KB |
Output is correct |
91 |
Correct |
82 ms |
48708 KB |
Output is correct |
92 |
Correct |
52 ms |
45384 KB |
Output is correct |
93 |
Correct |
9 ms |
33372 KB |
Output is correct |
94 |
Correct |
7 ms |
33116 KB |
Output is correct |
95 |
Correct |
72 ms |
49020 KB |
Output is correct |
96 |
Correct |
105 ms |
54316 KB |
Output is correct |
97 |
Correct |
9 ms |
33368 KB |
Output is correct |
98 |
Correct |
7 ms |
33116 KB |
Output is correct |
99 |
Correct |
59 ms |
46928 KB |
Output is correct |
100 |
Correct |
8 ms |
33116 KB |
Output is correct |
101 |
Correct |
75 ms |
45648 KB |
Output is correct |
102 |
Correct |
96 ms |
52428 KB |
Output is correct |
103 |
Correct |
8 ms |
33368 KB |
Output is correct |
104 |
Correct |
8 ms |
33496 KB |
Output is correct |
105 |
Correct |
9 ms |
33116 KB |
Output is correct |
106 |
Correct |
8 ms |
33240 KB |
Output is correct |
107 |
Correct |
10 ms |
33372 KB |
Output is correct |
108 |
Correct |
466 ms |
97820 KB |
Output is correct |
109 |
Correct |
819 ms |
120980 KB |
Output is correct |
110 |
Correct |
830 ms |
136640 KB |
Output is correct |
111 |
Correct |
891 ms |
149712 KB |
Output is correct |
112 |
Correct |
835 ms |
135744 KB |
Output is correct |
113 |
Correct |
500 ms |
116092 KB |
Output is correct |
114 |
Correct |
555 ms |
95476 KB |
Output is correct |
115 |
Correct |
608 ms |
95684 KB |
Output is correct |
116 |
Correct |
764 ms |
100288 KB |
Output is correct |
117 |
Correct |
707 ms |
117172 KB |
Output is correct |
118 |
Correct |
643 ms |
96592 KB |
Output is correct |
119 |
Correct |
681 ms |
117204 KB |
Output is correct |
120 |
Correct |
808 ms |
136364 KB |
Output is correct |
121 |
Correct |
679 ms |
115908 KB |
Output is correct |
122 |
Correct |
798 ms |
106280 KB |
Output is correct |
123 |
Correct |
512 ms |
117612 KB |
Output is correct |
124 |
Correct |
892 ms |
115588 KB |
Output is correct |