# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
630388 |
2022-08-16T09:58:55 Z |
Arnch |
Mergers (JOI19_mergers) |
C++17 |
|
3000 ms |
260972 KB |
// oooo
/*
har chi delet mikhad bebar ~
gitar o ba khodet nabar! ~
;Amoo_Hasan;
*/
#include<bits/stdc++.h>
//#pragma GCC optimize("O3,no-stack-protector,unroll-loops")
//#pragma GCC target("avx2,fma")
using namespace std;
typedef long long ll;
typedef long double ld;
#define Sz(x) int((x).size())
#define All(x) (x).begin(), (x).end()
#define wtf(x) cout<<#x <<" : " <<x <<endl
#define mak make_pair
//constexpr int PRI = 1000696969;
constexpr ll INF = 1e18, N = 3e6 + 10, MOD = 1e9 + 7, LOG = 20;
int n, k;
int s[N], sub[N], head[N], link[N], fin[N];
int par[N][LOG], h[N], st[N], fn[N], tim;
int sz[N], pv[N];
int ans, total;
int seg[N];
vector<int> vc[N], adj[N], nei[N];
void dfs(int x, int p = -1) {
par[x][0] = p;
for(int i = 1; i < LOG; i++) par[x][i] = par[par[x][i - 1]][i - 1];
h[x] = h[p] + 1;
sub[x] = 1;
for(auto j : adj[x]) {
if(j == p) continue;
dfs(j, x);
sub[x] += sub[j];
}
}
void hld(int x, int p = -1, int hi = 0) {
link[tim] = x;
st[x] = tim++;
head[x] = hi;
int bz = -1;
for(auto j : adj[x]) {
if(j == p) continue;
if(bz == -1 || sub[bz] < sub[j]) bz = j;
}
if(bz == -1) {
fn[x] = tim;
return;
}
hld(bz, x, hi);
for(auto j : adj[x]) {
if(j == p || j == bz) continue;
hld(j, x, j);
}
fn[x] = tim;
}
int get_par(int x, int y) {
for(int i = 0; i < LOG; i++)
if((y >> i) & 1)
x = par[x][i];
return x;
}
int lca(int x, int y) {
if(h[x] > h[y]) swap(x, y);
y = get_par(y, h[y] - h[x]);
if(x == y) return x;
for(int i = LOG - 1; i >= 0; i--)
if(par[x][i] != par[y][i])
x = par[x][i], y = par[y][i];
return par[x][0];
}
int find(int x) {
if(pv[x] == x) return x;
return pv[x] = find(pv[x]);
}
void merge(int x, int y) {
// cout<<"^^" <<x <<' ' <<y <<endl;
int X = find(x), Y = find(y);
if(X == Y) return;
if(sz[X] < sz[Y]) swap(X, Y);
pv[Y] = X, sz[X] += sz[Y];
}
void build(int l = 0, int r = n, int v = 1) {
seg[v] = -1;
if(r - l < 2) {
fin[link[l]] = v;
return;
}
int mid = (l + r) >> 1;
build(l, mid, 2 * v), build(mid, r, 2 * v + 1);
total = max(total, v);
}
void upd(int s, int e, int val, int l = 0, int r = n, int v = 1) {
if(r <= s || l >= e) return;
if(l >= s && r <= e) {
if(seg[v] == -1) seg[v] = val;
else merge(seg[v], val);
return;
}
int mid = (l + r) >> 1;
upd(s, e, val, l, mid, 2 * v), upd(s, e, val, mid, r, 2 * v + 1);
}
void relax(int l = 0, int r = n, int v = 1) {
if(r - l < 2) {
if(seg[v] != -1) merge(seg[v], s[link[l]]);
return;
}
int mid = (l + r) >> 1;
if(seg[v] != -1) {
if(seg[2 * v] == -1) seg[2 * v] = seg[v];
else merge(seg[v], seg[2 * v]);
if(seg[2 * v + 1] == -1) seg[2 * v + 1] = seg[v];
else merge(seg[v], seg[2 * v + 1]);
}
relax(l, mid, 2 * v), relax(mid, r, 2 * v + 1);
}
bool cmp(int i, int j) {
return st[i] < st[j];
}
/*void solve(int x) {
vector<int> ver;
for(auto i : vc[x]) ver.push_back(i);
sort(All(ver), cmp);
int sz = Sz(ver);
for(int i = 1; i < sz; i++) {
ver.push_back(lca(ver[i - 1], ver[i]));
}
sort(All(ver), cmp);
ver.erase(unique(All(ver)), ver.end());
stack<int> mt;
mt.push(ver[0]);
for(int i = 1; i < Sz(ver); i++) {
int v = ver[i];
while(fn[mt.top()] < fn[v]) mt.pop();
int p = mt.top();
//cout<<"^^" <<v <<' ' <<p <<endl;
while(v != -1 && h[v] >= h[p]) {
if(h[head[v]] < h[p]) break;
upd(st[head[v]], st[v] + 1, x);
v = par[head[v]][0];
}
if(v != -1)
upd(st[p], st[v] + 1, x);
mt.push(v);
}
}
*/
void solve(int x) {
vector<int> ver;
for(auto i : vc[x]) ver.push_back(i);
sort(All(ver), cmp);
int sz = Sz(ver);
for(int i = 1; i < sz; i++) {
ver.push_back(lca(ver[i - 1], ver[i]));
}
sort(All(ver), cmp);
ver.erase(unique(All(ver)), ver.end());
stack<int> mt;
mt.push(ver[0]);
merge(ver[0], x);
for(int i = 1; i < Sz(ver); i++) {
int v = ver[i];
while(fn[mt.top()] < fn[v]) mt.pop();
int p = mt.top();
merge(x, v);
v = par[v][0];
while(v != p) {
merge(x, v);
v = par[v][0];
}
mt.push(v);
}
}
int main() {
ios :: sync_with_stdio(0), cin.tie(0); cout.tie(0);
for(int i = 0; i < N; i++) sz[i] = 1, pv[i] = i;
cin >>n >>k;
for(int i = 0; i < n - 1; i++) {
int u, v; cin >>u >>v;
--u, --v;
adj[u].push_back(v), adj[v].push_back(u);
}
dfs(0);
hld(0);
for(int i = 0; i < n; i++) {
cin >>s[i];
}
// build();
total = n;
for(int i = 0; i < n; i++) {
--s[i], s[i] += total;
vc[s[i]].push_back(i);
}
for(int i = total; i < total + k; i++) {
solve(i);
}
// assert(0);
// relax();
for(int i = 0; i < N; i++) pv[i] = find(pv[i]);
for(int i = 0; i < n; i++) {
int u = s[i];
for(auto j : adj[i]) {
int v = s[j];
if(pv[u] == pv[v]) continue;
nei[pv[u]].push_back(pv[v]);
nei[pv[v]].push_back(pv[u]);
}
}
for(int i = 0; i < N; i++) {
nei[i].erase(unique(All(nei[i])), nei[i].end());
if(Sz(nei[i]) == 1) ans++;
}
cout<<(ans + 1) / 2;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
118 ms |
235080 KB |
Output is correct |
2 |
Correct |
117 ms |
235160 KB |
Output is correct |
3 |
Correct |
119 ms |
235128 KB |
Output is correct |
4 |
Correct |
119 ms |
235200 KB |
Output is correct |
5 |
Correct |
118 ms |
235084 KB |
Output is correct |
6 |
Correct |
120 ms |
235180 KB |
Output is correct |
7 |
Correct |
119 ms |
235184 KB |
Output is correct |
8 |
Correct |
119 ms |
235104 KB |
Output is correct |
9 |
Correct |
117 ms |
235208 KB |
Output is correct |
10 |
Correct |
119 ms |
235164 KB |
Output is correct |
11 |
Correct |
118 ms |
235208 KB |
Output is correct |
12 |
Correct |
133 ms |
235140 KB |
Output is correct |
13 |
Correct |
122 ms |
235192 KB |
Output is correct |
14 |
Correct |
120 ms |
235108 KB |
Output is correct |
15 |
Correct |
120 ms |
235192 KB |
Output is correct |
16 |
Correct |
118 ms |
235092 KB |
Output is correct |
17 |
Correct |
121 ms |
235116 KB |
Output is correct |
18 |
Correct |
120 ms |
235212 KB |
Output is correct |
19 |
Correct |
124 ms |
235168 KB |
Output is correct |
20 |
Correct |
119 ms |
235144 KB |
Output is correct |
21 |
Correct |
129 ms |
235180 KB |
Output is correct |
22 |
Correct |
128 ms |
235212 KB |
Output is correct |
23 |
Correct |
133 ms |
235180 KB |
Output is correct |
24 |
Correct |
128 ms |
235200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
118 ms |
235080 KB |
Output is correct |
2 |
Correct |
117 ms |
235160 KB |
Output is correct |
3 |
Correct |
119 ms |
235128 KB |
Output is correct |
4 |
Correct |
119 ms |
235200 KB |
Output is correct |
5 |
Correct |
118 ms |
235084 KB |
Output is correct |
6 |
Correct |
120 ms |
235180 KB |
Output is correct |
7 |
Correct |
119 ms |
235184 KB |
Output is correct |
8 |
Correct |
119 ms |
235104 KB |
Output is correct |
9 |
Correct |
117 ms |
235208 KB |
Output is correct |
10 |
Correct |
119 ms |
235164 KB |
Output is correct |
11 |
Correct |
118 ms |
235208 KB |
Output is correct |
12 |
Correct |
133 ms |
235140 KB |
Output is correct |
13 |
Correct |
122 ms |
235192 KB |
Output is correct |
14 |
Correct |
120 ms |
235108 KB |
Output is correct |
15 |
Correct |
120 ms |
235192 KB |
Output is correct |
16 |
Correct |
118 ms |
235092 KB |
Output is correct |
17 |
Correct |
121 ms |
235116 KB |
Output is correct |
18 |
Correct |
120 ms |
235212 KB |
Output is correct |
19 |
Correct |
124 ms |
235168 KB |
Output is correct |
20 |
Correct |
119 ms |
235144 KB |
Output is correct |
21 |
Correct |
129 ms |
235180 KB |
Output is correct |
22 |
Correct |
128 ms |
235212 KB |
Output is correct |
23 |
Correct |
133 ms |
235180 KB |
Output is correct |
24 |
Correct |
128 ms |
235200 KB |
Output is correct |
25 |
Correct |
143 ms |
235144 KB |
Output is correct |
26 |
Correct |
126 ms |
235756 KB |
Output is correct |
27 |
Correct |
145 ms |
235692 KB |
Output is correct |
28 |
Correct |
130 ms |
235828 KB |
Output is correct |
29 |
Correct |
131 ms |
235868 KB |
Output is correct |
30 |
Correct |
133 ms |
235764 KB |
Output is correct |
31 |
Correct |
202 ms |
235192 KB |
Output is correct |
32 |
Correct |
130 ms |
235928 KB |
Output is correct |
33 |
Correct |
182 ms |
235268 KB |
Output is correct |
34 |
Correct |
139 ms |
235596 KB |
Output is correct |
35 |
Correct |
143 ms |
235876 KB |
Output is correct |
36 |
Correct |
134 ms |
235596 KB |
Output is correct |
37 |
Correct |
143 ms |
235600 KB |
Output is correct |
38 |
Correct |
151 ms |
235228 KB |
Output is correct |
39 |
Correct |
139 ms |
235664 KB |
Output is correct |
40 |
Correct |
151 ms |
235612 KB |
Output is correct |
41 |
Correct |
149 ms |
235676 KB |
Output is correct |
42 |
Correct |
141 ms |
235680 KB |
Output is correct |
43 |
Correct |
208 ms |
236048 KB |
Output is correct |
44 |
Correct |
140 ms |
235136 KB |
Output is correct |
45 |
Correct |
169 ms |
235880 KB |
Output is correct |
46 |
Correct |
178 ms |
235600 KB |
Output is correct |
47 |
Correct |
142 ms |
235212 KB |
Output is correct |
48 |
Correct |
155 ms |
235596 KB |
Output is correct |
49 |
Correct |
163 ms |
236000 KB |
Output is correct |
50 |
Correct |
148 ms |
235948 KB |
Output is correct |
51 |
Correct |
134 ms |
235636 KB |
Output is correct |
52 |
Correct |
146 ms |
235628 KB |
Output is correct |
53 |
Correct |
145 ms |
235724 KB |
Output is correct |
54 |
Correct |
143 ms |
235680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
118 ms |
235080 KB |
Output is correct |
2 |
Correct |
117 ms |
235160 KB |
Output is correct |
3 |
Correct |
119 ms |
235128 KB |
Output is correct |
4 |
Correct |
119 ms |
235200 KB |
Output is correct |
5 |
Correct |
118 ms |
235084 KB |
Output is correct |
6 |
Correct |
120 ms |
235180 KB |
Output is correct |
7 |
Correct |
119 ms |
235184 KB |
Output is correct |
8 |
Correct |
119 ms |
235104 KB |
Output is correct |
9 |
Correct |
117 ms |
235208 KB |
Output is correct |
10 |
Correct |
119 ms |
235164 KB |
Output is correct |
11 |
Correct |
118 ms |
235208 KB |
Output is correct |
12 |
Correct |
133 ms |
235140 KB |
Output is correct |
13 |
Correct |
122 ms |
235192 KB |
Output is correct |
14 |
Correct |
120 ms |
235108 KB |
Output is correct |
15 |
Correct |
120 ms |
235192 KB |
Output is correct |
16 |
Correct |
118 ms |
235092 KB |
Output is correct |
17 |
Correct |
121 ms |
235116 KB |
Output is correct |
18 |
Correct |
120 ms |
235212 KB |
Output is correct |
19 |
Correct |
124 ms |
235168 KB |
Output is correct |
20 |
Correct |
119 ms |
235144 KB |
Output is correct |
21 |
Correct |
129 ms |
235180 KB |
Output is correct |
22 |
Correct |
128 ms |
235212 KB |
Output is correct |
23 |
Correct |
133 ms |
235180 KB |
Output is correct |
24 |
Correct |
128 ms |
235200 KB |
Output is correct |
25 |
Correct |
136 ms |
235216 KB |
Output is correct |
26 |
Correct |
222 ms |
250380 KB |
Output is correct |
27 |
Correct |
287 ms |
250096 KB |
Output is correct |
28 |
Correct |
133 ms |
235592 KB |
Output is correct |
29 |
Correct |
138 ms |
235096 KB |
Output is correct |
30 |
Correct |
131 ms |
235148 KB |
Output is correct |
31 |
Correct |
259 ms |
250880 KB |
Output is correct |
32 |
Correct |
135 ms |
235596 KB |
Output is correct |
33 |
Execution timed out |
3099 ms |
258164 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
217 ms |
250488 KB |
Output is correct |
2 |
Correct |
229 ms |
256780 KB |
Output is correct |
3 |
Correct |
133 ms |
235784 KB |
Output is correct |
4 |
Correct |
136 ms |
235624 KB |
Output is correct |
5 |
Correct |
132 ms |
235096 KB |
Output is correct |
6 |
Correct |
132 ms |
235152 KB |
Output is correct |
7 |
Correct |
135 ms |
235732 KB |
Output is correct |
8 |
Correct |
253 ms |
252480 KB |
Output is correct |
9 |
Correct |
133 ms |
235660 KB |
Output is correct |
10 |
Correct |
249 ms |
251160 KB |
Output is correct |
11 |
Correct |
138 ms |
235296 KB |
Output is correct |
12 |
Correct |
278 ms |
251068 KB |
Output is correct |
13 |
Correct |
246 ms |
253004 KB |
Output is correct |
14 |
Correct |
262 ms |
257764 KB |
Output is correct |
15 |
Correct |
219 ms |
251232 KB |
Output is correct |
16 |
Correct |
135 ms |
235664 KB |
Output is correct |
17 |
Correct |
131 ms |
235136 KB |
Output is correct |
18 |
Correct |
230 ms |
257316 KB |
Output is correct |
19 |
Correct |
281 ms |
260972 KB |
Output is correct |
20 |
Correct |
137 ms |
235608 KB |
Output is correct |
21 |
Correct |
135 ms |
235204 KB |
Output is correct |
22 |
Correct |
220 ms |
253264 KB |
Output is correct |
23 |
Correct |
133 ms |
235596 KB |
Output is correct |
24 |
Correct |
248 ms |
251844 KB |
Output is correct |
25 |
Correct |
288 ms |
260812 KB |
Output is correct |
26 |
Correct |
135 ms |
235996 KB |
Output is correct |
27 |
Correct |
135 ms |
235888 KB |
Output is correct |
28 |
Correct |
134 ms |
235584 KB |
Output is correct |
29 |
Correct |
134 ms |
235584 KB |
Output is correct |
30 |
Correct |
134 ms |
235680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
118 ms |
235080 KB |
Output is correct |
2 |
Correct |
117 ms |
235160 KB |
Output is correct |
3 |
Correct |
119 ms |
235128 KB |
Output is correct |
4 |
Correct |
119 ms |
235200 KB |
Output is correct |
5 |
Correct |
118 ms |
235084 KB |
Output is correct |
6 |
Correct |
120 ms |
235180 KB |
Output is correct |
7 |
Correct |
119 ms |
235184 KB |
Output is correct |
8 |
Correct |
119 ms |
235104 KB |
Output is correct |
9 |
Correct |
117 ms |
235208 KB |
Output is correct |
10 |
Correct |
119 ms |
235164 KB |
Output is correct |
11 |
Correct |
118 ms |
235208 KB |
Output is correct |
12 |
Correct |
133 ms |
235140 KB |
Output is correct |
13 |
Correct |
122 ms |
235192 KB |
Output is correct |
14 |
Correct |
120 ms |
235108 KB |
Output is correct |
15 |
Correct |
120 ms |
235192 KB |
Output is correct |
16 |
Correct |
118 ms |
235092 KB |
Output is correct |
17 |
Correct |
121 ms |
235116 KB |
Output is correct |
18 |
Correct |
120 ms |
235212 KB |
Output is correct |
19 |
Correct |
124 ms |
235168 KB |
Output is correct |
20 |
Correct |
119 ms |
235144 KB |
Output is correct |
21 |
Correct |
129 ms |
235180 KB |
Output is correct |
22 |
Correct |
128 ms |
235212 KB |
Output is correct |
23 |
Correct |
133 ms |
235180 KB |
Output is correct |
24 |
Correct |
128 ms |
235200 KB |
Output is correct |
25 |
Correct |
143 ms |
235144 KB |
Output is correct |
26 |
Correct |
126 ms |
235756 KB |
Output is correct |
27 |
Correct |
145 ms |
235692 KB |
Output is correct |
28 |
Correct |
130 ms |
235828 KB |
Output is correct |
29 |
Correct |
131 ms |
235868 KB |
Output is correct |
30 |
Correct |
133 ms |
235764 KB |
Output is correct |
31 |
Correct |
202 ms |
235192 KB |
Output is correct |
32 |
Correct |
130 ms |
235928 KB |
Output is correct |
33 |
Correct |
182 ms |
235268 KB |
Output is correct |
34 |
Correct |
139 ms |
235596 KB |
Output is correct |
35 |
Correct |
143 ms |
235876 KB |
Output is correct |
36 |
Correct |
134 ms |
235596 KB |
Output is correct |
37 |
Correct |
143 ms |
235600 KB |
Output is correct |
38 |
Correct |
151 ms |
235228 KB |
Output is correct |
39 |
Correct |
139 ms |
235664 KB |
Output is correct |
40 |
Correct |
151 ms |
235612 KB |
Output is correct |
41 |
Correct |
149 ms |
235676 KB |
Output is correct |
42 |
Correct |
141 ms |
235680 KB |
Output is correct |
43 |
Correct |
208 ms |
236048 KB |
Output is correct |
44 |
Correct |
140 ms |
235136 KB |
Output is correct |
45 |
Correct |
169 ms |
235880 KB |
Output is correct |
46 |
Correct |
178 ms |
235600 KB |
Output is correct |
47 |
Correct |
142 ms |
235212 KB |
Output is correct |
48 |
Correct |
155 ms |
235596 KB |
Output is correct |
49 |
Correct |
163 ms |
236000 KB |
Output is correct |
50 |
Correct |
148 ms |
235948 KB |
Output is correct |
51 |
Correct |
134 ms |
235636 KB |
Output is correct |
52 |
Correct |
146 ms |
235628 KB |
Output is correct |
53 |
Correct |
145 ms |
235724 KB |
Output is correct |
54 |
Correct |
143 ms |
235680 KB |
Output is correct |
55 |
Correct |
136 ms |
235216 KB |
Output is correct |
56 |
Correct |
222 ms |
250380 KB |
Output is correct |
57 |
Correct |
287 ms |
250096 KB |
Output is correct |
58 |
Correct |
133 ms |
235592 KB |
Output is correct |
59 |
Correct |
138 ms |
235096 KB |
Output is correct |
60 |
Correct |
131 ms |
235148 KB |
Output is correct |
61 |
Correct |
259 ms |
250880 KB |
Output is correct |
62 |
Correct |
135 ms |
235596 KB |
Output is correct |
63 |
Execution timed out |
3099 ms |
258164 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |