# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
647514 | 2022-10-03T02:37:18 Z | ghostwriter | Mergers (JOI19_mergers) | C++14 | 1511 ms | 132032 KB |
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include <debug.h> #else #define debug(...) #endif #define ft front #define bk back #define st first #define nd second #define ins insert #define ers erase #define pb push_back #define pf push_front #define _pb pop_back #define _pf pop_front #define lb lower_bound #define ub upper_bound #define mtp make_tuple #define bg begin #define ed end #define all(x) (x).bg(), (x).ed() #define sz(x) (int)(x).size() typedef long long ll; typedef unsigned long long ull; typedef double db; typedef long double ldb; typedef pair<int, int> pi; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll; typedef string str; template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); } template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; } #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i)) #define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i)) #define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i)) #define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i)) #define EACH(i, x) for (auto &(i) : (x)) #define WHILE while #define file "TEST" mt19937 rd(chrono::steady_clock::now().time_since_epoch().count()); ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); } /* ---------------------------------------------------------------- END OF TEMPLATE ---------------------------------------------------------------- Tran The Bao - ghostwriter Training for VOI23 gold medal ---------------------------------------------------------------- DIT ME CHUYEN BAO LOC ---------------------------------------------------------------- */ const int N = 5e5 + 1; int n, k, h[N], s[N], s1[N], s2[N], p[N][19], root = 0, ct = 0; vi adj[N], d[N]; void build() { FOR(j, 1, 18) FOR(i, 1, n) p[i][j] = p[p[i][j - 1]][j - 1]; } int lca(int a, int b) { if (h[a] > h[b]) swap(a, b); int diff = h[b] - h[a]; FOR(i, 0, 18) if (diff & (1 << i)) b = p[b][i]; if (a == b) return a; FOS(i, 18, 0) if (p[a][i] != p[b][i]) { a = p[a][i]; b = p[b][i]; } return p[a][0]; } void dfs(int u, int p) { ::p[u][0] = p; s[u] = 1; EACH(v, adj[u]) { if (v == p) continue; h[v] = h[u] + 1; dfs(v, u); s[u] += s[v]; } } void dfs1(int u, int p) { s2[u] = 0; EACH(v, adj[u]) { if (v == p) continue; dfs1(v, u); s1[u] += s1[v]; s2[u] += s2[v]; } if (!s2[u]) { if (p != 0 && s1[u] == s[u]) ++s2[u]; } } int fct(int u, int p, int maxn) { EACH(v, adj[u]) { if (v == p) continue; if (s2[v] > maxn / 2) return fct(v, u, maxn); } return u; } void dfs2(int u, int p) { s2[u] = 0; EACH(v, adj[u]) { if (v == p) continue; dfs2(v, u); s1[u] += s1[v]; s2[u] += s2[v]; } if (!s2[u]) { if (p != 0 && s1[u] == s[u]) ++s2[u]; } } signed main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); // freopen(file".inp", "r", stdin); // freopen(file".out", "w", stdout); cin >> n >> k; FOR(i, 1, n - 1) { int u, v; cin >> u >> v; adj[u].pb(v); adj[v].pb(u); } FOR(i, 1, n) { int s; cin >> s; d[s].pb(i); } if (k == 1) { cout << 0; return 0; } if (n == 2) { cout << (k == 1? 0 : 1); return 0; } FOR(i, 1, n) if (sz(adj[i]) > 1) root = i; dfs(root, 0); build(); FOR(i, 1, k) { int LCA = d[i].ft(); EACH(j, d[i]) LCA = lca(LCA, j); s1[LCA] += sz(d[i]); } dfs1(root, 0); ct = fct(root, 0, s2[root]); dfs(ct, 0); build(); memset(s1, 0, sizeof s1); FOR(i, 1, k) { int LCA = d[i].ft(); EACH(j, d[i]) LCA = lca(LCA, j); s1[LCA] += sz(d[i]); } dfs2(ct, 0); cout << (s2[ct] + 1) / 2; return 0; } /* 7 6 1 5 2 5 3 6 4 6 5 7 6 7 1 2 2 2 5 6 4 ---------------------------------------------------------------- From Benq: stuff you should look for * int overflow, array bounds * special cases (n=1?) * do smth instead of nothing and stay organized * WRITE STUFF DOWN * DON'T GET STUCK ON ONE APPROACH ---------------------------------------------------------------- */
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 16 ms | 25684 KB | Output is correct |
2 | Correct | 15 ms | 25684 KB | Output is correct |
3 | Correct | 15 ms | 25752 KB | Output is correct |
4 | Correct | 13 ms | 25780 KB | Output is correct |
5 | Correct | 13 ms | 25728 KB | Output is correct |
6 | Correct | 13 ms | 23812 KB | Output is correct |
7 | Correct | 14 ms | 23764 KB | Output is correct |
8 | Correct | 16 ms | 25728 KB | Output is correct |
9 | Correct | 14 ms | 25660 KB | Output is correct |
10 | Correct | 14 ms | 25736 KB | Output is correct |
11 | Correct | 14 ms | 25812 KB | Output is correct |
12 | Correct | 14 ms | 25816 KB | Output is correct |
13 | Correct | 13 ms | 25732 KB | Output is correct |
14 | Correct | 14 ms | 25704 KB | Output is correct |
15 | Correct | 15 ms | 25740 KB | Output is correct |
16 | Correct | 15 ms | 25684 KB | Output is correct |
17 | Correct | 16 ms | 25756 KB | Output is correct |
18 | Correct | 14 ms | 25736 KB | Output is correct |
19 | Correct | 14 ms | 25684 KB | Output is correct |
20 | Correct | 15 ms | 25684 KB | Output is correct |
21 | Correct | 15 ms | 25684 KB | Output is correct |
22 | Correct | 14 ms | 25736 KB | Output is correct |
23 | Correct | 14 ms | 25684 KB | Output is correct |
24 | Correct | 14 ms | 25700 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 16 ms | 25684 KB | Output is correct |
2 | Correct | 15 ms | 25684 KB | Output is correct |
3 | Correct | 15 ms | 25752 KB | Output is correct |
4 | Correct | 13 ms | 25780 KB | Output is correct |
5 | Correct | 13 ms | 25728 KB | Output is correct |
6 | Correct | 13 ms | 23812 KB | Output is correct |
7 | Correct | 14 ms | 23764 KB | Output is correct |
8 | Correct | 16 ms | 25728 KB | Output is correct |
9 | Correct | 14 ms | 25660 KB | Output is correct |
10 | Correct | 14 ms | 25736 KB | Output is correct |
11 | Correct | 14 ms | 25812 KB | Output is correct |
12 | Correct | 14 ms | 25816 KB | Output is correct |
13 | Correct | 13 ms | 25732 KB | Output is correct |
14 | Correct | 14 ms | 25704 KB | Output is correct |
15 | Correct | 15 ms | 25740 KB | Output is correct |
16 | Correct | 15 ms | 25684 KB | Output is correct |
17 | Correct | 16 ms | 25756 KB | Output is correct |
18 | Correct | 14 ms | 25736 KB | Output is correct |
19 | Correct | 14 ms | 25684 KB | Output is correct |
20 | Correct | 15 ms | 25684 KB | Output is correct |
21 | Correct | 15 ms | 25684 KB | Output is correct |
22 | Correct | 14 ms | 25736 KB | Output is correct |
23 | Correct | 14 ms | 25684 KB | Output is correct |
24 | Correct | 14 ms | 25700 KB | Output is correct |
25 | Correct | 17 ms | 25732 KB | Output is correct |
26 | Correct | 17 ms | 26196 KB | Output is correct |
27 | Correct | 17 ms | 26212 KB | Output is correct |
28 | Correct | 17 ms | 26324 KB | Output is correct |
29 | Correct | 21 ms | 26240 KB | Output is correct |
30 | Correct | 16 ms | 26196 KB | Output is correct |
31 | Correct | 16 ms | 25684 KB | Output is correct |
32 | Correct | 16 ms | 26320 KB | Output is correct |
33 | Correct | 13 ms | 25752 KB | Output is correct |
34 | Correct | 15 ms | 26156 KB | Output is correct |
35 | Correct | 16 ms | 26168 KB | Output is correct |
36 | Correct | 17 ms | 26324 KB | Output is correct |
37 | Correct | 16 ms | 26228 KB | Output is correct |
38 | Correct | 15 ms | 25736 KB | Output is correct |
39 | Correct | 17 ms | 26196 KB | Output is correct |
40 | Correct | 17 ms | 23956 KB | Output is correct |
41 | Correct | 15 ms | 26216 KB | Output is correct |
42 | Correct | 16 ms | 26196 KB | Output is correct |
43 | Correct | 14 ms | 24032 KB | Output is correct |
44 | Correct | 13 ms | 25736 KB | Output is correct |
45 | Correct | 16 ms | 26256 KB | Output is correct |
46 | Correct | 18 ms | 26168 KB | Output is correct |
47 | Correct | 17 ms | 25736 KB | Output is correct |
48 | Correct | 26 ms | 26192 KB | Output is correct |
49 | Correct | 23 ms | 26172 KB | Output is correct |
50 | Correct | 25 ms | 26324 KB | Output is correct |
51 | Correct | 22 ms | 26108 KB | Output is correct |
52 | Correct | 20 ms | 26124 KB | Output is correct |
53 | Correct | 21 ms | 26196 KB | Output is correct |
54 | Correct | 20 ms | 26176 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 16 ms | 25684 KB | Output is correct |
2 | Correct | 15 ms | 25684 KB | Output is correct |
3 | Correct | 15 ms | 25752 KB | Output is correct |
4 | Correct | 13 ms | 25780 KB | Output is correct |
5 | Correct | 13 ms | 25728 KB | Output is correct |
6 | Correct | 13 ms | 23812 KB | Output is correct |
7 | Correct | 14 ms | 23764 KB | Output is correct |
8 | Correct | 16 ms | 25728 KB | Output is correct |
9 | Correct | 14 ms | 25660 KB | Output is correct |
10 | Correct | 14 ms | 25736 KB | Output is correct |
11 | Correct | 14 ms | 25812 KB | Output is correct |
12 | Correct | 14 ms | 25816 KB | Output is correct |
13 | Correct | 13 ms | 25732 KB | Output is correct |
14 | Correct | 14 ms | 25704 KB | Output is correct |
15 | Correct | 15 ms | 25740 KB | Output is correct |
16 | Correct | 15 ms | 25684 KB | Output is correct |
17 | Correct | 16 ms | 25756 KB | Output is correct |
18 | Correct | 14 ms | 25736 KB | Output is correct |
19 | Correct | 14 ms | 25684 KB | Output is correct |
20 | Correct | 15 ms | 25684 KB | Output is correct |
21 | Correct | 15 ms | 25684 KB | Output is correct |
22 | Correct | 14 ms | 25736 KB | Output is correct |
23 | Correct | 14 ms | 25684 KB | Output is correct |
24 | Correct | 14 ms | 25700 KB | Output is correct |
25 | Correct | 20 ms | 25860 KB | Output is correct |
26 | Correct | 134 ms | 39920 KB | Output is correct |
27 | Correct | 166 ms | 39544 KB | Output is correct |
28 | Correct | 22 ms | 26196 KB | Output is correct |
29 | Correct | 19 ms | 25684 KB | Output is correct |
30 | Correct | 17 ms | 25684 KB | Output is correct |
31 | Correct | 205 ms | 39524 KB | Output is correct |
32 | Correct | 27 ms | 26092 KB | Output is correct |
33 | Correct | 263 ms | 44184 KB | Output is correct |
34 | Correct | 138 ms | 39552 KB | Output is correct |
35 | Correct | 15 ms | 26196 KB | Output is correct |
36 | Correct | 141 ms | 40204 KB | Output is correct |
37 | Correct | 15 ms | 23912 KB | Output is correct |
38 | Correct | 15 ms | 26228 KB | Output is correct |
39 | Correct | 122 ms | 39796 KB | Output is correct |
40 | Correct | 16 ms | 23892 KB | Output is correct |
41 | Correct | 58 ms | 28964 KB | Output is correct |
42 | Correct | 188 ms | 42556 KB | Output is correct |
43 | Correct | 17 ms | 25684 KB | Output is correct |
44 | Correct | 64 ms | 28872 KB | Output is correct |
45 | Correct | 215 ms | 43424 KB | Output is correct |
46 | Correct | 15 ms | 26196 KB | Output is correct |
47 | Correct | 16 ms | 26128 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 95 ms | 38420 KB | Output is correct |
2 | Correct | 123 ms | 42776 KB | Output is correct |
3 | Correct | 16 ms | 26196 KB | Output is correct |
4 | Correct | 19 ms | 26196 KB | Output is correct |
5 | Correct | 15 ms | 25736 KB | Output is correct |
6 | Correct | 16 ms | 25736 KB | Output is correct |
7 | Correct | 15 ms | 26184 KB | Output is correct |
8 | Correct | 171 ms | 40912 KB | Output is correct |
9 | Correct | 16 ms | 26196 KB | Output is correct |
10 | Correct | 159 ms | 39716 KB | Output is correct |
11 | Correct | 15 ms | 25720 KB | Output is correct |
12 | Correct | 184 ms | 39736 KB | Output is correct |
13 | Correct | 192 ms | 40940 KB | Output is correct |
14 | Correct | 145 ms | 42444 KB | Output is correct |
15 | Correct | 96 ms | 39804 KB | Output is correct |
16 | Correct | 16 ms | 26232 KB | Output is correct |
17 | Correct | 15 ms | 25812 KB | Output is correct |
18 | Correct | 132 ms | 42460 KB | Output is correct |
19 | Correct | 203 ms | 46568 KB | Output is correct |
20 | Correct | 16 ms | 26208 KB | Output is correct |
21 | Correct | 16 ms | 25812 KB | Output is correct |
22 | Correct | 126 ms | 41024 KB | Output is correct |
23 | Correct | 17 ms | 26252 KB | Output is correct |
24 | Correct | 199 ms | 40068 KB | Output is correct |
25 | Correct | 154 ms | 45532 KB | Output is correct |
26 | Correct | 15 ms | 26300 KB | Output is correct |
27 | Correct | 15 ms | 26324 KB | Output is correct |
28 | Correct | 17 ms | 26280 KB | Output is correct |
29 | Correct | 16 ms | 26196 KB | Output is correct |
30 | Correct | 17 ms | 26124 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 16 ms | 25684 KB | Output is correct |
2 | Correct | 15 ms | 25684 KB | Output is correct |
3 | Correct | 15 ms | 25752 KB | Output is correct |
4 | Correct | 13 ms | 25780 KB | Output is correct |
5 | Correct | 13 ms | 25728 KB | Output is correct |
6 | Correct | 13 ms | 23812 KB | Output is correct |
7 | Correct | 14 ms | 23764 KB | Output is correct |
8 | Correct | 16 ms | 25728 KB | Output is correct |
9 | Correct | 14 ms | 25660 KB | Output is correct |
10 | Correct | 14 ms | 25736 KB | Output is correct |
11 | Correct | 14 ms | 25812 KB | Output is correct |
12 | Correct | 14 ms | 25816 KB | Output is correct |
13 | Correct | 13 ms | 25732 KB | Output is correct |
14 | Correct | 14 ms | 25704 KB | Output is correct |
15 | Correct | 15 ms | 25740 KB | Output is correct |
16 | Correct | 15 ms | 25684 KB | Output is correct |
17 | Correct | 16 ms | 25756 KB | Output is correct |
18 | Correct | 14 ms | 25736 KB | Output is correct |
19 | Correct | 14 ms | 25684 KB | Output is correct |
20 | Correct | 15 ms | 25684 KB | Output is correct |
21 | Correct | 15 ms | 25684 KB | Output is correct |
22 | Correct | 14 ms | 25736 KB | Output is correct |
23 | Correct | 14 ms | 25684 KB | Output is correct |
24 | Correct | 14 ms | 25700 KB | Output is correct |
25 | Correct | 17 ms | 25732 KB | Output is correct |
26 | Correct | 17 ms | 26196 KB | Output is correct |
27 | Correct | 17 ms | 26212 KB | Output is correct |
28 | Correct | 17 ms | 26324 KB | Output is correct |
29 | Correct | 21 ms | 26240 KB | Output is correct |
30 | Correct | 16 ms | 26196 KB | Output is correct |
31 | Correct | 16 ms | 25684 KB | Output is correct |
32 | Correct | 16 ms | 26320 KB | Output is correct |
33 | Correct | 13 ms | 25752 KB | Output is correct |
34 | Correct | 15 ms | 26156 KB | Output is correct |
35 | Correct | 16 ms | 26168 KB | Output is correct |
36 | Correct | 17 ms | 26324 KB | Output is correct |
37 | Correct | 16 ms | 26228 KB | Output is correct |
38 | Correct | 15 ms | 25736 KB | Output is correct |
39 | Correct | 17 ms | 26196 KB | Output is correct |
40 | Correct | 17 ms | 23956 KB | Output is correct |
41 | Correct | 15 ms | 26216 KB | Output is correct |
42 | Correct | 16 ms | 26196 KB | Output is correct |
43 | Correct | 14 ms | 24032 KB | Output is correct |
44 | Correct | 13 ms | 25736 KB | Output is correct |
45 | Correct | 16 ms | 26256 KB | Output is correct |
46 | Correct | 18 ms | 26168 KB | Output is correct |
47 | Correct | 17 ms | 25736 KB | Output is correct |
48 | Correct | 26 ms | 26192 KB | Output is correct |
49 | Correct | 23 ms | 26172 KB | Output is correct |
50 | Correct | 25 ms | 26324 KB | Output is correct |
51 | Correct | 22 ms | 26108 KB | Output is correct |
52 | Correct | 20 ms | 26124 KB | Output is correct |
53 | Correct | 21 ms | 26196 KB | Output is correct |
54 | Correct | 20 ms | 26176 KB | Output is correct |
55 | Correct | 20 ms | 25860 KB | Output is correct |
56 | Correct | 134 ms | 39920 KB | Output is correct |
57 | Correct | 166 ms | 39544 KB | Output is correct |
58 | Correct | 22 ms | 26196 KB | Output is correct |
59 | Correct | 19 ms | 25684 KB | Output is correct |
60 | Correct | 17 ms | 25684 KB | Output is correct |
61 | Correct | 205 ms | 39524 KB | Output is correct |
62 | Correct | 27 ms | 26092 KB | Output is correct |
63 | Correct | 263 ms | 44184 KB | Output is correct |
64 | Correct | 138 ms | 39552 KB | Output is correct |
65 | Correct | 15 ms | 26196 KB | Output is correct |
66 | Correct | 141 ms | 40204 KB | Output is correct |
67 | Correct | 15 ms | 23912 KB | Output is correct |
68 | Correct | 15 ms | 26228 KB | Output is correct |
69 | Correct | 122 ms | 39796 KB | Output is correct |
70 | Correct | 16 ms | 23892 KB | Output is correct |
71 | Correct | 58 ms | 28964 KB | Output is correct |
72 | Correct | 188 ms | 42556 KB | Output is correct |
73 | Correct | 17 ms | 25684 KB | Output is correct |
74 | Correct | 64 ms | 28872 KB | Output is correct |
75 | Correct | 215 ms | 43424 KB | Output is correct |
76 | Correct | 15 ms | 26196 KB | Output is correct |
77 | Correct | 16 ms | 26128 KB | Output is correct |
78 | Correct | 95 ms | 38420 KB | Output is correct |
79 | Correct | 123 ms | 42776 KB | Output is correct |
80 | Correct | 16 ms | 26196 KB | Output is correct |
81 | Correct | 19 ms | 26196 KB | Output is correct |
82 | Correct | 15 ms | 25736 KB | Output is correct |
83 | Correct | 16 ms | 25736 KB | Output is correct |
84 | Correct | 15 ms | 26184 KB | Output is correct |
85 | Correct | 171 ms | 40912 KB | Output is correct |
86 | Correct | 16 ms | 26196 KB | Output is correct |
87 | Correct | 159 ms | 39716 KB | Output is correct |
88 | Correct | 15 ms | 25720 KB | Output is correct |
89 | Correct | 184 ms | 39736 KB | Output is correct |
90 | Correct | 192 ms | 40940 KB | Output is correct |
91 | Correct | 145 ms | 42444 KB | Output is correct |
92 | Correct | 96 ms | 39804 KB | Output is correct |
93 | Correct | 16 ms | 26232 KB | Output is correct |
94 | Correct | 15 ms | 25812 KB | Output is correct |
95 | Correct | 132 ms | 42460 KB | Output is correct |
96 | Correct | 203 ms | 46568 KB | Output is correct |
97 | Correct | 16 ms | 26208 KB | Output is correct |
98 | Correct | 16 ms | 25812 KB | Output is correct |
99 | Correct | 126 ms | 41024 KB | Output is correct |
100 | Correct | 17 ms | 26252 KB | Output is correct |
101 | Correct | 199 ms | 40068 KB | Output is correct |
102 | Correct | 154 ms | 45532 KB | Output is correct |
103 | Correct | 15 ms | 26300 KB | Output is correct |
104 | Correct | 15 ms | 26324 KB | Output is correct |
105 | Correct | 17 ms | 26280 KB | Output is correct |
106 | Correct | 16 ms | 26196 KB | Output is correct |
107 | Correct | 17 ms | 26124 KB | Output is correct |
108 | Correct | 807 ms | 96600 KB | Output is correct |
109 | Correct | 1361 ms | 104936 KB | Output is correct |
110 | Correct | 363 ms | 45112 KB | Output is correct |
111 | Correct | 1511 ms | 132032 KB | Output is correct |
112 | Correct | 1285 ms | 121944 KB | Output is correct |
113 | Correct | 835 ms | 104088 KB | Output is correct |
114 | Correct | 974 ms | 90580 KB | Output is correct |
115 | Correct | 343 ms | 45612 KB | Output is correct |
116 | Correct | 1203 ms | 93448 KB | Output is correct |
117 | Correct | 1148 ms | 104224 KB | Output is correct |
118 | Correct | 960 ms | 90560 KB | Output is correct |
119 | Correct | 1114 ms | 104248 KB | Output is correct |
120 | Correct | 1288 ms | 121648 KB | Output is correct |
121 | Correct | 1098 ms | 104064 KB | Output is correct |
122 | Correct | 1178 ms | 97720 KB | Output is correct |
123 | Correct | 745 ms | 105392 KB | Output is correct |
124 | Correct | 1334 ms | 102432 KB | Output is correct |