Submission #788049

# Submission time Handle Problem Language Result Execution time Memory
788049 2023-07-19T16:57:28 Z ToniB Swapping Cities (APIO20_swap) C++17
100 / 100
339 ms 46836 KB
#include <bits/stdc++.h>
#include "swap.h"
#define X first
#define Y second
using namespace std;
typedef pair<int, int> pii;
const int MAXN = 1e5 + 2;
const int LOG = 18;

int n, m, cycle[MAXN], uf[MAXN], sz[MAXN], cnt[MAXN];
int dep[MAXN], bl[MAXN][LOG], bl_w[MAXN][LOG];
bool uf_bio[MAXN];
vector<pair<int, int> > adj[MAXN], tree[MAXN];
vector<pair<int, pii> > edges;

int f(int x){
	while(uf[x] != -1) x = uf[x];
	return x;
}
void join(int u, int v, int edge){
	if(uf_bio[u] ^ uf_bio[v]){
		if(uf_bio[v]) swap(u, v);
		cycle[v] = edge;
		uf_bio[v] = 1;
	}
	if(sz[u] > sz[v]) swap(u, v);
	sz[v] += sz[u];
	uf[u] = v;
}

int calc(int u, int v){
	if(dep[u] < dep[v]) swap(u, v);
	int ret = 0, lift = u;
	for(int i = 0; i < LOG; ++i){
		if(dep[u] - dep[v] & 1 << i){
			ret = max(ret, bl_w[lift][i]);
			lift = bl[lift][i];
		}
	}
	u = lift;

	if(u == v) return ret;

	for(int i = LOG - 1; i >= 0; --i){
		if(bl[u][i] != bl[v][i]){
			ret = max(ret, bl_w[u][i]);
			ret = max(ret, bl_w[v][i]);
			u = bl[u][i];
			v = bl[v][i];
		}
	}
	ret = max(ret, bl_w[u][0]);
	ret = max(ret, bl_w[v][0]);
	return ret;
}

void dfs(int node, int anc, int edge = 0){
	bl[node][0] = anc;
	bl_w[node][0] = edge;
	dep[node] = dep[anc] + 1;
	for(pii x : tree[node]){
		if(x.X == anc) continue;
		dfs(x.X, node, x.Y);
	}
}

void connect(int node, int edge){
	if(!uf_bio[node]){
		uf_bio[node] = 1;
		cycle[node] = edge;
	}
}

void init(int _n, int _m, vector<int> u, vector<int> v, vector<int> w){
	n = _n; m = _m;
	for(int i = 0; i < n; ++i){
		uf[i] = -1; sz[i] = 1;
	}
	
	for(int i = 0; i < m; ++i){
		adj[u[i]].push_back({v[i], w[i]});
		adj[v[i]].push_back({u[i], w[i]});
		edges.push_back({w[i], {u[i], v[i]}});
	}

	sort(edges.begin(), edges.end());

	for(auto p : edges){
		int u = p.Y.X, v = p.Y.Y;
		++cnt[u]; ++cnt[v];
		int fu = f(u), fv = f(v);
		if(cnt[u] >= 3) connect(fu, p.X);
		if(cnt[v] >= 3) connect(fv, p.X);
		u = fu; v = fv;
		if(u == v){
			connect(u, p.X);
		} else {
			join(u, v, p.X);
			tree[p.Y.X].push_back({p.Y.Y, p.X});
			tree[p.Y.Y].push_back({p.Y.X, p.X});
		}
	}
	
	for(int i = 0; i < n; ++i){
		int node = i;
		while(node != -1 && !uf_bio[node]){
			node = uf[node];
		}
		if(node != -1) cycle[i] = cycle[node];
	}
	
	dfs(0, 0);

	for(int j = 1; j < LOG; ++j){
		for(int i = 0; i < n; ++i){
			bl[i][j] = bl[bl[i][j - 1]][j - 1];
			bl_w[i][j] = max(bl_w[i][j - 1], bl_w[bl[i][j - 1]][j - 1]);
		}
	}
}
int getMinimumFuelCapacity(int x, int y){
	if(min(cycle[x], cycle[y]) == 0) return -1;
	int val = calc(x, y);
	return max(val, min(cycle[x], cycle[y]));
}

Compilation message

swap.cpp: In function 'int calc(int, int)':
swap.cpp:35:13: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   35 |   if(dep[u] - dep[v] & 1 << i){
      |      ~~~~~~~^~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5044 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 5204 KB Output is correct
6 Correct 3 ms 5204 KB Output is correct
7 Correct 3 ms 5204 KB Output is correct
8 Correct 3 ms 5332 KB Output is correct
9 Correct 80 ms 27200 KB Output is correct
10 Correct 114 ms 32920 KB Output is correct
11 Correct 99 ms 32168 KB Output is correct
12 Correct 110 ms 33920 KB Output is correct
13 Correct 98 ms 35076 KB Output is correct
14 Correct 80 ms 26868 KB Output is correct
15 Correct 160 ms 34784 KB Output is correct
16 Correct 141 ms 33072 KB Output is correct
17 Correct 153 ms 36736 KB Output is correct
18 Correct 143 ms 35916 KB Output is correct
19 Correct 67 ms 13260 KB Output is correct
20 Correct 204 ms 35984 KB Output is correct
21 Correct 247 ms 34716 KB Output is correct
22 Correct 238 ms 37176 KB Output is correct
23 Correct 212 ms 37480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5044 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 129 ms 34048 KB Output is correct
4 Correct 147 ms 39120 KB Output is correct
5 Correct 131 ms 38572 KB Output is correct
6 Correct 126 ms 38972 KB Output is correct
7 Correct 159 ms 38956 KB Output is correct
8 Correct 128 ms 37740 KB Output is correct
9 Correct 131 ms 38640 KB Output is correct
10 Correct 133 ms 37476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5044 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 5204 KB Output is correct
6 Correct 3 ms 5204 KB Output is correct
7 Correct 3 ms 5204 KB Output is correct
8 Correct 3 ms 5332 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 3 ms 5276 KB Output is correct
11 Correct 3 ms 5332 KB Output is correct
12 Correct 3 ms 5300 KB Output is correct
13 Correct 3 ms 5204 KB Output is correct
14 Correct 3 ms 5284 KB Output is correct
15 Correct 3 ms 5332 KB Output is correct
16 Correct 3 ms 5332 KB Output is correct
17 Correct 3 ms 5332 KB Output is correct
18 Correct 3 ms 5332 KB Output is correct
19 Correct 3 ms 5152 KB Output is correct
20 Correct 4 ms 5332 KB Output is correct
21 Correct 3 ms 5204 KB Output is correct
22 Correct 4 ms 5192 KB Output is correct
23 Correct 3 ms 5204 KB Output is correct
24 Correct 3 ms 5332 KB Output is correct
25 Correct 4 ms 5320 KB Output is correct
26 Correct 3 ms 5332 KB Output is correct
27 Correct 3 ms 5332 KB Output is correct
28 Correct 3 ms 5332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 5044 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5204 KB Output is correct
7 Correct 3 ms 5204 KB Output is correct
8 Correct 3 ms 5204 KB Output is correct
9 Correct 3 ms 5332 KB Output is correct
10 Correct 80 ms 27200 KB Output is correct
11 Correct 114 ms 32920 KB Output is correct
12 Correct 99 ms 32168 KB Output is correct
13 Correct 110 ms 33920 KB Output is correct
14 Correct 98 ms 35076 KB Output is correct
15 Correct 3 ms 5276 KB Output is correct
16 Correct 3 ms 5332 KB Output is correct
17 Correct 3 ms 5300 KB Output is correct
18 Correct 3 ms 5204 KB Output is correct
19 Correct 3 ms 5284 KB Output is correct
20 Correct 3 ms 5332 KB Output is correct
21 Correct 3 ms 5332 KB Output is correct
22 Correct 3 ms 5332 KB Output is correct
23 Correct 3 ms 5332 KB Output is correct
24 Correct 3 ms 5152 KB Output is correct
25 Correct 4 ms 5332 KB Output is correct
26 Correct 3 ms 5204 KB Output is correct
27 Correct 4 ms 5192 KB Output is correct
28 Correct 3 ms 5204 KB Output is correct
29 Correct 3 ms 5332 KB Output is correct
30 Correct 4 ms 5320 KB Output is correct
31 Correct 3 ms 5332 KB Output is correct
32 Correct 3 ms 5332 KB Output is correct
33 Correct 3 ms 5332 KB Output is correct
34 Correct 13 ms 8872 KB Output is correct
35 Correct 130 ms 35996 KB Output is correct
36 Correct 107 ms 34388 KB Output is correct
37 Correct 117 ms 33240 KB Output is correct
38 Correct 106 ms 32600 KB Output is correct
39 Correct 91 ms 32316 KB Output is correct
40 Correct 74 ms 30444 KB Output is correct
41 Correct 99 ms 35028 KB Output is correct
42 Correct 104 ms 35396 KB Output is correct
43 Correct 103 ms 36288 KB Output is correct
44 Correct 88 ms 34068 KB Output is correct
45 Correct 117 ms 33044 KB Output is correct
46 Correct 133 ms 33844 KB Output is correct
47 Correct 118 ms 32772 KB Output is correct
48 Correct 86 ms 33412 KB Output is correct
49 Correct 57 ms 18432 KB Output is correct
50 Correct 48 ms 16208 KB Output is correct
51 Correct 79 ms 28480 KB Output is correct
52 Correct 130 ms 39244 KB Output is correct
53 Correct 153 ms 40652 KB Output is correct
54 Correct 148 ms 42984 KB Output is correct
55 Correct 93 ms 37080 KB Output is correct
56 Correct 149 ms 39576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5044 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 5204 KB Output is correct
6 Correct 3 ms 5204 KB Output is correct
7 Correct 3 ms 5204 KB Output is correct
8 Correct 3 ms 5332 KB Output is correct
9 Correct 80 ms 27200 KB Output is correct
10 Correct 114 ms 32920 KB Output is correct
11 Correct 99 ms 32168 KB Output is correct
12 Correct 110 ms 33920 KB Output is correct
13 Correct 98 ms 35076 KB Output is correct
14 Correct 80 ms 26868 KB Output is correct
15 Correct 160 ms 34784 KB Output is correct
16 Correct 141 ms 33072 KB Output is correct
17 Correct 153 ms 36736 KB Output is correct
18 Correct 143 ms 35916 KB Output is correct
19 Correct 129 ms 34048 KB Output is correct
20 Correct 147 ms 39120 KB Output is correct
21 Correct 131 ms 38572 KB Output is correct
22 Correct 126 ms 38972 KB Output is correct
23 Correct 159 ms 38956 KB Output is correct
24 Correct 128 ms 37740 KB Output is correct
25 Correct 131 ms 38640 KB Output is correct
26 Correct 133 ms 37476 KB Output is correct
27 Correct 3 ms 5276 KB Output is correct
28 Correct 3 ms 5332 KB Output is correct
29 Correct 3 ms 5300 KB Output is correct
30 Correct 3 ms 5204 KB Output is correct
31 Correct 3 ms 5284 KB Output is correct
32 Correct 3 ms 5332 KB Output is correct
33 Correct 3 ms 5332 KB Output is correct
34 Correct 3 ms 5332 KB Output is correct
35 Correct 3 ms 5332 KB Output is correct
36 Correct 13 ms 8872 KB Output is correct
37 Correct 130 ms 35996 KB Output is correct
38 Correct 107 ms 34388 KB Output is correct
39 Correct 117 ms 33240 KB Output is correct
40 Correct 106 ms 32600 KB Output is correct
41 Correct 91 ms 32316 KB Output is correct
42 Correct 74 ms 30444 KB Output is correct
43 Correct 99 ms 35028 KB Output is correct
44 Correct 104 ms 35396 KB Output is correct
45 Correct 103 ms 36288 KB Output is correct
46 Correct 88 ms 34068 KB Output is correct
47 Correct 17 ms 9112 KB Output is correct
48 Correct 225 ms 39636 KB Output is correct
49 Correct 243 ms 38856 KB Output is correct
50 Correct 221 ms 38096 KB Output is correct
51 Correct 209 ms 37704 KB Output is correct
52 Correct 189 ms 36068 KB Output is correct
53 Correct 184 ms 30016 KB Output is correct
54 Correct 225 ms 40072 KB Output is correct
55 Correct 281 ms 39912 KB Output is correct
56 Correct 188 ms 41540 KB Output is correct
57 Correct 178 ms 39572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 5044 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5204 KB Output is correct
7 Correct 3 ms 5204 KB Output is correct
8 Correct 3 ms 5204 KB Output is correct
9 Correct 3 ms 5332 KB Output is correct
10 Correct 80 ms 27200 KB Output is correct
11 Correct 114 ms 32920 KB Output is correct
12 Correct 99 ms 32168 KB Output is correct
13 Correct 110 ms 33920 KB Output is correct
14 Correct 98 ms 35076 KB Output is correct
15 Correct 80 ms 26868 KB Output is correct
16 Correct 160 ms 34784 KB Output is correct
17 Correct 141 ms 33072 KB Output is correct
18 Correct 153 ms 36736 KB Output is correct
19 Correct 143 ms 35916 KB Output is correct
20 Correct 129 ms 34048 KB Output is correct
21 Correct 147 ms 39120 KB Output is correct
22 Correct 131 ms 38572 KB Output is correct
23 Correct 126 ms 38972 KB Output is correct
24 Correct 159 ms 38956 KB Output is correct
25 Correct 128 ms 37740 KB Output is correct
26 Correct 131 ms 38640 KB Output is correct
27 Correct 133 ms 37476 KB Output is correct
28 Correct 3 ms 5276 KB Output is correct
29 Correct 3 ms 5332 KB Output is correct
30 Correct 3 ms 5300 KB Output is correct
31 Correct 3 ms 5204 KB Output is correct
32 Correct 3 ms 5284 KB Output is correct
33 Correct 3 ms 5332 KB Output is correct
34 Correct 3 ms 5332 KB Output is correct
35 Correct 3 ms 5332 KB Output is correct
36 Correct 3 ms 5332 KB Output is correct
37 Correct 13 ms 8872 KB Output is correct
38 Correct 130 ms 35996 KB Output is correct
39 Correct 107 ms 34388 KB Output is correct
40 Correct 117 ms 33240 KB Output is correct
41 Correct 106 ms 32600 KB Output is correct
42 Correct 91 ms 32316 KB Output is correct
43 Correct 74 ms 30444 KB Output is correct
44 Correct 99 ms 35028 KB Output is correct
45 Correct 104 ms 35396 KB Output is correct
46 Correct 103 ms 36288 KB Output is correct
47 Correct 88 ms 34068 KB Output is correct
48 Correct 17 ms 9112 KB Output is correct
49 Correct 225 ms 39636 KB Output is correct
50 Correct 243 ms 38856 KB Output is correct
51 Correct 221 ms 38096 KB Output is correct
52 Correct 209 ms 37704 KB Output is correct
53 Correct 189 ms 36068 KB Output is correct
54 Correct 184 ms 30016 KB Output is correct
55 Correct 225 ms 40072 KB Output is correct
56 Correct 281 ms 39912 KB Output is correct
57 Correct 188 ms 41540 KB Output is correct
58 Correct 178 ms 39572 KB Output is correct
59 Correct 67 ms 13260 KB Output is correct
60 Correct 204 ms 35984 KB Output is correct
61 Correct 247 ms 34716 KB Output is correct
62 Correct 238 ms 37176 KB Output is correct
63 Correct 212 ms 37480 KB Output is correct
64 Correct 3 ms 5152 KB Output is correct
65 Correct 4 ms 5332 KB Output is correct
66 Correct 3 ms 5204 KB Output is correct
67 Correct 4 ms 5192 KB Output is correct
68 Correct 3 ms 5204 KB Output is correct
69 Correct 3 ms 5332 KB Output is correct
70 Correct 4 ms 5320 KB Output is correct
71 Correct 3 ms 5332 KB Output is correct
72 Correct 3 ms 5332 KB Output is correct
73 Correct 3 ms 5332 KB Output is correct
74 Correct 117 ms 33044 KB Output is correct
75 Correct 133 ms 33844 KB Output is correct
76 Correct 118 ms 32772 KB Output is correct
77 Correct 86 ms 33412 KB Output is correct
78 Correct 57 ms 18432 KB Output is correct
79 Correct 48 ms 16208 KB Output is correct
80 Correct 79 ms 28480 KB Output is correct
81 Correct 130 ms 39244 KB Output is correct
82 Correct 153 ms 40652 KB Output is correct
83 Correct 148 ms 42984 KB Output is correct
84 Correct 93 ms 37080 KB Output is correct
85 Correct 149 ms 39576 KB Output is correct
86 Correct 59 ms 16568 KB Output is correct
87 Correct 268 ms 38552 KB Output is correct
88 Correct 221 ms 38704 KB Output is correct
89 Correct 154 ms 36400 KB Output is correct
90 Correct 129 ms 22232 KB Output is correct
91 Correct 138 ms 23092 KB Output is correct
92 Correct 164 ms 33336 KB Output is correct
93 Correct 293 ms 44092 KB Output is correct
94 Correct 225 ms 44900 KB Output is correct
95 Correct 339 ms 46836 KB Output is correct
96 Correct 249 ms 40504 KB Output is correct
97 Correct 195 ms 41664 KB Output is correct