Submission #1033016

# Submission time Handle Problem Language Result Execution time Memory
1033016 2024-07-24T12:01:48 Z juicy Mergers (JOI19_mergers) C++17
100 / 100
1081 ms 103388 KB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

const int N = 5e5 + 5, LG = 19;

int n, k;
int dep[N], pr[LG][N], lab[N], top[N], lst[N], deg[N];
vector<int> g[N];

void dfs(int u) {
	for (int v : g[u]) {
		if (v != pr[0][u]) {
			pr[0][v] = u;
			for (int j = 1; j < LG; ++j) {
				pr[j][v] = pr[j - 1][pr[j - 1][v]];
			}
			dep[v] = dep[u] + 1;
			dfs(v);
		}
	}
} 

int get(int u) {
	return lab[u] < 0 ? u : lab[u] = get(lab[u]);
}

void unite(int u, int v) {
	u = get(u);
	v = get(v);
	if (u != v) {
		if (lab[u] > lab[v]) {
			swap(u, v);
		}
		lab[u] += lab[v];
		lab[v] = u;
		if (dep[top[v]] < dep[top[u]]) {
			top[u] = top[v];
		}
	}
}

int lca(int u, int v) {
	if (dep[u] < dep[v]) {
		swap(u, v);
	}
	for (int j = LG - 1; ~j; --j) {
		if (dep[u] - (1 << j) >= dep[v]) {
			u = pr[j][u];
		}
	}
	if (u == v) {
		return u;
	}
	for (int j = LG - 1; ~j; --j) {
		if (pr[j][u] ^ pr[j][v]) {
			u = pr[j][u], v = pr[j][v];
		}
	}
	return pr[0][u];
}

void up(int u, int x) {
	while (dep[top[get(u)]] >= dep[x]) {
		unite(u, top[get(u)]);
	}
}

void add(int u, int v) {
	int x = lca(u, v);
	up(u, x);
	up(v, x);
}

int main() {
	ios::sync_with_stdio(false); cin.tie(nullptr);

	cin >> n >> k;
	for (int i = 1; i < n; ++i) {
		int u, v; cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	dep[1] = 1;
	dfs(1);
	for (int i = 1; i <= n; ++i) {
		top[i] = pr[0][i];
		lab[i] = -1;
	}
	for (int i = 1; i <= n; ++i) {
		int c; cin >> c;
		if (lst[c]) {
			add(lst[c], i);
		}
		lst[c] = i;
	}
	for (int u = 1; u <= n; ++u) {
		for (int v : g[u]) {
			if (get(u) != get(v)) {
				++deg[get(v)];
			}
		}
	}
	int cnt = count(deg + 1, deg + n + 1, 1);
	if (cnt & 1) {
		cout << cnt / 2 + 1;
		exit(0);
	}
	cout << cnt / 2;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 12380 KB Output is correct
2 Correct 6 ms 12380 KB Output is correct
3 Correct 5 ms 12632 KB Output is correct
4 Correct 5 ms 12380 KB Output is correct
5 Correct 5 ms 12388 KB Output is correct
6 Correct 5 ms 12380 KB Output is correct
7 Correct 5 ms 12120 KB Output is correct
8 Correct 5 ms 12380 KB Output is correct
9 Correct 5 ms 12240 KB Output is correct
10 Correct 5 ms 12376 KB Output is correct
11 Correct 5 ms 12380 KB Output is correct
12 Correct 8 ms 12208 KB Output is correct
13 Correct 6 ms 12376 KB Output is correct
14 Correct 5 ms 12380 KB Output is correct
15 Correct 5 ms 12208 KB Output is correct
16 Correct 5 ms 12380 KB Output is correct
17 Correct 5 ms 12380 KB Output is correct
18 Correct 5 ms 12380 KB Output is correct
19 Correct 5 ms 12380 KB Output is correct
20 Correct 5 ms 12380 KB Output is correct
21 Correct 5 ms 12376 KB Output is correct
22 Correct 5 ms 12228 KB Output is correct
23 Correct 8 ms 12380 KB Output is correct
24 Correct 5 ms 12380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 12380 KB Output is correct
2 Correct 6 ms 12380 KB Output is correct
3 Correct 5 ms 12632 KB Output is correct
4 Correct 5 ms 12380 KB Output is correct
5 Correct 5 ms 12388 KB Output is correct
6 Correct 5 ms 12380 KB Output is correct
7 Correct 5 ms 12120 KB Output is correct
8 Correct 5 ms 12380 KB Output is correct
9 Correct 5 ms 12240 KB Output is correct
10 Correct 5 ms 12376 KB Output is correct
11 Correct 5 ms 12380 KB Output is correct
12 Correct 8 ms 12208 KB Output is correct
13 Correct 6 ms 12376 KB Output is correct
14 Correct 5 ms 12380 KB Output is correct
15 Correct 5 ms 12208 KB Output is correct
16 Correct 5 ms 12380 KB Output is correct
17 Correct 5 ms 12380 KB Output is correct
18 Correct 5 ms 12380 KB Output is correct
19 Correct 5 ms 12380 KB Output is correct
20 Correct 5 ms 12380 KB Output is correct
21 Correct 5 ms 12376 KB Output is correct
22 Correct 5 ms 12228 KB Output is correct
23 Correct 8 ms 12380 KB Output is correct
24 Correct 5 ms 12380 KB Output is correct
25 Correct 5 ms 12380 KB Output is correct
26 Correct 6 ms 12736 KB Output is correct
27 Correct 7 ms 12636 KB Output is correct
28 Correct 6 ms 12892 KB Output is correct
29 Correct 6 ms 12636 KB Output is correct
30 Correct 6 ms 12636 KB Output is correct
31 Correct 5 ms 12380 KB Output is correct
32 Correct 7 ms 12888 KB Output is correct
33 Correct 5 ms 12380 KB Output is correct
34 Correct 6 ms 12636 KB Output is correct
35 Correct 6 ms 12828 KB Output is correct
36 Correct 6 ms 12644 KB Output is correct
37 Correct 6 ms 12644 KB Output is correct
38 Correct 5 ms 12380 KB Output is correct
39 Correct 7 ms 12724 KB Output is correct
40 Correct 6 ms 12636 KB Output is correct
41 Correct 6 ms 12636 KB Output is correct
42 Correct 7 ms 12636 KB Output is correct
43 Correct 6 ms 12892 KB Output is correct
44 Correct 5 ms 12380 KB Output is correct
45 Correct 6 ms 12812 KB Output is correct
46 Correct 6 ms 12632 KB Output is correct
47 Correct 5 ms 12404 KB Output is correct
48 Correct 6 ms 12636 KB Output is correct
49 Correct 6 ms 12636 KB Output is correct
50 Correct 6 ms 12736 KB Output is correct
51 Correct 6 ms 12636 KB Output is correct
52 Correct 6 ms 12632 KB Output is correct
53 Correct 7 ms 12636 KB Output is correct
54 Correct 8 ms 12632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 12380 KB Output is correct
2 Correct 6 ms 12380 KB Output is correct
3 Correct 5 ms 12632 KB Output is correct
4 Correct 5 ms 12380 KB Output is correct
5 Correct 5 ms 12388 KB Output is correct
6 Correct 5 ms 12380 KB Output is correct
7 Correct 5 ms 12120 KB Output is correct
8 Correct 5 ms 12380 KB Output is correct
9 Correct 5 ms 12240 KB Output is correct
10 Correct 5 ms 12376 KB Output is correct
11 Correct 5 ms 12380 KB Output is correct
12 Correct 8 ms 12208 KB Output is correct
13 Correct 6 ms 12376 KB Output is correct
14 Correct 5 ms 12380 KB Output is correct
15 Correct 5 ms 12208 KB Output is correct
16 Correct 5 ms 12380 KB Output is correct
17 Correct 5 ms 12380 KB Output is correct
18 Correct 5 ms 12380 KB Output is correct
19 Correct 5 ms 12380 KB Output is correct
20 Correct 5 ms 12380 KB Output is correct
21 Correct 5 ms 12376 KB Output is correct
22 Correct 5 ms 12228 KB Output is correct
23 Correct 8 ms 12380 KB Output is correct
24 Correct 5 ms 12380 KB Output is correct
25 Correct 6 ms 12380 KB Output is correct
26 Correct 46 ms 25800 KB Output is correct
27 Correct 53 ms 25424 KB Output is correct
28 Correct 6 ms 12636 KB Output is correct
29 Correct 7 ms 12388 KB Output is correct
30 Correct 5 ms 12380 KB Output is correct
31 Correct 61 ms 25648 KB Output is correct
32 Correct 8 ms 12736 KB Output is correct
33 Correct 95 ms 31184 KB Output is correct
34 Correct 62 ms 25564 KB Output is correct
35 Correct 7 ms 12636 KB Output is correct
36 Correct 82 ms 26080 KB Output is correct
37 Correct 7 ms 12636 KB Output is correct
38 Correct 7 ms 12552 KB Output is correct
39 Correct 50 ms 25808 KB Output is correct
40 Correct 11 ms 13148 KB Output is correct
41 Correct 73 ms 25372 KB Output is correct
42 Correct 114 ms 27328 KB Output is correct
43 Correct 6 ms 12380 KB Output is correct
44 Correct 96 ms 31340 KB Output is correct
45 Correct 91 ms 28528 KB Output is correct
46 Correct 6 ms 12636 KB Output is correct
47 Correct 7 ms 12636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 25816 KB Output is correct
2 Correct 43 ms 26828 KB Output is correct
3 Correct 7 ms 12640 KB Output is correct
4 Correct 8 ms 12640 KB Output is correct
5 Correct 5 ms 12216 KB Output is correct
6 Correct 6 ms 12380 KB Output is correct
7 Correct 6 ms 12680 KB Output is correct
8 Correct 68 ms 26344 KB Output is correct
9 Correct 7 ms 12636 KB Output is correct
10 Correct 67 ms 25684 KB Output is correct
11 Correct 6 ms 12380 KB Output is correct
12 Correct 64 ms 25712 KB Output is correct
13 Correct 61 ms 26452 KB Output is correct
14 Correct 52 ms 26708 KB Output is correct
15 Correct 48 ms 25888 KB Output is correct
16 Correct 7 ms 12636 KB Output is correct
17 Correct 5 ms 12380 KB Output is correct
18 Correct 54 ms 26888 KB Output is correct
19 Correct 56 ms 29736 KB Output is correct
20 Correct 6 ms 12740 KB Output is correct
21 Correct 5 ms 12380 KB Output is correct
22 Correct 60 ms 26484 KB Output is correct
23 Correct 6 ms 12636 KB Output is correct
24 Correct 71 ms 25936 KB Output is correct
25 Correct 56 ms 28756 KB Output is correct
26 Correct 6 ms 12764 KB Output is correct
27 Correct 8 ms 12636 KB Output is correct
28 Correct 7 ms 12636 KB Output is correct
29 Correct 6 ms 12888 KB Output is correct
30 Correct 6 ms 12636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 12380 KB Output is correct
2 Correct 6 ms 12380 KB Output is correct
3 Correct 5 ms 12632 KB Output is correct
4 Correct 5 ms 12380 KB Output is correct
5 Correct 5 ms 12388 KB Output is correct
6 Correct 5 ms 12380 KB Output is correct
7 Correct 5 ms 12120 KB Output is correct
8 Correct 5 ms 12380 KB Output is correct
9 Correct 5 ms 12240 KB Output is correct
10 Correct 5 ms 12376 KB Output is correct
11 Correct 5 ms 12380 KB Output is correct
12 Correct 8 ms 12208 KB Output is correct
13 Correct 6 ms 12376 KB Output is correct
14 Correct 5 ms 12380 KB Output is correct
15 Correct 5 ms 12208 KB Output is correct
16 Correct 5 ms 12380 KB Output is correct
17 Correct 5 ms 12380 KB Output is correct
18 Correct 5 ms 12380 KB Output is correct
19 Correct 5 ms 12380 KB Output is correct
20 Correct 5 ms 12380 KB Output is correct
21 Correct 5 ms 12376 KB Output is correct
22 Correct 5 ms 12228 KB Output is correct
23 Correct 8 ms 12380 KB Output is correct
24 Correct 5 ms 12380 KB Output is correct
25 Correct 5 ms 12380 KB Output is correct
26 Correct 6 ms 12736 KB Output is correct
27 Correct 7 ms 12636 KB Output is correct
28 Correct 6 ms 12892 KB Output is correct
29 Correct 6 ms 12636 KB Output is correct
30 Correct 6 ms 12636 KB Output is correct
31 Correct 5 ms 12380 KB Output is correct
32 Correct 7 ms 12888 KB Output is correct
33 Correct 5 ms 12380 KB Output is correct
34 Correct 6 ms 12636 KB Output is correct
35 Correct 6 ms 12828 KB Output is correct
36 Correct 6 ms 12644 KB Output is correct
37 Correct 6 ms 12644 KB Output is correct
38 Correct 5 ms 12380 KB Output is correct
39 Correct 7 ms 12724 KB Output is correct
40 Correct 6 ms 12636 KB Output is correct
41 Correct 6 ms 12636 KB Output is correct
42 Correct 7 ms 12636 KB Output is correct
43 Correct 6 ms 12892 KB Output is correct
44 Correct 5 ms 12380 KB Output is correct
45 Correct 6 ms 12812 KB Output is correct
46 Correct 6 ms 12632 KB Output is correct
47 Correct 5 ms 12404 KB Output is correct
48 Correct 6 ms 12636 KB Output is correct
49 Correct 6 ms 12636 KB Output is correct
50 Correct 6 ms 12736 KB Output is correct
51 Correct 6 ms 12636 KB Output is correct
52 Correct 6 ms 12632 KB Output is correct
53 Correct 7 ms 12636 KB Output is correct
54 Correct 8 ms 12632 KB Output is correct
55 Correct 6 ms 12380 KB Output is correct
56 Correct 46 ms 25800 KB Output is correct
57 Correct 53 ms 25424 KB Output is correct
58 Correct 6 ms 12636 KB Output is correct
59 Correct 7 ms 12388 KB Output is correct
60 Correct 5 ms 12380 KB Output is correct
61 Correct 61 ms 25648 KB Output is correct
62 Correct 8 ms 12736 KB Output is correct
63 Correct 95 ms 31184 KB Output is correct
64 Correct 62 ms 25564 KB Output is correct
65 Correct 7 ms 12636 KB Output is correct
66 Correct 82 ms 26080 KB Output is correct
67 Correct 7 ms 12636 KB Output is correct
68 Correct 7 ms 12552 KB Output is correct
69 Correct 50 ms 25808 KB Output is correct
70 Correct 11 ms 13148 KB Output is correct
71 Correct 73 ms 25372 KB Output is correct
72 Correct 114 ms 27328 KB Output is correct
73 Correct 6 ms 12380 KB Output is correct
74 Correct 96 ms 31340 KB Output is correct
75 Correct 91 ms 28528 KB Output is correct
76 Correct 6 ms 12636 KB Output is correct
77 Correct 7 ms 12636 KB Output is correct
78 Correct 52 ms 25816 KB Output is correct
79 Correct 43 ms 26828 KB Output is correct
80 Correct 7 ms 12640 KB Output is correct
81 Correct 8 ms 12640 KB Output is correct
82 Correct 5 ms 12216 KB Output is correct
83 Correct 6 ms 12380 KB Output is correct
84 Correct 6 ms 12680 KB Output is correct
85 Correct 68 ms 26344 KB Output is correct
86 Correct 7 ms 12636 KB Output is correct
87 Correct 67 ms 25684 KB Output is correct
88 Correct 6 ms 12380 KB Output is correct
89 Correct 64 ms 25712 KB Output is correct
90 Correct 61 ms 26452 KB Output is correct
91 Correct 52 ms 26708 KB Output is correct
92 Correct 48 ms 25888 KB Output is correct
93 Correct 7 ms 12636 KB Output is correct
94 Correct 5 ms 12380 KB Output is correct
95 Correct 54 ms 26888 KB Output is correct
96 Correct 56 ms 29736 KB Output is correct
97 Correct 6 ms 12740 KB Output is correct
98 Correct 5 ms 12380 KB Output is correct
99 Correct 60 ms 26484 KB Output is correct
100 Correct 6 ms 12636 KB Output is correct
101 Correct 71 ms 25936 KB Output is correct
102 Correct 56 ms 28756 KB Output is correct
103 Correct 6 ms 12764 KB Output is correct
104 Correct 8 ms 12636 KB Output is correct
105 Correct 7 ms 12636 KB Output is correct
106 Correct 6 ms 12888 KB Output is correct
107 Correct 6 ms 12636 KB Output is correct
108 Correct 452 ms 85476 KB Output is correct
109 Correct 1081 ms 93160 KB Output is correct
110 Correct 1030 ms 102128 KB Output is correct
111 Correct 505 ms 103388 KB Output is correct
112 Correct 530 ms 95568 KB Output is correct
113 Correct 317 ms 86580 KB Output is correct
114 Correct 588 ms 79016 KB Output is correct
115 Correct 565 ms 79052 KB Output is correct
116 Correct 687 ms 82768 KB Output is correct
117 Correct 427 ms 85064 KB Output is correct
118 Correct 559 ms 78692 KB Output is correct
119 Correct 362 ms 85072 KB Output is correct
120 Correct 622 ms 96340 KB Output is correct
121 Correct 389 ms 85312 KB Output is correct
122 Correct 606 ms 84304 KB Output is correct
123 Correct 290 ms 86624 KB Output is correct
124 Correct 448 ms 83536 KB Output is correct