Submission #396088

# Submission time Handle Problem Language Result Execution time Memory
396088 2021-04-29T12:34:10 Z palilo Mergers (JOI19_mergers) C++17
100 / 100
833 ms 91772 KB
#include <bits/stdc++.h>
using namespace std;

namespace std {
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0200r0.html
template <class Fun>
class y_combinator_result {
	Fun fun_;

public:
	template <class T>
	explicit y_combinator_result(T&& fun) : fun_(std::forward<T>(fun)) {}

	template <class... Args>
	decltype(auto) operator()(Args&&... args) {
		return fun_(std::ref(*this), std::forward<Args>(args)...);
	}
};

template <class Fun>
decltype(auto) y_combinator(Fun&& fun) {
	return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
}; // namespace std

int main() {
	cin.tie(nullptr)->sync_with_stdio(false);
#ifdef home
	freopen("in", "r", stdin);
	freopen("out", "w", stdout);
#endif
	int n, k;
	cin >> n >> k;

	vector<vector<int>> adj(n);
	for (int i = n - 1, u, v; i--;) {
		cin >> u >> v, --u, --v;
		adj[u].emplace_back(v);
		adj[v].emplace_back(u);
	}

	vector<vector<int>> colour(k);
	for (int i = 0, c; i < n; ++i) {
		cin >> c, --c;
		colour[c].emplace_back(i);
	}

	vector<int> par(n), dep(n);
	par[0] = -1;
	y_combinator([&](auto self, int u) -> void {
		for (const auto& v : adj[u]) {
			if (v == par[u]) continue;
			par[v] = u, dep[v] = dep[u] + 1;
			self(v);
		}
	})(0);

	vector dsu(n, -1);
	auto get_par = [&](int u) {
		while (dsu[u] != -1) {
			if (dsu[dsu[u]] != -1) dsu[u] = dsu[dsu[u]];
			u = dsu[u];
		}
		return u;
	};

	for (const auto& nodes : colour) {
		int u = get_par(nodes[0]), p;
		for (auto v : nodes) {
			for (v = get_par(v); u != v; u = p) {
				if (dep[u] < dep[v]) swap(u, v);
				p = get_par(par[u]);
				dsu[u] = p;
			}
		}
	}

	vector<int> deg(n);
	for (int u = 0; u < n; ++u) {
		int pu = get_par(u);
		for (const auto& v : adj[u]) {
			if (v == par[u]) continue;
			if (int pv = get_par(v); pu != pv)
				++deg[pu], ++deg[pv];
		}
	}
	cout << ((count(deg.begin(), deg.end(), 1) + 1) >> 1);
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 316 KB Output is correct
26 Correct 3 ms 664 KB Output is correct
27 Correct 2 ms 588 KB Output is correct
28 Correct 4 ms 716 KB Output is correct
29 Correct 3 ms 716 KB Output is correct
30 Correct 2 ms 588 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 3 ms 844 KB Output is correct
33 Correct 1 ms 320 KB Output is correct
34 Correct 2 ms 588 KB Output is correct
35 Correct 3 ms 716 KB Output is correct
36 Correct 2 ms 588 KB Output is correct
37 Correct 3 ms 588 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 3 ms 716 KB Output is correct
40 Correct 2 ms 588 KB Output is correct
41 Correct 2 ms 588 KB Output is correct
42 Correct 2 ms 588 KB Output is correct
43 Correct 3 ms 716 KB Output is correct
44 Correct 1 ms 332 KB Output is correct
45 Correct 3 ms 716 KB Output is correct
46 Correct 3 ms 588 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 3 ms 588 KB Output is correct
49 Correct 2 ms 716 KB Output is correct
50 Correct 3 ms 716 KB Output is correct
51 Correct 3 ms 588 KB Output is correct
52 Correct 2 ms 588 KB Output is correct
53 Correct 3 ms 588 KB Output is correct
54 Correct 2 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 50 ms 9708 KB Output is correct
27 Correct 86 ms 9360 KB Output is correct
28 Correct 2 ms 588 KB Output is correct
29 Correct 1 ms 316 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 71 ms 9376 KB Output is correct
32 Correct 2 ms 588 KB Output is correct
33 Correct 88 ms 15044 KB Output is correct
34 Correct 70 ms 9412 KB Output is correct
35 Correct 2 ms 588 KB Output is correct
36 Correct 92 ms 9896 KB Output is correct
37 Correct 2 ms 588 KB Output is correct
38 Correct 2 ms 588 KB Output is correct
39 Correct 60 ms 9664 KB Output is correct
40 Correct 2 ms 716 KB Output is correct
41 Correct 69 ms 9332 KB Output is correct
42 Correct 81 ms 11412 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 104 ms 15276 KB Output is correct
45 Correct 86 ms 12356 KB Output is correct
46 Correct 2 ms 588 KB Output is correct
47 Correct 2 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 9672 KB Output is correct
2 Correct 89 ms 14908 KB Output is correct
3 Correct 4 ms 588 KB Output is correct
4 Correct 2 ms 588 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 2 ms 584 KB Output is correct
8 Correct 88 ms 12000 KB Output is correct
9 Correct 3 ms 588 KB Output is correct
10 Correct 103 ms 9496 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 77 ms 9528 KB Output is correct
13 Correct 84 ms 11788 KB Output is correct
14 Correct 88 ms 14768 KB Output is correct
15 Correct 48 ms 9660 KB Output is correct
16 Correct 2 ms 588 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 93 ms 14404 KB Output is correct
19 Correct 97 ms 17696 KB Output is correct
20 Correct 3 ms 588 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 63 ms 12092 KB Output is correct
23 Correct 3 ms 588 KB Output is correct
24 Correct 87 ms 10056 KB Output is correct
25 Correct 92 ms 16708 KB Output is correct
26 Correct 2 ms 716 KB Output is correct
27 Correct 2 ms 716 KB Output is correct
28 Correct 2 ms 588 KB Output is correct
29 Correct 2 ms 588 KB Output is correct
30 Correct 2 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 316 KB Output is correct
26 Correct 3 ms 664 KB Output is correct
27 Correct 2 ms 588 KB Output is correct
28 Correct 4 ms 716 KB Output is correct
29 Correct 3 ms 716 KB Output is correct
30 Correct 2 ms 588 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 3 ms 844 KB Output is correct
33 Correct 1 ms 320 KB Output is correct
34 Correct 2 ms 588 KB Output is correct
35 Correct 3 ms 716 KB Output is correct
36 Correct 2 ms 588 KB Output is correct
37 Correct 3 ms 588 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 3 ms 716 KB Output is correct
40 Correct 2 ms 588 KB Output is correct
41 Correct 2 ms 588 KB Output is correct
42 Correct 2 ms 588 KB Output is correct
43 Correct 3 ms 716 KB Output is correct
44 Correct 1 ms 332 KB Output is correct
45 Correct 3 ms 716 KB Output is correct
46 Correct 3 ms 588 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 3 ms 588 KB Output is correct
49 Correct 2 ms 716 KB Output is correct
50 Correct 3 ms 716 KB Output is correct
51 Correct 3 ms 588 KB Output is correct
52 Correct 2 ms 588 KB Output is correct
53 Correct 3 ms 588 KB Output is correct
54 Correct 2 ms 588 KB Output is correct
55 Correct 1 ms 204 KB Output is correct
56 Correct 50 ms 9708 KB Output is correct
57 Correct 86 ms 9360 KB Output is correct
58 Correct 2 ms 588 KB Output is correct
59 Correct 1 ms 316 KB Output is correct
60 Correct 1 ms 204 KB Output is correct
61 Correct 71 ms 9376 KB Output is correct
62 Correct 2 ms 588 KB Output is correct
63 Correct 88 ms 15044 KB Output is correct
64 Correct 70 ms 9412 KB Output is correct
65 Correct 2 ms 588 KB Output is correct
66 Correct 92 ms 9896 KB Output is correct
67 Correct 2 ms 588 KB Output is correct
68 Correct 2 ms 588 KB Output is correct
69 Correct 60 ms 9664 KB Output is correct
70 Correct 2 ms 716 KB Output is correct
71 Correct 69 ms 9332 KB Output is correct
72 Correct 81 ms 11412 KB Output is correct
73 Correct 1 ms 204 KB Output is correct
74 Correct 104 ms 15276 KB Output is correct
75 Correct 86 ms 12356 KB Output is correct
76 Correct 2 ms 588 KB Output is correct
77 Correct 2 ms 588 KB Output is correct
78 Correct 54 ms 9672 KB Output is correct
79 Correct 89 ms 14908 KB Output is correct
80 Correct 4 ms 588 KB Output is correct
81 Correct 2 ms 588 KB Output is correct
82 Correct 1 ms 204 KB Output is correct
83 Correct 1 ms 312 KB Output is correct
84 Correct 2 ms 584 KB Output is correct
85 Correct 88 ms 12000 KB Output is correct
86 Correct 3 ms 588 KB Output is correct
87 Correct 103 ms 9496 KB Output is correct
88 Correct 1 ms 204 KB Output is correct
89 Correct 77 ms 9528 KB Output is correct
90 Correct 84 ms 11788 KB Output is correct
91 Correct 88 ms 14768 KB Output is correct
92 Correct 48 ms 9660 KB Output is correct
93 Correct 2 ms 588 KB Output is correct
94 Correct 1 ms 204 KB Output is correct
95 Correct 93 ms 14404 KB Output is correct
96 Correct 97 ms 17696 KB Output is correct
97 Correct 3 ms 588 KB Output is correct
98 Correct 1 ms 204 KB Output is correct
99 Correct 63 ms 12092 KB Output is correct
100 Correct 3 ms 588 KB Output is correct
101 Correct 87 ms 10056 KB Output is correct
102 Correct 92 ms 16708 KB Output is correct
103 Correct 2 ms 716 KB Output is correct
104 Correct 2 ms 716 KB Output is correct
105 Correct 2 ms 588 KB Output is correct
106 Correct 2 ms 588 KB Output is correct
107 Correct 2 ms 588 KB Output is correct
108 Correct 545 ms 58524 KB Output is correct
109 Correct 667 ms 60056 KB Output is correct
110 Correct 669 ms 69048 KB Output is correct
111 Correct 833 ms 91772 KB Output is correct
112 Correct 807 ms 84008 KB Output is correct
113 Correct 521 ms 72188 KB Output is correct
114 Correct 582 ms 45820 KB Output is correct
115 Correct 569 ms 45808 KB Output is correct
116 Correct 685 ms 52804 KB Output is correct
117 Correct 732 ms 73488 KB Output is correct
118 Correct 616 ms 46908 KB Output is correct
119 Correct 807 ms 73372 KB Output is correct
120 Correct 749 ms 84436 KB Output is correct
121 Correct 757 ms 73404 KB Output is correct
122 Correct 770 ms 62616 KB Output is correct
123 Correct 496 ms 74892 KB Output is correct
124 Correct 807 ms 71676 KB Output is correct