#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
#define pb push_back
#define f first
#define s second
namespace debug {
const int DEBUG = true;
template<class T1, class T2>
void pr(const pair<T1, T2> &x);
template<class T, size_t SZ>
void pr(const array<T, SZ> &x);
template<class T>
void pr(const vector<T> &x);
template<class T>
void pr(const set<T> &x);
template<class T1, class T2>
void pr(const map<T1, T2> &x);
template<class T>
void pr(const T &x) { if (DEBUG) cout << x; }
template<class T, class... Ts>
void pr(const T &first, const Ts &... rest) { pr(first), pr(rest...); }
template<class T1, class T2>
void pr(const pair<T1, T2> &x) { pr("{", x.f, ", ", x.s, "}"); }
template<class T>
void prIn(const T &x) {
pr("{");
bool fst = 1;
for (auto &a : x) {
pr(fst ? "" : ", ", a), fst = 0;
}
pr("}");
}
template<class T, size_t SZ>
void pr(const array<T, SZ> &x) { prIn(x); }
template<class T>
void pr(const vector<T> &x) { prIn(x); }
template<class T>
void pr(const set<T> &x) { prIn(x); }
template<class T1, class T2>
void pr(const map<T1, T2> &x) { prIn(x); }
void ps() { pr("\n"), cout << flush; }
template<class Arg, class... Args>
void ps(const Arg &first, const Args &... rest) {
pr(first, " ");
ps(rest...);
}
}
using namespace debug;
int N, K;
const int MAXN = 1e5 + 100;
const int LOGN = 20;
vi aL[MAXN];
int anc[MAXN][LOGN];
int depth[MAXN];
void dfs(int cV, int pV) {
for (int aV : aL[cV]) {
if (aV != pV) {
depth[aV] = depth[cV] + 1;
dfs(aV, cV);
anc[aV][0] = cV;
}
}
}
void prep() {
dfs(0, -1);
anc[0][0] = 0;
for (int k = 1; k < LOGN; k++) {
for (int i = 0; i < N; i++) {
anc[i][k] = anc[anc[i][k - 1]][k - 1];
}
}
}
int j(int a, int d) {
for (int i = 0; i < LOGN; i++) {
if (d & (1 << i)) {
a = anc[a][i];
}
}
return a;
}
int fLCA(int a, int b) {
if (depth[a] > depth[b]) swap(a, b);
b = j(b, depth[b] - depth[a]);
if (a == b) return a;
for (int i = LOGN - 1; i >= 0; i--) {
if (anc[a][i] != anc[b][i]) {
a = anc[a][i];
b = anc[b][i];
}
}
return anc[a][0];
}
int st[MAXN];
vi bySt[MAXN];
int mark[MAXN];
vector<pi> mrg;
int dfs2(int cV, int pV) {
int sum = mark[cV];
for (int aV : aL[cV]) {
if (aV != pV) {
sum += dfs2(aV, cV);
}
}
// ps(cV, sum);
if (sum) {
mrg.pb({cV, pV});
}
return sum;
}
struct DSU {
int anc[MAXN];
void init() {
for (int i = 0; i < MAXN; i++) {
anc[i] = i;
}
}
int fRt(int i) {
return anc[i] == i ? i : anc[i] = fRt(anc[i]);
}
bool merge(int a, int b) {
a = fRt(a), b = fRt(b);
if (a == b) {
return false;
}
anc[a] = b;
return true;
}
} dsu;
int id[MAXN];
vi cAL[MAXN];
int main() {
cin >> N >> K;
for (int i = 0; i < N - 1; i++) {
int x, y;
cin >> x >> y;
x--, y--;
aL[x].pb(y);
aL[y].pb(x);
}
for (int i = 0; i < N; i++) {
cin >> st[i];
bySt[st[i]].pb(i);
}
prep();
// ps(fLCA(5, 7));
memset(mark, 0, sizeof(mark));
for (vi &list : bySt) {
for (int i = 0; i + 1 < list.size(); i++) {
int x = list[i];
int y = list[i + 1];
int lca = fLCA(x, y);
// ps(x, y, lca);
mark[x]++;
mark[y]++;
mark[lca] -= 2;
}
}
// for (int i = 0; i < N; i++) {
// ps(mark[i]);
// }
dfs2(0, -1);
// ps("mrg:", mrg);
dsu.init();
for (pi x : mrg) {
dsu.merge(x.f, x.s);
}
for (int i = 0; i < N; i++) {
id[i] = dsu.fRt(i);
}
for (int cV = 0; cV < N; cV++) {
for (int aV :aL[cV]) {
if (id[cV] != id[aV]) {
cAL[id[cV]].pb(id[aV]);
}
}
}
for (int i = 0; i < N; i++) {
sort(cAL[i].begin(), cAL[i].end());
cAL[i].erase(unique(cAL[i].begin(), cAL[i].end()), cAL[i].end());
// ps(cAL[i]);
}
int leaves = 0;
for (int i = 0; i < N; i++) {
if (cAL[i].size() == 1) {
leaves++;
}
}
cout << (leaves + 1) / 2 << endl;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:204:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i + 1 < list.size(); i++) {
~~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8184 KB |
Output is correct |
2 |
Correct |
9 ms |
8188 KB |
Output is correct |
3 |
Correct |
10 ms |
8184 KB |
Output is correct |
4 |
Correct |
10 ms |
8184 KB |
Output is correct |
5 |
Correct |
10 ms |
8184 KB |
Output is correct |
6 |
Correct |
10 ms |
8184 KB |
Output is correct |
7 |
Correct |
10 ms |
8184 KB |
Output is correct |
8 |
Correct |
10 ms |
8188 KB |
Output is correct |
9 |
Correct |
10 ms |
8160 KB |
Output is correct |
10 |
Correct |
10 ms |
8184 KB |
Output is correct |
11 |
Correct |
10 ms |
8184 KB |
Output is correct |
12 |
Correct |
10 ms |
8184 KB |
Output is correct |
13 |
Correct |
10 ms |
8204 KB |
Output is correct |
14 |
Correct |
10 ms |
8184 KB |
Output is correct |
15 |
Correct |
10 ms |
8184 KB |
Output is correct |
16 |
Correct |
10 ms |
8184 KB |
Output is correct |
17 |
Correct |
10 ms |
8176 KB |
Output is correct |
18 |
Correct |
10 ms |
8184 KB |
Output is correct |
19 |
Correct |
10 ms |
8184 KB |
Output is correct |
20 |
Correct |
10 ms |
8184 KB |
Output is correct |
21 |
Correct |
10 ms |
8184 KB |
Output is correct |
22 |
Correct |
10 ms |
8184 KB |
Output is correct |
23 |
Correct |
10 ms |
8184 KB |
Output is correct |
24 |
Correct |
10 ms |
8184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8184 KB |
Output is correct |
2 |
Correct |
9 ms |
8188 KB |
Output is correct |
3 |
Correct |
10 ms |
8184 KB |
Output is correct |
4 |
Correct |
10 ms |
8184 KB |
Output is correct |
5 |
Correct |
10 ms |
8184 KB |
Output is correct |
6 |
Correct |
10 ms |
8184 KB |
Output is correct |
7 |
Correct |
10 ms |
8184 KB |
Output is correct |
8 |
Correct |
10 ms |
8188 KB |
Output is correct |
9 |
Correct |
10 ms |
8160 KB |
Output is correct |
10 |
Correct |
10 ms |
8184 KB |
Output is correct |
11 |
Correct |
10 ms |
8184 KB |
Output is correct |
12 |
Correct |
10 ms |
8184 KB |
Output is correct |
13 |
Correct |
10 ms |
8204 KB |
Output is correct |
14 |
Correct |
10 ms |
8184 KB |
Output is correct |
15 |
Correct |
10 ms |
8184 KB |
Output is correct |
16 |
Correct |
10 ms |
8184 KB |
Output is correct |
17 |
Correct |
10 ms |
8176 KB |
Output is correct |
18 |
Correct |
10 ms |
8184 KB |
Output is correct |
19 |
Correct |
10 ms |
8184 KB |
Output is correct |
20 |
Correct |
10 ms |
8184 KB |
Output is correct |
21 |
Correct |
10 ms |
8184 KB |
Output is correct |
22 |
Correct |
10 ms |
8184 KB |
Output is correct |
23 |
Correct |
10 ms |
8184 KB |
Output is correct |
24 |
Correct |
10 ms |
8184 KB |
Output is correct |
25 |
Correct |
10 ms |
8184 KB |
Output is correct |
26 |
Correct |
14 ms |
8696 KB |
Output is correct |
27 |
Correct |
14 ms |
8696 KB |
Output is correct |
28 |
Correct |
15 ms |
8824 KB |
Output is correct |
29 |
Correct |
14 ms |
8828 KB |
Output is correct |
30 |
Correct |
14 ms |
8696 KB |
Output is correct |
31 |
Correct |
10 ms |
8184 KB |
Output is correct |
32 |
Correct |
14 ms |
8824 KB |
Output is correct |
33 |
Correct |
11 ms |
8184 KB |
Output is correct |
34 |
Correct |
13 ms |
8700 KB |
Output is correct |
35 |
Correct |
15 ms |
8824 KB |
Output is correct |
36 |
Correct |
14 ms |
8696 KB |
Output is correct |
37 |
Correct |
14 ms |
8696 KB |
Output is correct |
38 |
Correct |
10 ms |
8184 KB |
Output is correct |
39 |
Correct |
14 ms |
8696 KB |
Output is correct |
40 |
Correct |
14 ms |
8696 KB |
Output is correct |
41 |
Correct |
13 ms |
8696 KB |
Output is correct |
42 |
Correct |
14 ms |
8696 KB |
Output is correct |
43 |
Correct |
14 ms |
8828 KB |
Output is correct |
44 |
Correct |
10 ms |
8184 KB |
Output is correct |
45 |
Correct |
15 ms |
8824 KB |
Output is correct |
46 |
Correct |
14 ms |
8696 KB |
Output is correct |
47 |
Correct |
11 ms |
8184 KB |
Output is correct |
48 |
Correct |
14 ms |
8696 KB |
Output is correct |
49 |
Correct |
14 ms |
8824 KB |
Output is correct |
50 |
Correct |
15 ms |
8824 KB |
Output is correct |
51 |
Correct |
14 ms |
8696 KB |
Output is correct |
52 |
Correct |
13 ms |
8696 KB |
Output is correct |
53 |
Correct |
14 ms |
8696 KB |
Output is correct |
54 |
Correct |
14 ms |
8696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8184 KB |
Output is correct |
2 |
Correct |
9 ms |
8188 KB |
Output is correct |
3 |
Correct |
10 ms |
8184 KB |
Output is correct |
4 |
Correct |
10 ms |
8184 KB |
Output is correct |
5 |
Correct |
10 ms |
8184 KB |
Output is correct |
6 |
Correct |
10 ms |
8184 KB |
Output is correct |
7 |
Correct |
10 ms |
8184 KB |
Output is correct |
8 |
Correct |
10 ms |
8188 KB |
Output is correct |
9 |
Correct |
10 ms |
8160 KB |
Output is correct |
10 |
Correct |
10 ms |
8184 KB |
Output is correct |
11 |
Correct |
10 ms |
8184 KB |
Output is correct |
12 |
Correct |
10 ms |
8184 KB |
Output is correct |
13 |
Correct |
10 ms |
8204 KB |
Output is correct |
14 |
Correct |
10 ms |
8184 KB |
Output is correct |
15 |
Correct |
10 ms |
8184 KB |
Output is correct |
16 |
Correct |
10 ms |
8184 KB |
Output is correct |
17 |
Correct |
10 ms |
8176 KB |
Output is correct |
18 |
Correct |
10 ms |
8184 KB |
Output is correct |
19 |
Correct |
10 ms |
8184 KB |
Output is correct |
20 |
Correct |
10 ms |
8184 KB |
Output is correct |
21 |
Correct |
10 ms |
8184 KB |
Output is correct |
22 |
Correct |
10 ms |
8184 KB |
Output is correct |
23 |
Correct |
10 ms |
8184 KB |
Output is correct |
24 |
Correct |
10 ms |
8184 KB |
Output is correct |
25 |
Correct |
10 ms |
8184 KB |
Output is correct |
26 |
Correct |
175 ms |
23548 KB |
Output is correct |
27 |
Correct |
208 ms |
23272 KB |
Output is correct |
28 |
Correct |
14 ms |
8696 KB |
Output is correct |
29 |
Correct |
10 ms |
8184 KB |
Output is correct |
30 |
Correct |
10 ms |
8184 KB |
Output is correct |
31 |
Correct |
207 ms |
23156 KB |
Output is correct |
32 |
Correct |
13 ms |
8696 KB |
Output is correct |
33 |
Correct |
255 ms |
28880 KB |
Output is correct |
34 |
Correct |
205 ms |
23308 KB |
Output is correct |
35 |
Correct |
14 ms |
8696 KB |
Output is correct |
36 |
Correct |
220 ms |
23676 KB |
Output is correct |
37 |
Correct |
14 ms |
8696 KB |
Output is correct |
38 |
Correct |
15 ms |
8696 KB |
Output is correct |
39 |
Correct |
177 ms |
23544 KB |
Output is correct |
40 |
Correct |
14 ms |
8824 KB |
Output is correct |
41 |
Correct |
194 ms |
23024 KB |
Output is correct |
42 |
Correct |
267 ms |
25064 KB |
Output is correct |
43 |
Correct |
10 ms |
8184 KB |
Output is correct |
44 |
Correct |
243 ms |
29016 KB |
Output is correct |
45 |
Correct |
236 ms |
26240 KB |
Output is correct |
46 |
Correct |
14 ms |
8696 KB |
Output is correct |
47 |
Correct |
14 ms |
8696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
171 ms |
22140 KB |
Output is correct |
2 |
Correct |
198 ms |
27368 KB |
Output is correct |
3 |
Correct |
14 ms |
8696 KB |
Output is correct |
4 |
Correct |
14 ms |
8696 KB |
Output is correct |
5 |
Correct |
10 ms |
8184 KB |
Output is correct |
6 |
Correct |
10 ms |
8184 KB |
Output is correct |
7 |
Correct |
14 ms |
8700 KB |
Output is correct |
8 |
Correct |
230 ms |
24808 KB |
Output is correct |
9 |
Correct |
14 ms |
8696 KB |
Output is correct |
10 |
Correct |
206 ms |
23412 KB |
Output is correct |
11 |
Correct |
10 ms |
8312 KB |
Output is correct |
12 |
Correct |
226 ms |
23272 KB |
Output is correct |
13 |
Correct |
234 ms |
24940 KB |
Output is correct |
14 |
Correct |
224 ms |
28536 KB |
Output is correct |
15 |
Correct |
167 ms |
23416 KB |
Output is correct |
16 |
Correct |
14 ms |
8696 KB |
Output is correct |
17 |
Correct |
10 ms |
8184 KB |
Output is correct |
18 |
Correct |
203 ms |
28268 KB |
Output is correct |
19 |
Correct |
241 ms |
31480 KB |
Output is correct |
20 |
Correct |
14 ms |
8696 KB |
Output is correct |
21 |
Correct |
10 ms |
8184 KB |
Output is correct |
22 |
Correct |
189 ms |
25128 KB |
Output is correct |
23 |
Correct |
14 ms |
8696 KB |
Output is correct |
24 |
Correct |
225 ms |
23788 KB |
Output is correct |
25 |
Correct |
234 ms |
30456 KB |
Output is correct |
26 |
Correct |
15 ms |
8824 KB |
Output is correct |
27 |
Correct |
14 ms |
8824 KB |
Output is correct |
28 |
Correct |
14 ms |
8696 KB |
Output is correct |
29 |
Correct |
14 ms |
8696 KB |
Output is correct |
30 |
Correct |
14 ms |
8696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8184 KB |
Output is correct |
2 |
Correct |
9 ms |
8188 KB |
Output is correct |
3 |
Correct |
10 ms |
8184 KB |
Output is correct |
4 |
Correct |
10 ms |
8184 KB |
Output is correct |
5 |
Correct |
10 ms |
8184 KB |
Output is correct |
6 |
Correct |
10 ms |
8184 KB |
Output is correct |
7 |
Correct |
10 ms |
8184 KB |
Output is correct |
8 |
Correct |
10 ms |
8188 KB |
Output is correct |
9 |
Correct |
10 ms |
8160 KB |
Output is correct |
10 |
Correct |
10 ms |
8184 KB |
Output is correct |
11 |
Correct |
10 ms |
8184 KB |
Output is correct |
12 |
Correct |
10 ms |
8184 KB |
Output is correct |
13 |
Correct |
10 ms |
8204 KB |
Output is correct |
14 |
Correct |
10 ms |
8184 KB |
Output is correct |
15 |
Correct |
10 ms |
8184 KB |
Output is correct |
16 |
Correct |
10 ms |
8184 KB |
Output is correct |
17 |
Correct |
10 ms |
8176 KB |
Output is correct |
18 |
Correct |
10 ms |
8184 KB |
Output is correct |
19 |
Correct |
10 ms |
8184 KB |
Output is correct |
20 |
Correct |
10 ms |
8184 KB |
Output is correct |
21 |
Correct |
10 ms |
8184 KB |
Output is correct |
22 |
Correct |
10 ms |
8184 KB |
Output is correct |
23 |
Correct |
10 ms |
8184 KB |
Output is correct |
24 |
Correct |
10 ms |
8184 KB |
Output is correct |
25 |
Correct |
10 ms |
8184 KB |
Output is correct |
26 |
Correct |
14 ms |
8696 KB |
Output is correct |
27 |
Correct |
14 ms |
8696 KB |
Output is correct |
28 |
Correct |
15 ms |
8824 KB |
Output is correct |
29 |
Correct |
14 ms |
8828 KB |
Output is correct |
30 |
Correct |
14 ms |
8696 KB |
Output is correct |
31 |
Correct |
10 ms |
8184 KB |
Output is correct |
32 |
Correct |
14 ms |
8824 KB |
Output is correct |
33 |
Correct |
11 ms |
8184 KB |
Output is correct |
34 |
Correct |
13 ms |
8700 KB |
Output is correct |
35 |
Correct |
15 ms |
8824 KB |
Output is correct |
36 |
Correct |
14 ms |
8696 KB |
Output is correct |
37 |
Correct |
14 ms |
8696 KB |
Output is correct |
38 |
Correct |
10 ms |
8184 KB |
Output is correct |
39 |
Correct |
14 ms |
8696 KB |
Output is correct |
40 |
Correct |
14 ms |
8696 KB |
Output is correct |
41 |
Correct |
13 ms |
8696 KB |
Output is correct |
42 |
Correct |
14 ms |
8696 KB |
Output is correct |
43 |
Correct |
14 ms |
8828 KB |
Output is correct |
44 |
Correct |
10 ms |
8184 KB |
Output is correct |
45 |
Correct |
15 ms |
8824 KB |
Output is correct |
46 |
Correct |
14 ms |
8696 KB |
Output is correct |
47 |
Correct |
11 ms |
8184 KB |
Output is correct |
48 |
Correct |
14 ms |
8696 KB |
Output is correct |
49 |
Correct |
14 ms |
8824 KB |
Output is correct |
50 |
Correct |
15 ms |
8824 KB |
Output is correct |
51 |
Correct |
14 ms |
8696 KB |
Output is correct |
52 |
Correct |
13 ms |
8696 KB |
Output is correct |
53 |
Correct |
14 ms |
8696 KB |
Output is correct |
54 |
Correct |
14 ms |
8696 KB |
Output is correct |
55 |
Correct |
10 ms |
8184 KB |
Output is correct |
56 |
Correct |
175 ms |
23548 KB |
Output is correct |
57 |
Correct |
208 ms |
23272 KB |
Output is correct |
58 |
Correct |
14 ms |
8696 KB |
Output is correct |
59 |
Correct |
10 ms |
8184 KB |
Output is correct |
60 |
Correct |
10 ms |
8184 KB |
Output is correct |
61 |
Correct |
207 ms |
23156 KB |
Output is correct |
62 |
Correct |
13 ms |
8696 KB |
Output is correct |
63 |
Correct |
255 ms |
28880 KB |
Output is correct |
64 |
Correct |
205 ms |
23308 KB |
Output is correct |
65 |
Correct |
14 ms |
8696 KB |
Output is correct |
66 |
Correct |
220 ms |
23676 KB |
Output is correct |
67 |
Correct |
14 ms |
8696 KB |
Output is correct |
68 |
Correct |
15 ms |
8696 KB |
Output is correct |
69 |
Correct |
177 ms |
23544 KB |
Output is correct |
70 |
Correct |
14 ms |
8824 KB |
Output is correct |
71 |
Correct |
194 ms |
23024 KB |
Output is correct |
72 |
Correct |
267 ms |
25064 KB |
Output is correct |
73 |
Correct |
10 ms |
8184 KB |
Output is correct |
74 |
Correct |
243 ms |
29016 KB |
Output is correct |
75 |
Correct |
236 ms |
26240 KB |
Output is correct |
76 |
Correct |
14 ms |
8696 KB |
Output is correct |
77 |
Correct |
14 ms |
8696 KB |
Output is correct |
78 |
Correct |
171 ms |
22140 KB |
Output is correct |
79 |
Correct |
198 ms |
27368 KB |
Output is correct |
80 |
Correct |
14 ms |
8696 KB |
Output is correct |
81 |
Correct |
14 ms |
8696 KB |
Output is correct |
82 |
Correct |
10 ms |
8184 KB |
Output is correct |
83 |
Correct |
10 ms |
8184 KB |
Output is correct |
84 |
Correct |
14 ms |
8700 KB |
Output is correct |
85 |
Correct |
230 ms |
24808 KB |
Output is correct |
86 |
Correct |
14 ms |
8696 KB |
Output is correct |
87 |
Correct |
206 ms |
23412 KB |
Output is correct |
88 |
Correct |
10 ms |
8312 KB |
Output is correct |
89 |
Correct |
226 ms |
23272 KB |
Output is correct |
90 |
Correct |
234 ms |
24940 KB |
Output is correct |
91 |
Correct |
224 ms |
28536 KB |
Output is correct |
92 |
Correct |
167 ms |
23416 KB |
Output is correct |
93 |
Correct |
14 ms |
8696 KB |
Output is correct |
94 |
Correct |
10 ms |
8184 KB |
Output is correct |
95 |
Correct |
203 ms |
28268 KB |
Output is correct |
96 |
Correct |
241 ms |
31480 KB |
Output is correct |
97 |
Correct |
14 ms |
8696 KB |
Output is correct |
98 |
Correct |
10 ms |
8184 KB |
Output is correct |
99 |
Correct |
189 ms |
25128 KB |
Output is correct |
100 |
Correct |
14 ms |
8696 KB |
Output is correct |
101 |
Correct |
225 ms |
23788 KB |
Output is correct |
102 |
Correct |
234 ms |
30456 KB |
Output is correct |
103 |
Correct |
15 ms |
8824 KB |
Output is correct |
104 |
Correct |
14 ms |
8824 KB |
Output is correct |
105 |
Correct |
14 ms |
8696 KB |
Output is correct |
106 |
Correct |
14 ms |
8696 KB |
Output is correct |
107 |
Correct |
14 ms |
8696 KB |
Output is correct |
108 |
Runtime error |
17 ms |
14712 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
109 |
Halted |
0 ms |
0 KB |
- |