Submission #684215

# Submission time Handle Problem Language Result Execution time Memory
684215 2023-01-20T16:55:09 Z thiago_bastos Swapping Cities (APIO20_swap) C++17
100 / 100
157 ms 17108 KB
#include "swap.h"

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1e5 + 10;

int par[MAXN], depth[MAXN], t[MAXN], third[MAXN], deg[MAXN];

int findSet(int u) {
	return u == par[u] ? u : findSet(par[u]);
}

void merge(int u, int v, int w) {
	int a = findSet(u);
	int b = findSet(v);
	if(depth[a] > depth[b]) swap(a, b);
	++deg[u], ++deg[v];
	if(a == b) third[a] = min(third[a], w);
	else {
		par[a] = b;
		depth[b] += depth[a] == depth[b];
		int x = max(w, third[a]), y = max(w, third[b]);
		if(max(deg[u], deg[v]) > 2) x = min(x, w), y = min(y, w);	
		t[a] = w;
		third[b] = min(third[b], x);
		third[a] = min(third[a], y);
	}
}

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
	for(int i = 0; i < N; ++i) {
		par[i] = i;
		third[i] = INT_MAX;
	}
	vector<tuple<int, int, int>> edge(M);
	for(int i = 0; i < M; ++i) edge[i] = {W[i], U[i], V[i]};
	sort(edge.begin(), edge.end());
	for(auto [w, u, v] : edge) merge(u, v, w);
}

int h(int u) {
	return u == par[u] ? 0 : 1 + h(par[u]);
}

int getMinimumFuelCapacity(int X, int Y) {
	int hx = h(X), hy = h(Y);
	int ans = 0, trd = INT_MAX;
	while(hx != hy) {
		if(hx < hy) {
			swap(hx, hy);
			swap(X, Y);
		}
		--hx;
		trd = min(trd, third[X]);
		ans = max(ans, t[X]);
		X = par[X];
	}
	while(X != Y) {
		ans = max(ans, max(t[X], t[Y]));
		trd = min(trd, min(third[X], third[Y]));
		X = par[X];
		Y = par[Y];
	}
	trd = min(trd, third[X]);
	while(X != par[X] && trd == INT_MAX) {
		ans = max(ans, t[X]);
		X = par[X];
		trd = min(trd, third[X]);
	}	
	ans = max(ans, trd);
	return ans == INT_MAX ? -1 : ans;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 31 ms 4572 KB Output is correct
10 Correct 38 ms 5468 KB Output is correct
11 Correct 36 ms 5404 KB Output is correct
12 Correct 39 ms 5700 KB Output is correct
13 Correct 38 ms 5328 KB Output is correct
14 Correct 45 ms 4732 KB Output is correct
15 Correct 110 ms 7084 KB Output is correct
16 Correct 109 ms 6932 KB Output is correct
17 Correct 111 ms 7248 KB Output is correct
18 Correct 93 ms 6868 KB Output is correct
19 Correct 59 ms 4408 KB Output is correct
20 Correct 114 ms 7400 KB Output is correct
21 Correct 109 ms 7492 KB Output is correct
22 Correct 112 ms 7732 KB Output is correct
23 Correct 92 ms 7324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 84 ms 6952 KB Output is correct
4 Correct 91 ms 6952 KB Output is correct
5 Correct 95 ms 7332 KB Output is correct
6 Correct 83 ms 6944 KB Output is correct
7 Correct 86 ms 7220 KB Output is correct
8 Correct 85 ms 7024 KB Output is correct
9 Correct 89 ms 7092 KB Output is correct
10 Correct 84 ms 7044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 324 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 31 ms 4572 KB Output is correct
11 Correct 38 ms 5468 KB Output is correct
12 Correct 36 ms 5404 KB Output is correct
13 Correct 39 ms 5700 KB Output is correct
14 Correct 38 ms 5328 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 324 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 5 ms 980 KB Output is correct
35 Correct 37 ms 5776 KB Output is correct
36 Correct 36 ms 5704 KB Output is correct
37 Correct 36 ms 5720 KB Output is correct
38 Correct 36 ms 5700 KB Output is correct
39 Correct 36 ms 5696 KB Output is correct
40 Correct 35 ms 5208 KB Output is correct
41 Correct 39 ms 5712 KB Output is correct
42 Correct 36 ms 5708 KB Output is correct
43 Correct 36 ms 5708 KB Output is correct
44 Correct 38 ms 5716 KB Output is correct
45 Correct 61 ms 10080 KB Output is correct
46 Correct 39 ms 7488 KB Output is correct
47 Correct 37 ms 7500 KB Output is correct
48 Correct 45 ms 8136 KB Output is correct
49 Correct 57 ms 8544 KB Output is correct
50 Correct 45 ms 6988 KB Output is correct
51 Correct 54 ms 8780 KB Output is correct
52 Correct 65 ms 11272 KB Output is correct
53 Correct 71 ms 12240 KB Output is correct
54 Correct 87 ms 13144 KB Output is correct
55 Correct 36 ms 7484 KB Output is correct
56 Correct 70 ms 11852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 31 ms 4572 KB Output is correct
10 Correct 38 ms 5468 KB Output is correct
11 Correct 36 ms 5404 KB Output is correct
12 Correct 39 ms 5700 KB Output is correct
13 Correct 38 ms 5328 KB Output is correct
14 Correct 45 ms 4732 KB Output is correct
15 Correct 110 ms 7084 KB Output is correct
16 Correct 109 ms 6932 KB Output is correct
17 Correct 111 ms 7248 KB Output is correct
18 Correct 93 ms 6868 KB Output is correct
19 Correct 84 ms 6952 KB Output is correct
20 Correct 91 ms 6952 KB Output is correct
21 Correct 95 ms 7332 KB Output is correct
22 Correct 83 ms 6944 KB Output is correct
23 Correct 86 ms 7220 KB Output is correct
24 Correct 85 ms 7024 KB Output is correct
25 Correct 89 ms 7092 KB Output is correct
26 Correct 84 ms 7044 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 5 ms 980 KB Output is correct
37 Correct 37 ms 5776 KB Output is correct
38 Correct 36 ms 5704 KB Output is correct
39 Correct 36 ms 5720 KB Output is correct
40 Correct 36 ms 5700 KB Output is correct
41 Correct 36 ms 5696 KB Output is correct
42 Correct 35 ms 5208 KB Output is correct
43 Correct 39 ms 5712 KB Output is correct
44 Correct 36 ms 5708 KB Output is correct
45 Correct 36 ms 5708 KB Output is correct
46 Correct 38 ms 5716 KB Output is correct
47 Correct 11 ms 1216 KB Output is correct
48 Correct 110 ms 7292 KB Output is correct
49 Correct 114 ms 11460 KB Output is correct
50 Correct 112 ms 11452 KB Output is correct
51 Correct 106 ms 11376 KB Output is correct
52 Correct 109 ms 10956 KB Output is correct
53 Correct 112 ms 10304 KB Output is correct
54 Correct 114 ms 12188 KB Output is correct
55 Correct 113 ms 11360 KB Output is correct
56 Correct 99 ms 11340 KB Output is correct
57 Correct 108 ms 12200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 31 ms 4572 KB Output is correct
11 Correct 38 ms 5468 KB Output is correct
12 Correct 36 ms 5404 KB Output is correct
13 Correct 39 ms 5700 KB Output is correct
14 Correct 38 ms 5328 KB Output is correct
15 Correct 45 ms 4732 KB Output is correct
16 Correct 110 ms 7084 KB Output is correct
17 Correct 109 ms 6932 KB Output is correct
18 Correct 111 ms 7248 KB Output is correct
19 Correct 93 ms 6868 KB Output is correct
20 Correct 84 ms 6952 KB Output is correct
21 Correct 91 ms 6952 KB Output is correct
22 Correct 95 ms 7332 KB Output is correct
23 Correct 83 ms 6944 KB Output is correct
24 Correct 86 ms 7220 KB Output is correct
25 Correct 85 ms 7024 KB Output is correct
26 Correct 89 ms 7092 KB Output is correct
27 Correct 84 ms 7044 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 5 ms 980 KB Output is correct
38 Correct 37 ms 5776 KB Output is correct
39 Correct 36 ms 5704 KB Output is correct
40 Correct 36 ms 5720 KB Output is correct
41 Correct 36 ms 5700 KB Output is correct
42 Correct 36 ms 5696 KB Output is correct
43 Correct 35 ms 5208 KB Output is correct
44 Correct 39 ms 5712 KB Output is correct
45 Correct 36 ms 5708 KB Output is correct
46 Correct 36 ms 5708 KB Output is correct
47 Correct 38 ms 5716 KB Output is correct
48 Correct 11 ms 1216 KB Output is correct
49 Correct 110 ms 7292 KB Output is correct
50 Correct 114 ms 11460 KB Output is correct
51 Correct 112 ms 11452 KB Output is correct
52 Correct 106 ms 11376 KB Output is correct
53 Correct 109 ms 10956 KB Output is correct
54 Correct 112 ms 10304 KB Output is correct
55 Correct 114 ms 12188 KB Output is correct
56 Correct 113 ms 11360 KB Output is correct
57 Correct 99 ms 11340 KB Output is correct
58 Correct 108 ms 12200 KB Output is correct
59 Correct 59 ms 4408 KB Output is correct
60 Correct 114 ms 7400 KB Output is correct
61 Correct 109 ms 7492 KB Output is correct
62 Correct 112 ms 7732 KB Output is correct
63 Correct 92 ms 7324 KB Output is correct
64 Correct 1 ms 340 KB Output is correct
65 Correct 1 ms 340 KB Output is correct
66 Correct 1 ms 340 KB Output is correct
67 Correct 1 ms 340 KB Output is correct
68 Correct 1 ms 340 KB Output is correct
69 Correct 1 ms 340 KB Output is correct
70 Correct 1 ms 340 KB Output is correct
71 Correct 1 ms 340 KB Output is correct
72 Correct 1 ms 324 KB Output is correct
73 Correct 1 ms 364 KB Output is correct
74 Correct 61 ms 10080 KB Output is correct
75 Correct 39 ms 7488 KB Output is correct
76 Correct 37 ms 7500 KB Output is correct
77 Correct 45 ms 8136 KB Output is correct
78 Correct 57 ms 8544 KB Output is correct
79 Correct 45 ms 6988 KB Output is correct
80 Correct 54 ms 8780 KB Output is correct
81 Correct 65 ms 11272 KB Output is correct
82 Correct 71 ms 12240 KB Output is correct
83 Correct 87 ms 13144 KB Output is correct
84 Correct 36 ms 7484 KB Output is correct
85 Correct 70 ms 11852 KB Output is correct
86 Correct 36 ms 4600 KB Output is correct
87 Correct 111 ms 11356 KB Output is correct
88 Correct 118 ms 11460 KB Output is correct
89 Correct 101 ms 11524 KB Output is correct
90 Correct 118 ms 12496 KB Output is correct
91 Correct 123 ms 12108 KB Output is correct
92 Correct 109 ms 12236 KB Output is correct
93 Correct 138 ms 15052 KB Output is correct
94 Correct 147 ms 16188 KB Output is correct
95 Correct 157 ms 17108 KB Output is correct
96 Correct 94 ms 11452 KB Output is correct
97 Correct 128 ms 13804 KB Output is correct