# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
834765 |
2023-08-22T18:43:42 Z |
NK_ |
Mergers (JOI19_mergers) |
C++17 |
|
1304 ms |
160724 KB |
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define pb push_back
#define pf push_front
#define mp make_pair
#define f first
#define s second
#define sz(x) int(x.size())
template<class T> using V = vector<T>;
using pi = pair<int, int>;
using vi = V<int>;
using vpi = V<pi>;
using ll = long long;
using pl = pair<ll, ll>;
using vpl = V<pl>;
using vl = V<ll>;
using db = long double;
template<class T> using pq = priority_queue<T, V<T>, less<T>>;
const int MOD = 1e9 + 7;
const ll INFL = ll(1e17);
const int LG = 19;
int main() {
cin.tie(0)->sync_with_stdio(0);
int N, K; cin >> N >> K;
V<vi> adj(N); for(int i = 0; i < N - 1; i++) {
int u, v; cin >> u >> v; --u, --v;
adj[u].pb(v);
adj[v].pb(u);
}
vi A(N); for(auto& x : A) { cin >> x; --x; }
V<vi> up(N, vi(LG)); vi dep(N);
function<void(int, int)> gen = [&](int u, int p) {
up[u][0] = p; dep[u] = (u == p ? 0 : dep[p] + 1);
for(int i = 1; i < LG; i++) up[u][i] = up[up[u][i-1]][i-1];
for(auto& v : adj[u]) if (v != p) gen(v, u);
};
auto jmp = [&](int u, int d) {
for(int i = 0; i < LG; i++) if ((d >> i) & 1) u = up[u][i];
return u;
};
auto lca = [&](int a, int b) {
if (dep[a] < dep[b]) swap(a, b);
a = jmp(a, dep[a] - dep[b]);
if (a == b) return a;
for(int i = LG - 1; i >= 0; i--) {
if (up[a][i] != up[b][i]) a = up[a][i], b = up[b][i];
}
return up[a][0];
};
gen(0, 0);
vi L(K, -1); for(int u = 0; u < N; u++) {
if (L[A[u]] == -1) L[A[u]] = u;
else L[A[u]] = lca(L[A[u]], u);
}
int t = 0; vi U(N), st(N), en(N), VT;
function<void(int, int)> dfs = [&](int u, int p) {
U[u] = L[A[u]];
st[u] = t++;
for(auto& v : adj[u]) if (v != p) {
dfs(v, u);
U[u] = lca(U[u], U[v]);
}
en[u] = t - 1;
if (U[u] == u) VT.pb(u);
};
dfs(0, 0);
if (sz(VT) == 1) {
cout << 0 << nl;
exit(0-0);
}
sort(begin(VT), end(VT), [&](int u, int v) {
return st[u] < st[v];
});
vi stk = {-1}; V<vi> ADJ(N);
for(auto& u : VT) {
while(stk.back() != -1 && en[stk.back()] < st[u]) stk.pop_back();
if (stk.back() != -1) {
ADJ[stk.back()].pb(u);
ADJ[u].pb(stk.back());
}
stk.pb(u);
}
int leaves = 0;
for(auto& u : VT) if (size(ADJ[u]) == 1) leaves++;
cout << (leaves + 1) / 2 << nl;
exit(0-0);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
2 ms |
980 KB |
Output is correct |
27 |
Correct |
2 ms |
980 KB |
Output is correct |
28 |
Correct |
2 ms |
1108 KB |
Output is correct |
29 |
Correct |
2 ms |
1108 KB |
Output is correct |
30 |
Correct |
2 ms |
980 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
2 ms |
1108 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
2 ms |
852 KB |
Output is correct |
35 |
Correct |
2 ms |
1108 KB |
Output is correct |
36 |
Correct |
2 ms |
980 KB |
Output is correct |
37 |
Correct |
3 ms |
1024 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
2 ms |
980 KB |
Output is correct |
40 |
Correct |
2 ms |
852 KB |
Output is correct |
41 |
Correct |
2 ms |
980 KB |
Output is correct |
42 |
Correct |
2 ms |
980 KB |
Output is correct |
43 |
Correct |
2 ms |
1108 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
2 ms |
980 KB |
Output is correct |
46 |
Correct |
2 ms |
980 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
2 ms |
980 KB |
Output is correct |
49 |
Correct |
3 ms |
1108 KB |
Output is correct |
50 |
Correct |
2 ms |
1108 KB |
Output is correct |
51 |
Correct |
2 ms |
980 KB |
Output is correct |
52 |
Correct |
2 ms |
852 KB |
Output is correct |
53 |
Correct |
2 ms |
980 KB |
Output is correct |
54 |
Correct |
2 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
58 ms |
21232 KB |
Output is correct |
27 |
Correct |
83 ms |
20940 KB |
Output is correct |
28 |
Correct |
2 ms |
1104 KB |
Output is correct |
29 |
Correct |
1 ms |
224 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
88 ms |
23308 KB |
Output is correct |
32 |
Correct |
2 ms |
852 KB |
Output is correct |
33 |
Correct |
120 ms |
28208 KB |
Output is correct |
34 |
Correct |
89 ms |
23356 KB |
Output is correct |
35 |
Correct |
2 ms |
980 KB |
Output is correct |
36 |
Correct |
101 ms |
24080 KB |
Output is correct |
37 |
Correct |
2 ms |
852 KB |
Output is correct |
38 |
Correct |
2 ms |
976 KB |
Output is correct |
39 |
Correct |
57 ms |
21188 KB |
Output is correct |
40 |
Correct |
2 ms |
1108 KB |
Output is correct |
41 |
Correct |
82 ms |
20924 KB |
Output is correct |
42 |
Correct |
120 ms |
25624 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
119 ms |
28368 KB |
Output is correct |
45 |
Correct |
111 ms |
24648 KB |
Output is correct |
46 |
Correct |
2 ms |
852 KB |
Output is correct |
47 |
Correct |
2 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
19780 KB |
Output is correct |
2 |
Correct |
102 ms |
26568 KB |
Output is correct |
3 |
Correct |
3 ms |
980 KB |
Output is correct |
4 |
Correct |
2 ms |
980 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
2 ms |
852 KB |
Output is correct |
8 |
Correct |
92 ms |
24268 KB |
Output is correct |
9 |
Correct |
2 ms |
968 KB |
Output is correct |
10 |
Correct |
92 ms |
23500 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
110 ms |
23416 KB |
Output is correct |
13 |
Correct |
92 ms |
24444 KB |
Output is correct |
14 |
Correct |
120 ms |
27624 KB |
Output is correct |
15 |
Correct |
63 ms |
21164 KB |
Output is correct |
16 |
Correct |
2 ms |
980 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
88 ms |
27456 KB |
Output is correct |
19 |
Correct |
133 ms |
31732 KB |
Output is correct |
20 |
Correct |
2 ms |
980 KB |
Output is correct |
21 |
Correct |
1 ms |
324 KB |
Output is correct |
22 |
Correct |
68 ms |
24704 KB |
Output is correct |
23 |
Correct |
2 ms |
980 KB |
Output is correct |
24 |
Correct |
93 ms |
23884 KB |
Output is correct |
25 |
Correct |
124 ms |
30300 KB |
Output is correct |
26 |
Correct |
2 ms |
1108 KB |
Output is correct |
27 |
Correct |
3 ms |
1108 KB |
Output is correct |
28 |
Correct |
2 ms |
980 KB |
Output is correct |
29 |
Correct |
2 ms |
852 KB |
Output is correct |
30 |
Correct |
2 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
2 ms |
980 KB |
Output is correct |
27 |
Correct |
2 ms |
980 KB |
Output is correct |
28 |
Correct |
2 ms |
1108 KB |
Output is correct |
29 |
Correct |
2 ms |
1108 KB |
Output is correct |
30 |
Correct |
2 ms |
980 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
2 ms |
1108 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
2 ms |
852 KB |
Output is correct |
35 |
Correct |
2 ms |
1108 KB |
Output is correct |
36 |
Correct |
2 ms |
980 KB |
Output is correct |
37 |
Correct |
3 ms |
1024 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
2 ms |
980 KB |
Output is correct |
40 |
Correct |
2 ms |
852 KB |
Output is correct |
41 |
Correct |
2 ms |
980 KB |
Output is correct |
42 |
Correct |
2 ms |
980 KB |
Output is correct |
43 |
Correct |
2 ms |
1108 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
2 ms |
980 KB |
Output is correct |
46 |
Correct |
2 ms |
980 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
2 ms |
980 KB |
Output is correct |
49 |
Correct |
3 ms |
1108 KB |
Output is correct |
50 |
Correct |
2 ms |
1108 KB |
Output is correct |
51 |
Correct |
2 ms |
980 KB |
Output is correct |
52 |
Correct |
2 ms |
852 KB |
Output is correct |
53 |
Correct |
2 ms |
980 KB |
Output is correct |
54 |
Correct |
2 ms |
852 KB |
Output is correct |
55 |
Correct |
0 ms |
212 KB |
Output is correct |
56 |
Correct |
58 ms |
21232 KB |
Output is correct |
57 |
Correct |
83 ms |
20940 KB |
Output is correct |
58 |
Correct |
2 ms |
1104 KB |
Output is correct |
59 |
Correct |
1 ms |
224 KB |
Output is correct |
60 |
Correct |
1 ms |
212 KB |
Output is correct |
61 |
Correct |
88 ms |
23308 KB |
Output is correct |
62 |
Correct |
2 ms |
852 KB |
Output is correct |
63 |
Correct |
120 ms |
28208 KB |
Output is correct |
64 |
Correct |
89 ms |
23356 KB |
Output is correct |
65 |
Correct |
2 ms |
980 KB |
Output is correct |
66 |
Correct |
101 ms |
24080 KB |
Output is correct |
67 |
Correct |
2 ms |
852 KB |
Output is correct |
68 |
Correct |
2 ms |
976 KB |
Output is correct |
69 |
Correct |
57 ms |
21188 KB |
Output is correct |
70 |
Correct |
2 ms |
1108 KB |
Output is correct |
71 |
Correct |
82 ms |
20924 KB |
Output is correct |
72 |
Correct |
120 ms |
25624 KB |
Output is correct |
73 |
Correct |
1 ms |
340 KB |
Output is correct |
74 |
Correct |
119 ms |
28368 KB |
Output is correct |
75 |
Correct |
111 ms |
24648 KB |
Output is correct |
76 |
Correct |
2 ms |
852 KB |
Output is correct |
77 |
Correct |
2 ms |
852 KB |
Output is correct |
78 |
Correct |
58 ms |
19780 KB |
Output is correct |
79 |
Correct |
102 ms |
26568 KB |
Output is correct |
80 |
Correct |
3 ms |
980 KB |
Output is correct |
81 |
Correct |
2 ms |
980 KB |
Output is correct |
82 |
Correct |
0 ms |
212 KB |
Output is correct |
83 |
Correct |
0 ms |
212 KB |
Output is correct |
84 |
Correct |
2 ms |
852 KB |
Output is correct |
85 |
Correct |
92 ms |
24268 KB |
Output is correct |
86 |
Correct |
2 ms |
968 KB |
Output is correct |
87 |
Correct |
92 ms |
23500 KB |
Output is correct |
88 |
Correct |
1 ms |
340 KB |
Output is correct |
89 |
Correct |
110 ms |
23416 KB |
Output is correct |
90 |
Correct |
92 ms |
24444 KB |
Output is correct |
91 |
Correct |
120 ms |
27624 KB |
Output is correct |
92 |
Correct |
63 ms |
21164 KB |
Output is correct |
93 |
Correct |
2 ms |
980 KB |
Output is correct |
94 |
Correct |
0 ms |
340 KB |
Output is correct |
95 |
Correct |
88 ms |
27456 KB |
Output is correct |
96 |
Correct |
133 ms |
31732 KB |
Output is correct |
97 |
Correct |
2 ms |
980 KB |
Output is correct |
98 |
Correct |
1 ms |
324 KB |
Output is correct |
99 |
Correct |
68 ms |
24704 KB |
Output is correct |
100 |
Correct |
2 ms |
980 KB |
Output is correct |
101 |
Correct |
93 ms |
23884 KB |
Output is correct |
102 |
Correct |
124 ms |
30300 KB |
Output is correct |
103 |
Correct |
2 ms |
1108 KB |
Output is correct |
104 |
Correct |
3 ms |
1108 KB |
Output is correct |
105 |
Correct |
2 ms |
980 KB |
Output is correct |
106 |
Correct |
2 ms |
852 KB |
Output is correct |
107 |
Correct |
2 ms |
980 KB |
Output is correct |
108 |
Correct |
518 ms |
122348 KB |
Output is correct |
109 |
Correct |
1304 ms |
134060 KB |
Output is correct |
110 |
Correct |
1233 ms |
133692 KB |
Output is correct |
111 |
Correct |
1282 ms |
160724 KB |
Output is correct |
112 |
Correct |
1112 ms |
144028 KB |
Output is correct |
113 |
Correct |
705 ms |
137468 KB |
Output is correct |
114 |
Correct |
734 ms |
116036 KB |
Output is correct |
115 |
Correct |
686 ms |
104400 KB |
Output is correct |
116 |
Correct |
794 ms |
118460 KB |
Output is correct |
117 |
Correct |
919 ms |
138596 KB |
Output is correct |
118 |
Correct |
703 ms |
114764 KB |
Output is correct |
119 |
Correct |
931 ms |
138572 KB |
Output is correct |
120 |
Correct |
1259 ms |
152780 KB |
Output is correct |
121 |
Correct |
901 ms |
138544 KB |
Output is correct |
122 |
Correct |
816 ms |
122844 KB |
Output is correct |
123 |
Correct |
695 ms |
141516 KB |
Output is correct |
124 |
Correct |
994 ms |
129180 KB |
Output is correct |