# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
699456 | 2023-02-17T03:14:42 Z | nguyentunglam | Mergers (JOI19_mergers) | C++17 | 945 ms | 170012 KB |
#include<bits/stdc++.h> #define fi first #define se second #define endl "\n" #define ii pair<int, int> using namespace std; const int N = 5e5 + 10; int c[21][N * 2], st[N], lab[N], f[N], g[N]; vector<int> adj[N], lst[N]; int cnt; void dfs(int u) { c[0][++cnt] = u; st[u] = cnt; for(int &v : adj[u]) if (!st[v]) { dfs(v); c[0][++cnt] = u; } } int lca(int u, int v) { int l = st[u], r = st[v]; if (l > r) swap(l, r); int k = log2(r - l + 1); int x = c[k][l], y = c[k][r - (1 << k) + 1]; return st[x] < st[y] ? x : y; } int root(int v) { return lab[v] < 0 ? v : lab[v] = root(lab[v]); } void join(int u, int v) { u = root(u); v = root(v); if (u == v) return; if (lab[u] > lab[v]) swap(u, v); lab[u] += lab[v]; lab[v] = u; } void scan(int u, int par) { for(int &v : adj[u]) if (v != par) { scan(v, u); f[u] += f[v]; } if (f[u] && par) join(u, par); } int main() { #define task "" cin.tie(0) -> sync_with_stdio(0); if (fopen ("task.inp", "r")) { freopen ("task.inp", "r", stdin); freopen ("task.out", "w", stdout); } if (fopen (task".inp", "r")) { freopen (task".inp", "r", stdin); freopen (task".out", "w", stdout); } int n, k; cin >> n >> k; for(int i = 1; i < n; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } dfs(1); for(int j = 1; j <= log2(cnt); j++) for(int i = 1; i <= cnt; i++) { int x = c[j - 1][i], y = c[j - 1][i + (1 << (j - 1))]; c[j][i] = st[x] < st[y] ? x : y; } for(int i = 1; i <= n; i++) { int s; cin >> s; lst[s].push_back(i); lab[i] = -1; } for(int color = 1; color <= k; color++) if (!lst[color].empty()) { int p = lst[color].back(); for(int &j : lst[color]) p = lca(p, j); for(int &j : lst[color]) { f[j]++; f[p]--; } } scan(1, 0); for(int u = 1; u <= n; u++) { for(int &v : adj[u]) if (root(v) != root(u)) { g[root(u)]++, g[root(v)]++; } } int leaf = 0; for(int i = 1; i <= n; i++) if (g[i] == 2) leaf++; cout << (leaf + 1) / 2; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 12 ms | 23764 KB | Output is correct |
2 | Correct | 12 ms | 23892 KB | Output is correct |
3 | Correct | 11 ms | 23900 KB | Output is correct |
4 | Correct | 11 ms | 23772 KB | Output is correct |
5 | Correct | 12 ms | 23764 KB | Output is correct |
6 | Correct | 11 ms | 23892 KB | Output is correct |
7 | Correct | 11 ms | 23772 KB | Output is correct |
8 | Correct | 11 ms | 23892 KB | Output is correct |
9 | Correct | 12 ms | 23832 KB | Output is correct |
10 | Correct | 12 ms | 23764 KB | Output is correct |
11 | Correct | 11 ms | 23868 KB | Output is correct |
12 | Correct | 11 ms | 23892 KB | Output is correct |
13 | Correct | 12 ms | 23764 KB | Output is correct |
14 | Correct | 12 ms | 23892 KB | Output is correct |
15 | Correct | 12 ms | 23844 KB | Output is correct |
16 | Correct | 12 ms | 23792 KB | Output is correct |
17 | Correct | 11 ms | 23764 KB | Output is correct |
18 | Correct | 11 ms | 23860 KB | Output is correct |
19 | Correct | 11 ms | 23848 KB | Output is correct |
20 | Correct | 11 ms | 23760 KB | Output is correct |
21 | Correct | 12 ms | 23900 KB | Output is correct |
22 | Correct | 11 ms | 23892 KB | Output is correct |
23 | Correct | 11 ms | 23836 KB | Output is correct |
24 | Correct | 11 ms | 23792 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 12 ms | 23764 KB | Output is correct |
2 | Correct | 12 ms | 23892 KB | Output is correct |
3 | Correct | 11 ms | 23900 KB | Output is correct |
4 | Correct | 11 ms | 23772 KB | Output is correct |
5 | Correct | 12 ms | 23764 KB | Output is correct |
6 | Correct | 11 ms | 23892 KB | Output is correct |
7 | Correct | 11 ms | 23772 KB | Output is correct |
8 | Correct | 11 ms | 23892 KB | Output is correct |
9 | Correct | 12 ms | 23832 KB | Output is correct |
10 | Correct | 12 ms | 23764 KB | Output is correct |
11 | Correct | 11 ms | 23868 KB | Output is correct |
12 | Correct | 11 ms | 23892 KB | Output is correct |
13 | Correct | 12 ms | 23764 KB | Output is correct |
14 | Correct | 12 ms | 23892 KB | Output is correct |
15 | Correct | 12 ms | 23844 KB | Output is correct |
16 | Correct | 12 ms | 23792 KB | Output is correct |
17 | Correct | 11 ms | 23764 KB | Output is correct |
18 | Correct | 11 ms | 23860 KB | Output is correct |
19 | Correct | 11 ms | 23848 KB | Output is correct |
20 | Correct | 11 ms | 23760 KB | Output is correct |
21 | Correct | 12 ms | 23900 KB | Output is correct |
22 | Correct | 11 ms | 23892 KB | Output is correct |
23 | Correct | 11 ms | 23836 KB | Output is correct |
24 | Correct | 11 ms | 23792 KB | Output is correct |
25 | Correct | 12 ms | 23788 KB | Output is correct |
26 | Correct | 13 ms | 24404 KB | Output is correct |
27 | Correct | 13 ms | 24404 KB | Output is correct |
28 | Correct | 13 ms | 24532 KB | Output is correct |
29 | Correct | 14 ms | 24392 KB | Output is correct |
30 | Correct | 13 ms | 24404 KB | Output is correct |
31 | Correct | 12 ms | 24020 KB | Output is correct |
32 | Correct | 17 ms | 24616 KB | Output is correct |
33 | Correct | 12 ms | 23892 KB | Output is correct |
34 | Correct | 13 ms | 24320 KB | Output is correct |
35 | Correct | 13 ms | 24404 KB | Output is correct |
36 | Correct | 13 ms | 24348 KB | Output is correct |
37 | Correct | 13 ms | 24404 KB | Output is correct |
38 | Correct | 12 ms | 23892 KB | Output is correct |
39 | Correct | 13 ms | 24316 KB | Output is correct |
40 | Correct | 13 ms | 24404 KB | Output is correct |
41 | Correct | 13 ms | 24332 KB | Output is correct |
42 | Correct | 14 ms | 24460 KB | Output is correct |
43 | Correct | 15 ms | 24600 KB | Output is correct |
44 | Correct | 12 ms | 23892 KB | Output is correct |
45 | Correct | 13 ms | 24404 KB | Output is correct |
46 | Correct | 13 ms | 24324 KB | Output is correct |
47 | Correct | 12 ms | 23908 KB | Output is correct |
48 | Correct | 15 ms | 24404 KB | Output is correct |
49 | Correct | 14 ms | 24504 KB | Output is correct |
50 | Correct | 14 ms | 24532 KB | Output is correct |
51 | Correct | 13 ms | 24412 KB | Output is correct |
52 | Correct | 13 ms | 24420 KB | Output is correct |
53 | Correct | 13 ms | 24428 KB | Output is correct |
54 | Correct | 13 ms | 24404 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 12 ms | 23764 KB | Output is correct |
2 | Correct | 12 ms | 23892 KB | Output is correct |
3 | Correct | 11 ms | 23900 KB | Output is correct |
4 | Correct | 11 ms | 23772 KB | Output is correct |
5 | Correct | 12 ms | 23764 KB | Output is correct |
6 | Correct | 11 ms | 23892 KB | Output is correct |
7 | Correct | 11 ms | 23772 KB | Output is correct |
8 | Correct | 11 ms | 23892 KB | Output is correct |
9 | Correct | 12 ms | 23832 KB | Output is correct |
10 | Correct | 12 ms | 23764 KB | Output is correct |
11 | Correct | 11 ms | 23868 KB | Output is correct |
12 | Correct | 11 ms | 23892 KB | Output is correct |
13 | Correct | 12 ms | 23764 KB | Output is correct |
14 | Correct | 12 ms | 23892 KB | Output is correct |
15 | Correct | 12 ms | 23844 KB | Output is correct |
16 | Correct | 12 ms | 23792 KB | Output is correct |
17 | Correct | 11 ms | 23764 KB | Output is correct |
18 | Correct | 11 ms | 23860 KB | Output is correct |
19 | Correct | 11 ms | 23848 KB | Output is correct |
20 | Correct | 11 ms | 23760 KB | Output is correct |
21 | Correct | 12 ms | 23900 KB | Output is correct |
22 | Correct | 11 ms | 23892 KB | Output is correct |
23 | Correct | 11 ms | 23836 KB | Output is correct |
24 | Correct | 11 ms | 23792 KB | Output is correct |
25 | Correct | 11 ms | 23892 KB | Output is correct |
26 | Correct | 63 ms | 43244 KB | Output is correct |
27 | Correct | 89 ms | 43012 KB | Output is correct |
28 | Correct | 13 ms | 24404 KB | Output is correct |
29 | Correct | 12 ms | 23892 KB | Output is correct |
30 | Correct | 11 ms | 23884 KB | Output is correct |
31 | Correct | 82 ms | 43072 KB | Output is correct |
32 | Correct | 13 ms | 24420 KB | Output is correct |
33 | Correct | 91 ms | 48696 KB | Output is correct |
34 | Correct | 76 ms | 42956 KB | Output is correct |
35 | Correct | 13 ms | 24404 KB | Output is correct |
36 | Correct | 80 ms | 43664 KB | Output is correct |
37 | Correct | 13 ms | 24392 KB | Output is correct |
38 | Correct | 14 ms | 24404 KB | Output is correct |
39 | Correct | 68 ms | 43340 KB | Output is correct |
40 | Correct | 14 ms | 24532 KB | Output is correct |
41 | Correct | 81 ms | 42944 KB | Output is correct |
42 | Correct | 85 ms | 44832 KB | Output is correct |
43 | Correct | 12 ms | 23808 KB | Output is correct |
44 | Correct | 84 ms | 48836 KB | Output is correct |
45 | Correct | 82 ms | 45888 KB | Output is correct |
46 | Correct | 14 ms | 24404 KB | Output is correct |
47 | Correct | 17 ms | 24400 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 62 ms | 43300 KB | Output is correct |
2 | Correct | 76 ms | 46288 KB | Output is correct |
3 | Correct | 14 ms | 24380 KB | Output is correct |
4 | Correct | 14 ms | 24404 KB | Output is correct |
5 | Correct | 11 ms | 23904 KB | Output is correct |
6 | Correct | 12 ms | 23892 KB | Output is correct |
7 | Correct | 13 ms | 24404 KB | Output is correct |
8 | Correct | 101 ms | 44404 KB | Output is correct |
9 | Correct | 13 ms | 24380 KB | Output is correct |
10 | Correct | 78 ms | 43388 KB | Output is correct |
11 | Correct | 12 ms | 23820 KB | Output is correct |
12 | Correct | 86 ms | 43416 KB | Output is correct |
13 | Correct | 91 ms | 44364 KB | Output is correct |
14 | Correct | 87 ms | 45988 KB | Output is correct |
15 | Correct | 61 ms | 43248 KB | Output is correct |
16 | Correct | 15 ms | 24372 KB | Output is correct |
17 | Correct | 13 ms | 23856 KB | Output is correct |
18 | Correct | 73 ms | 45908 KB | Output is correct |
19 | Correct | 105 ms | 49100 KB | Output is correct |
20 | Correct | 13 ms | 24408 KB | Output is correct |
21 | Correct | 11 ms | 23892 KB | Output is correct |
22 | Correct | 73 ms | 44736 KB | Output is correct |
23 | Correct | 14 ms | 24428 KB | Output is correct |
24 | Correct | 85 ms | 43608 KB | Output is correct |
25 | Correct | 96 ms | 47964 KB | Output is correct |
26 | Correct | 13 ms | 24472 KB | Output is correct |
27 | Correct | 16 ms | 24528 KB | Output is correct |
28 | Correct | 13 ms | 24404 KB | Output is correct |
29 | Correct | 12 ms | 24404 KB | Output is correct |
30 | Correct | 13 ms | 24356 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 12 ms | 23764 KB | Output is correct |
2 | Correct | 12 ms | 23892 KB | Output is correct |
3 | Correct | 11 ms | 23900 KB | Output is correct |
4 | Correct | 11 ms | 23772 KB | Output is correct |
5 | Correct | 12 ms | 23764 KB | Output is correct |
6 | Correct | 11 ms | 23892 KB | Output is correct |
7 | Correct | 11 ms | 23772 KB | Output is correct |
8 | Correct | 11 ms | 23892 KB | Output is correct |
9 | Correct | 12 ms | 23832 KB | Output is correct |
10 | Correct | 12 ms | 23764 KB | Output is correct |
11 | Correct | 11 ms | 23868 KB | Output is correct |
12 | Correct | 11 ms | 23892 KB | Output is correct |
13 | Correct | 12 ms | 23764 KB | Output is correct |
14 | Correct | 12 ms | 23892 KB | Output is correct |
15 | Correct | 12 ms | 23844 KB | Output is correct |
16 | Correct | 12 ms | 23792 KB | Output is correct |
17 | Correct | 11 ms | 23764 KB | Output is correct |
18 | Correct | 11 ms | 23860 KB | Output is correct |
19 | Correct | 11 ms | 23848 KB | Output is correct |
20 | Correct | 11 ms | 23760 KB | Output is correct |
21 | Correct | 12 ms | 23900 KB | Output is correct |
22 | Correct | 11 ms | 23892 KB | Output is correct |
23 | Correct | 11 ms | 23836 KB | Output is correct |
24 | Correct | 11 ms | 23792 KB | Output is correct |
25 | Correct | 12 ms | 23788 KB | Output is correct |
26 | Correct | 13 ms | 24404 KB | Output is correct |
27 | Correct | 13 ms | 24404 KB | Output is correct |
28 | Correct | 13 ms | 24532 KB | Output is correct |
29 | Correct | 14 ms | 24392 KB | Output is correct |
30 | Correct | 13 ms | 24404 KB | Output is correct |
31 | Correct | 12 ms | 24020 KB | Output is correct |
32 | Correct | 17 ms | 24616 KB | Output is correct |
33 | Correct | 12 ms | 23892 KB | Output is correct |
34 | Correct | 13 ms | 24320 KB | Output is correct |
35 | Correct | 13 ms | 24404 KB | Output is correct |
36 | Correct | 13 ms | 24348 KB | Output is correct |
37 | Correct | 13 ms | 24404 KB | Output is correct |
38 | Correct | 12 ms | 23892 KB | Output is correct |
39 | Correct | 13 ms | 24316 KB | Output is correct |
40 | Correct | 13 ms | 24404 KB | Output is correct |
41 | Correct | 13 ms | 24332 KB | Output is correct |
42 | Correct | 14 ms | 24460 KB | Output is correct |
43 | Correct | 15 ms | 24600 KB | Output is correct |
44 | Correct | 12 ms | 23892 KB | Output is correct |
45 | Correct | 13 ms | 24404 KB | Output is correct |
46 | Correct | 13 ms | 24324 KB | Output is correct |
47 | Correct | 12 ms | 23908 KB | Output is correct |
48 | Correct | 15 ms | 24404 KB | Output is correct |
49 | Correct | 14 ms | 24504 KB | Output is correct |
50 | Correct | 14 ms | 24532 KB | Output is correct |
51 | Correct | 13 ms | 24412 KB | Output is correct |
52 | Correct | 13 ms | 24420 KB | Output is correct |
53 | Correct | 13 ms | 24428 KB | Output is correct |
54 | Correct | 13 ms | 24404 KB | Output is correct |
55 | Correct | 11 ms | 23892 KB | Output is correct |
56 | Correct | 63 ms | 43244 KB | Output is correct |
57 | Correct | 89 ms | 43012 KB | Output is correct |
58 | Correct | 13 ms | 24404 KB | Output is correct |
59 | Correct | 12 ms | 23892 KB | Output is correct |
60 | Correct | 11 ms | 23884 KB | Output is correct |
61 | Correct | 82 ms | 43072 KB | Output is correct |
62 | Correct | 13 ms | 24420 KB | Output is correct |
63 | Correct | 91 ms | 48696 KB | Output is correct |
64 | Correct | 76 ms | 42956 KB | Output is correct |
65 | Correct | 13 ms | 24404 KB | Output is correct |
66 | Correct | 80 ms | 43664 KB | Output is correct |
67 | Correct | 13 ms | 24392 KB | Output is correct |
68 | Correct | 14 ms | 24404 KB | Output is correct |
69 | Correct | 68 ms | 43340 KB | Output is correct |
70 | Correct | 14 ms | 24532 KB | Output is correct |
71 | Correct | 81 ms | 42944 KB | Output is correct |
72 | Correct | 85 ms | 44832 KB | Output is correct |
73 | Correct | 12 ms | 23808 KB | Output is correct |
74 | Correct | 84 ms | 48836 KB | Output is correct |
75 | Correct | 82 ms | 45888 KB | Output is correct |
76 | Correct | 14 ms | 24404 KB | Output is correct |
77 | Correct | 17 ms | 24400 KB | Output is correct |
78 | Correct | 62 ms | 43300 KB | Output is correct |
79 | Correct | 76 ms | 46288 KB | Output is correct |
80 | Correct | 14 ms | 24380 KB | Output is correct |
81 | Correct | 14 ms | 24404 KB | Output is correct |
82 | Correct | 11 ms | 23904 KB | Output is correct |
83 | Correct | 12 ms | 23892 KB | Output is correct |
84 | Correct | 13 ms | 24404 KB | Output is correct |
85 | Correct | 101 ms | 44404 KB | Output is correct |
86 | Correct | 13 ms | 24380 KB | Output is correct |
87 | Correct | 78 ms | 43388 KB | Output is correct |
88 | Correct | 12 ms | 23820 KB | Output is correct |
89 | Correct | 86 ms | 43416 KB | Output is correct |
90 | Correct | 91 ms | 44364 KB | Output is correct |
91 | Correct | 87 ms | 45988 KB | Output is correct |
92 | Correct | 61 ms | 43248 KB | Output is correct |
93 | Correct | 15 ms | 24372 KB | Output is correct |
94 | Correct | 13 ms | 23856 KB | Output is correct |
95 | Correct | 73 ms | 45908 KB | Output is correct |
96 | Correct | 105 ms | 49100 KB | Output is correct |
97 | Correct | 13 ms | 24408 KB | Output is correct |
98 | Correct | 11 ms | 23892 KB | Output is correct |
99 | Correct | 73 ms | 44736 KB | Output is correct |
100 | Correct | 14 ms | 24428 KB | Output is correct |
101 | Correct | 85 ms | 43608 KB | Output is correct |
102 | Correct | 96 ms | 47964 KB | Output is correct |
103 | Correct | 13 ms | 24472 KB | Output is correct |
104 | Correct | 16 ms | 24528 KB | Output is correct |
105 | Correct | 13 ms | 24404 KB | Output is correct |
106 | Correct | 12 ms | 24404 KB | Output is correct |
107 | Correct | 13 ms | 24356 KB | Output is correct |
108 | Correct | 525 ms | 136952 KB | Output is correct |
109 | Correct | 684 ms | 148152 KB | Output is correct |
110 | Correct | 694 ms | 157108 KB | Output is correct |
111 | Correct | 874 ms | 170012 KB | Output is correct |
112 | Correct | 861 ms | 162120 KB | Output is correct |
113 | Correct | 595 ms | 151728 KB | Output is correct |
114 | Correct | 595 ms | 134044 KB | Output is correct |
115 | Correct | 624 ms | 133816 KB | Output is correct |
116 | Correct | 681 ms | 140364 KB | Output is correct |
117 | Correct | 751 ms | 151756 KB | Output is correct |
118 | Correct | 588 ms | 135136 KB | Output is correct |
119 | Correct | 731 ms | 151704 KB | Output is correct |
120 | Correct | 852 ms | 162728 KB | Output is correct |
121 | Correct | 810 ms | 151712 KB | Output is correct |
122 | Correct | 803 ms | 145672 KB | Output is correct |
123 | Correct | 582 ms | 153220 KB | Output is correct |
124 | Correct | 945 ms | 149024 KB | Output is correct |