Submission #571060

# Submission time Handle Problem Language Result Execution time Memory
571060 2022-06-01T07:39:24 Z grt Swapping Cities (APIO20_swap) C++17
100 / 100
439 ms 60924 KB
//GRT_2018
#include <bits/stdc++.h>
#define PB push_back
#define ST first
#define ND second
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

using namespace std;

using ll = long long;
using pi = pair<int,int>;
using vi = vector<int>;

const int INF = 1e9, nax = 100 * 1000 + 10;
vector<tuple<int, int, int>> edges;
int p[nax * 3], ed[nax * 3], leaves[nax * 3], sz[nax * 3];
int cost[nax * 3];
bool good[3 * nax];
int deg[nax], nxt;

int Fund(int x) {
	if(p[x] != x) {
		p[x] = Fund(p[x]);
	}
	return p[x];
}

vi G[3 * nax];

void Onion(int a, int b, int w) {
	int pa = Fund(a), pb = Fund(b);
	if(pa == pb) {
		p[pa] = nxt;
		p[nxt] = nxt;
		G[nxt].PB(pa);
		leaves[nxt] = leaves[pa], good[nxt] = good[pa], ed[nxt] = ed[pa];
		sz[nxt] = sz[pa];
	} else {
		p[pa] = nxt, p[pb] = nxt;
		p[nxt] = nxt;
		G[nxt].PB(pa);
		G[nxt].PB(pb);
		leaves[nxt] = leaves[pa] + leaves[pb];
		good[nxt] = good[pa] | good[pb];
		ed[nxt] = ed[pa] + ed[pb];
		sz[nxt] = sz[pa] + sz[pb];
	}
	if(deg[a] == 1) leaves[nxt]--;
	else if(deg[a] == 0) leaves[nxt]++;
	if(deg[b] == 1) leaves[nxt]--;
	else if(deg[b] == 0) leaves[nxt]++;
	deg[a]++, deg[b]++;
	ed[nxt]++;
	if(ed[nxt] >= sz[nxt]) {
		good[nxt] = 1;
	} else if(leaves[nxt] > 2) {
		good[nxt] = 1;
	}
	cost[nxt] = w;
	nxt++;
}

int par[3 * nax][19];
int dep[3 * nax];

void dfs(int x, int pr) {
	par[x][0] = pr;
	//cerr << x << ": " << good[x] << "\n";
	for(int y : G[x]) {
		//cerr << x << " -> " << y << "\n";
		dep[y] = dep[x] + 1;
		dfs(y, x);
	}
}

void init(int n, int m, vi U, vi V, vi W) {
	for(int i = 0; i < n; ++i) {
		p[i] = i;
		sz[i] = 1;
		leaves[i] = 0;
		ed[i] = deg[i] = 0;
	}
	for(int i = 0; i < m; ++i) {
		edges.emplace_back(W[i], U[i], V[i]);
	}
	sort(edges.begin(), edges.end());
	nxt = n;
	for(auto [w, u, v] : edges) {
		Onion(u, v, w);
	}
	nxt--;
	dfs(nxt, 0);
	par[nxt][0] = nxt;
	for(int step = 1; step < 19; ++step) {
		for(int i = 0; i <= nxt; ++i) {
			par[i][step] = par[par[i][step - 1]][step - 1];
		}
	}
}

int lca(int x, int y) {
	if(dep[x] > dep[y]) swap(x, y);
	for(int i = 18; i >= 0; --i) {
		if(dep[par[y][i]] >= dep[x]) {
			y = par[y][i];
		}
	}
	if(x == y) return x;
	for(int i = 18; i >= 0; --i) {
		if(par[x][i] != par[y][i]) {
			x = par[x][i];
			y = par[y][i];
		}
	}
	return par[x][0];
}

int getMinimumFuelCapacity(int x, int y) {
	int z = lca(x, y);
	//cerr << "Z: " << z << "\n";
	if(good[z]) return cost[z];
	for(int i = 18; i >= 0; --i) {
		if(!good[par[z][i]]) {
			z = par[z][i];
		}
	}
	z = par[z][0];
	if(!good[z]) {
		return -1;
	} else {
		return cost[z];
	}
}

//int main() {
	//ios_base::sync_with_stdio(0);
	//cin.tie(0);
	//init(5, 6, {0, 0, 1, 1, 1, 2}, {1, 2, 2, 3, 4, 3}, {4, 4, 1, 2, 10, 3});
	//cout << getMinimumFuelCapacity(1, 2) << "\n";
	//cout << getMinimumFuelCapacity(2, 4) << "\n";
	//cout << getMinimumFuelCapacity(0, 1) << "\n";
//}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 5 ms 7380 KB Output is correct
3 Correct 4 ms 7360 KB Output is correct
4 Correct 5 ms 7508 KB Output is correct
5 Correct 5 ms 7624 KB Output is correct
6 Correct 6 ms 7636 KB Output is correct
7 Correct 5 ms 7624 KB Output is correct
8 Correct 5 ms 7628 KB Output is correct
9 Correct 111 ms 29684 KB Output is correct
10 Correct 108 ms 34604 KB Output is correct
11 Correct 106 ms 34144 KB Output is correct
12 Correct 133 ms 35772 KB Output is correct
13 Correct 112 ms 37312 KB Output is correct
14 Correct 100 ms 30024 KB Output is correct
15 Correct 310 ms 38916 KB Output is correct
16 Correct 271 ms 38128 KB Output is correct
17 Correct 291 ms 39840 KB Output is correct
18 Correct 417 ms 41504 KB Output is correct
19 Correct 113 ms 16908 KB Output is correct
20 Correct 284 ms 40096 KB Output is correct
21 Correct 276 ms 39360 KB Output is correct
22 Correct 310 ms 41220 KB Output is correct
23 Correct 439 ms 42688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 5 ms 7380 KB Output is correct
3 Correct 341 ms 41928 KB Output is correct
4 Correct 354 ms 43252 KB Output is correct
5 Correct 303 ms 42608 KB Output is correct
6 Correct 333 ms 42988 KB Output is correct
7 Correct 306 ms 43072 KB Output is correct
8 Correct 306 ms 41732 KB Output is correct
9 Correct 316 ms 42624 KB Output is correct
10 Correct 347 ms 41464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 5 ms 7380 KB Output is correct
3 Correct 4 ms 7360 KB Output is correct
4 Correct 5 ms 7508 KB Output is correct
5 Correct 5 ms 7624 KB Output is correct
6 Correct 6 ms 7636 KB Output is correct
7 Correct 5 ms 7624 KB Output is correct
8 Correct 5 ms 7628 KB Output is correct
9 Correct 5 ms 7380 KB Output is correct
10 Correct 6 ms 7620 KB Output is correct
11 Correct 5 ms 7636 KB Output is correct
12 Correct 4 ms 7636 KB Output is correct
13 Correct 5 ms 7500 KB Output is correct
14 Correct 5 ms 7508 KB Output is correct
15 Correct 5 ms 7636 KB Output is correct
16 Correct 5 ms 7636 KB Output is correct
17 Correct 5 ms 7636 KB Output is correct
18 Correct 6 ms 7636 KB Output is correct
19 Correct 5 ms 7552 KB Output is correct
20 Correct 5 ms 7636 KB Output is correct
21 Correct 5 ms 7636 KB Output is correct
22 Correct 5 ms 7732 KB Output is correct
23 Correct 5 ms 7524 KB Output is correct
24 Correct 5 ms 7832 KB Output is correct
25 Correct 6 ms 7764 KB Output is correct
26 Correct 5 ms 7764 KB Output is correct
27 Correct 5 ms 7636 KB Output is correct
28 Correct 5 ms 7776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 5 ms 7380 KB Output is correct
4 Correct 4 ms 7360 KB Output is correct
5 Correct 5 ms 7508 KB Output is correct
6 Correct 5 ms 7624 KB Output is correct
7 Correct 6 ms 7636 KB Output is correct
8 Correct 5 ms 7624 KB Output is correct
9 Correct 5 ms 7628 KB Output is correct
10 Correct 111 ms 29684 KB Output is correct
11 Correct 108 ms 34604 KB Output is correct
12 Correct 106 ms 34144 KB Output is correct
13 Correct 133 ms 35772 KB Output is correct
14 Correct 112 ms 37312 KB Output is correct
15 Correct 6 ms 7620 KB Output is correct
16 Correct 5 ms 7636 KB Output is correct
17 Correct 4 ms 7636 KB Output is correct
18 Correct 5 ms 7500 KB Output is correct
19 Correct 5 ms 7508 KB Output is correct
20 Correct 5 ms 7636 KB Output is correct
21 Correct 5 ms 7636 KB Output is correct
22 Correct 5 ms 7636 KB Output is correct
23 Correct 6 ms 7636 KB Output is correct
24 Correct 5 ms 7552 KB Output is correct
25 Correct 5 ms 7636 KB Output is correct
26 Correct 5 ms 7636 KB Output is correct
27 Correct 5 ms 7732 KB Output is correct
28 Correct 5 ms 7524 KB Output is correct
29 Correct 5 ms 7832 KB Output is correct
30 Correct 6 ms 7764 KB Output is correct
31 Correct 5 ms 7764 KB Output is correct
32 Correct 5 ms 7636 KB Output is correct
33 Correct 5 ms 7776 KB Output is correct
34 Correct 19 ms 11156 KB Output is correct
35 Correct 112 ms 35520 KB Output is correct
36 Correct 107 ms 35512 KB Output is correct
37 Correct 118 ms 35488 KB Output is correct
38 Correct 116 ms 35120 KB Output is correct
39 Correct 109 ms 34980 KB Output is correct
40 Correct 97 ms 32564 KB Output is correct
41 Correct 137 ms 35776 KB Output is correct
42 Correct 107 ms 35536 KB Output is correct
43 Correct 100 ms 37424 KB Output is correct
44 Correct 116 ms 35904 KB Output is correct
45 Correct 168 ms 45112 KB Output is correct
46 Correct 113 ms 35520 KB Output is correct
47 Correct 108 ms 35664 KB Output is correct
48 Correct 118 ms 38328 KB Output is correct
49 Correct 132 ms 40904 KB Output is correct
50 Correct 85 ms 33696 KB Output is correct
51 Correct 123 ms 39956 KB Output is correct
52 Correct 169 ms 49028 KB Output is correct
53 Correct 224 ms 52116 KB Output is correct
54 Correct 186 ms 57044 KB Output is correct
55 Correct 122 ms 37312 KB Output is correct
56 Correct 227 ms 52192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 5 ms 7380 KB Output is correct
3 Correct 4 ms 7360 KB Output is correct
4 Correct 5 ms 7508 KB Output is correct
5 Correct 5 ms 7624 KB Output is correct
6 Correct 6 ms 7636 KB Output is correct
7 Correct 5 ms 7624 KB Output is correct
8 Correct 5 ms 7628 KB Output is correct
9 Correct 111 ms 29684 KB Output is correct
10 Correct 108 ms 34604 KB Output is correct
11 Correct 106 ms 34144 KB Output is correct
12 Correct 133 ms 35772 KB Output is correct
13 Correct 112 ms 37312 KB Output is correct
14 Correct 100 ms 30024 KB Output is correct
15 Correct 310 ms 38916 KB Output is correct
16 Correct 271 ms 38128 KB Output is correct
17 Correct 291 ms 39840 KB Output is correct
18 Correct 417 ms 41504 KB Output is correct
19 Correct 341 ms 41928 KB Output is correct
20 Correct 354 ms 43252 KB Output is correct
21 Correct 303 ms 42608 KB Output is correct
22 Correct 333 ms 42988 KB Output is correct
23 Correct 306 ms 43072 KB Output is correct
24 Correct 306 ms 41732 KB Output is correct
25 Correct 316 ms 42624 KB Output is correct
26 Correct 347 ms 41464 KB Output is correct
27 Correct 6 ms 7620 KB Output is correct
28 Correct 5 ms 7636 KB Output is correct
29 Correct 4 ms 7636 KB Output is correct
30 Correct 5 ms 7500 KB Output is correct
31 Correct 5 ms 7508 KB Output is correct
32 Correct 5 ms 7636 KB Output is correct
33 Correct 5 ms 7636 KB Output is correct
34 Correct 5 ms 7636 KB Output is correct
35 Correct 6 ms 7636 KB Output is correct
36 Correct 19 ms 11156 KB Output is correct
37 Correct 112 ms 35520 KB Output is correct
38 Correct 107 ms 35512 KB Output is correct
39 Correct 118 ms 35488 KB Output is correct
40 Correct 116 ms 35120 KB Output is correct
41 Correct 109 ms 34980 KB Output is correct
42 Correct 97 ms 32564 KB Output is correct
43 Correct 137 ms 35776 KB Output is correct
44 Correct 107 ms 35536 KB Output is correct
45 Correct 100 ms 37424 KB Output is correct
46 Correct 116 ms 35904 KB Output is correct
47 Correct 22 ms 11388 KB Output is correct
48 Correct 288 ms 40408 KB Output is correct
49 Correct 249 ms 40344 KB Output is correct
50 Correct 274 ms 40212 KB Output is correct
51 Correct 237 ms 40084 KB Output is correct
52 Correct 252 ms 38584 KB Output is correct
53 Correct 211 ms 31980 KB Output is correct
54 Correct 284 ms 41276 KB Output is correct
55 Correct 260 ms 40412 KB Output is correct
56 Correct 424 ms 41824 KB Output is correct
57 Correct 238 ms 41260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 5 ms 7380 KB Output is correct
4 Correct 4 ms 7360 KB Output is correct
5 Correct 5 ms 7508 KB Output is correct
6 Correct 5 ms 7624 KB Output is correct
7 Correct 6 ms 7636 KB Output is correct
8 Correct 5 ms 7624 KB Output is correct
9 Correct 5 ms 7628 KB Output is correct
10 Correct 111 ms 29684 KB Output is correct
11 Correct 108 ms 34604 KB Output is correct
12 Correct 106 ms 34144 KB Output is correct
13 Correct 133 ms 35772 KB Output is correct
14 Correct 112 ms 37312 KB Output is correct
15 Correct 100 ms 30024 KB Output is correct
16 Correct 310 ms 38916 KB Output is correct
17 Correct 271 ms 38128 KB Output is correct
18 Correct 291 ms 39840 KB Output is correct
19 Correct 417 ms 41504 KB Output is correct
20 Correct 341 ms 41928 KB Output is correct
21 Correct 354 ms 43252 KB Output is correct
22 Correct 303 ms 42608 KB Output is correct
23 Correct 333 ms 42988 KB Output is correct
24 Correct 306 ms 43072 KB Output is correct
25 Correct 306 ms 41732 KB Output is correct
26 Correct 316 ms 42624 KB Output is correct
27 Correct 347 ms 41464 KB Output is correct
28 Correct 6 ms 7620 KB Output is correct
29 Correct 5 ms 7636 KB Output is correct
30 Correct 4 ms 7636 KB Output is correct
31 Correct 5 ms 7500 KB Output is correct
32 Correct 5 ms 7508 KB Output is correct
33 Correct 5 ms 7636 KB Output is correct
34 Correct 5 ms 7636 KB Output is correct
35 Correct 5 ms 7636 KB Output is correct
36 Correct 6 ms 7636 KB Output is correct
37 Correct 19 ms 11156 KB Output is correct
38 Correct 112 ms 35520 KB Output is correct
39 Correct 107 ms 35512 KB Output is correct
40 Correct 118 ms 35488 KB Output is correct
41 Correct 116 ms 35120 KB Output is correct
42 Correct 109 ms 34980 KB Output is correct
43 Correct 97 ms 32564 KB Output is correct
44 Correct 137 ms 35776 KB Output is correct
45 Correct 107 ms 35536 KB Output is correct
46 Correct 100 ms 37424 KB Output is correct
47 Correct 116 ms 35904 KB Output is correct
48 Correct 22 ms 11388 KB Output is correct
49 Correct 288 ms 40408 KB Output is correct
50 Correct 249 ms 40344 KB Output is correct
51 Correct 274 ms 40212 KB Output is correct
52 Correct 237 ms 40084 KB Output is correct
53 Correct 252 ms 38584 KB Output is correct
54 Correct 211 ms 31980 KB Output is correct
55 Correct 284 ms 41276 KB Output is correct
56 Correct 260 ms 40412 KB Output is correct
57 Correct 424 ms 41824 KB Output is correct
58 Correct 238 ms 41260 KB Output is correct
59 Correct 113 ms 16908 KB Output is correct
60 Correct 284 ms 40096 KB Output is correct
61 Correct 276 ms 39360 KB Output is correct
62 Correct 310 ms 41220 KB Output is correct
63 Correct 439 ms 42688 KB Output is correct
64 Correct 5 ms 7552 KB Output is correct
65 Correct 5 ms 7636 KB Output is correct
66 Correct 5 ms 7636 KB Output is correct
67 Correct 5 ms 7732 KB Output is correct
68 Correct 5 ms 7524 KB Output is correct
69 Correct 5 ms 7832 KB Output is correct
70 Correct 6 ms 7764 KB Output is correct
71 Correct 5 ms 7764 KB Output is correct
72 Correct 5 ms 7636 KB Output is correct
73 Correct 5 ms 7776 KB Output is correct
74 Correct 168 ms 45112 KB Output is correct
75 Correct 113 ms 35520 KB Output is correct
76 Correct 108 ms 35664 KB Output is correct
77 Correct 118 ms 38328 KB Output is correct
78 Correct 132 ms 40904 KB Output is correct
79 Correct 85 ms 33696 KB Output is correct
80 Correct 123 ms 39956 KB Output is correct
81 Correct 169 ms 49028 KB Output is correct
82 Correct 224 ms 52116 KB Output is correct
83 Correct 186 ms 57044 KB Output is correct
84 Correct 122 ms 37312 KB Output is correct
85 Correct 227 ms 52192 KB Output is correct
86 Correct 101 ms 21624 KB Output is correct
87 Correct 233 ms 40472 KB Output is correct
88 Correct 295 ms 40452 KB Output is correct
89 Correct 292 ms 41704 KB Output is correct
90 Correct 225 ms 46884 KB Output is correct
91 Correct 282 ms 44556 KB Output is correct
92 Correct 343 ms 43816 KB Output is correct
93 Correct 358 ms 53140 KB Output is correct
94 Correct 371 ms 56468 KB Output is correct
95 Correct 393 ms 60924 KB Output is correct
96 Correct 379 ms 42152 KB Output is correct
97 Correct 370 ms 49364 KB Output is correct