Submission #965950

# Submission time Handle Problem Language Result Execution time Memory
965950 2024-04-19T08:36:50 Z Gromp15 Swapping Cities (APIO20_swap) C++17
100 / 100
450 ms 84560 KB
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
#define sz(x) int(x.size())
#define all(x) x.begin(), x.end()
#define db double
#define ll long long
#define ar array
template<typename T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<typename T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

const int LOG = 17;
struct dsu {
	vector<vector<int>> adj;
	vector<int> deg, p, sub;
	vector<bool> mark;
	vector<vector<int>> up;
	vector<int> depth, good;
	int who, N;
	dsu() {}
	dsu(int n, int m) : N(n), adj(n + m), deg(n), p(n + m), who(n), sub(n + m), mark(n + m), up(n + m, vector<int>(LOG, -1)), depth(n + m), good(n + m, -1) {
		iota(all(p), 0);
	}
	int get(int v) {
		return v == p[v] ? v : p[v] = get(p[v]);
	}
	void add(int x, int y) {
		int ox = x, oy = y;
		x = get(x), y = get(y);
		p[x] = who, p[y] = who;
		if (deg[ox] == 0) sub[who] += -1;
		else if (deg[ox] == 1) sub[who] += 1;
		if (deg[oy] == 0) sub[who] += -1;
		else if (deg[oy] == 1) sub[who] += 1;
		deg[ox]++, deg[oy]++;
		if (deg[ox] > 2 || deg[oy] > 2) mark[who] = 1;
		adj[who].emplace_back(x);
		if (x != y) adj[who].emplace_back(y);
		who++;
	}
	void process(int sx) {
		auto dfs = [&](auto&& s, int v, int p) -> void {
			for (int u : adj[v]) if (u != p) {
				up[u][0] = v, depth[u] = depth[v] + 1;
				for (int i = 1; i < LOG; i++) {
					if (~up[u][i-1]) up[u][i] = up[up[u][i-1]][i-1];
				}
				s(s, u, v);
				sub[v] += sub[u];
				mark[v] = mark[v] || mark[u];
			}
		};
		dfs(dfs, sx, -1);
		auto dfs2 = [&](auto&& s, int v, int p, int lst) -> void {
			if (v >= N && (mark[v] || sub[v] == 0)) lst = v;
			good[v] = lst;
			for (int u : adj[v]) if (u != p) {
				s(s, u, v, lst);
			}
		};
		dfs2(dfs2, sx, -1, -1);
	}
	int lca(int x, int y) {
		if (depth[x] < depth[y]) swap(x, y);
		int diff = depth[x] - depth[y];
		for (int i = 0; i < LOG; i++) if (diff >> i & 1) x = up[x][i];
		assert(depth[x] == depth[y]);
		for (int i = LOG-1; i >= 0; i--) if (up[x][i] != up[y][i]) x = up[x][i], y = up[y][i];
		return x == y ? x : up[x][0];
	}
};
const int INF = 1e9;
vector<vector<ar<int, 3>>> adj;
vector<int> weights;
int n, m;
dsu d;

void init(int N, int M,
          std::vector<int> U, std::vector<int> V, std::vector<int> W) {
	n = N, m = M;
	adj.resize(N + 1);
	d = dsu(n, m);
	for (int i = 0; i < M; i++) {
		adj[U[i]].push_back({V[i], W[i], i});
		adj[V[i]].push_back({U[i], W[i], i});
	}
	vector<int> idx(M);
	iota(all(idx), 0);
	sort(all(idx), [&](int x, int y) { return W[x] < W[y]; });
	for (int i = 0; i < M; i++) {
		d.add(U[idx[i]], V[idx[i]]);
	}
	d.process(n + m - 1);
	weights.resize(m);
	for (int i = 0; i < m; i++) {
		weights[i] = W[idx[i]];
	}
}

int getMinimumFuelCapacity(int X, int Y) {
	int ans = d.good[d.lca(X, Y)];
	return ~ans ? weights[ans - n] : -1;
}

Compilation message

swap.cpp: In constructor 'dsu::dsu(int, int)':
swap.cpp:19:11: warning: 'dsu::N' will be initialized after [-Wreorder]
   19 |  int who, N;
      |           ^
swap.cpp:14:22: warning:   'std::vector<std::vector<int> > dsu::adj' [-Wreorder]
   14 |  vector<vector<int>> adj;
      |                      ^~~
swap.cpp:21:2: warning:   when initialized here [-Wreorder]
   21 |  dsu(int n, int m) : N(n), adj(n + m), deg(n), p(n + m), who(n), sub(n + m), mark(n + m), up(n + m, vector<int>(LOG, -1)), depth(n + m), good(n + m, -1) {
      |  ^~~
swap.cpp:19:6: warning: 'dsu::who' will be initialized after [-Wreorder]
   19 |  int who, N;
      |      ^~~
swap.cpp:15:22: warning:   'std::vector<int> dsu::sub' [-Wreorder]
   15 |  vector<int> deg, p, sub;
      |                      ^~~
swap.cpp:21:2: warning:   when initialized here [-Wreorder]
   21 |  dsu(int n, int m) : N(n), adj(n + m), deg(n), p(n + m), who(n), sub(n + m), mark(n + m), up(n + m, vector<int>(LOG, -1)), depth(n + m), good(n + m, -1) {
      |  ^~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 95 ms 33108 KB Output is correct
10 Correct 132 ms 40272 KB Output is correct
11 Correct 117 ms 39644 KB Output is correct
12 Correct 128 ms 42000 KB Output is correct
13 Correct 173 ms 47408 KB Output is correct
14 Correct 113 ms 33356 KB Output is correct
15 Correct 226 ms 43364 KB Output is correct
16 Correct 237 ms 42308 KB Output is correct
17 Correct 284 ms 45032 KB Output is correct
18 Correct 326 ms 50512 KB Output is correct
19 Correct 72 ms 11344 KB Output is correct
20 Correct 231 ms 44476 KB Output is correct
21 Correct 286 ms 43168 KB Output is correct
22 Correct 252 ms 46032 KB Output is correct
23 Correct 402 ms 51400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 309 ms 52904 KB Output is correct
4 Correct 307 ms 59428 KB Output is correct
5 Correct 289 ms 58012 KB Output is correct
6 Correct 313 ms 59252 KB Output is correct
7 Correct 311 ms 58756 KB Output is correct
8 Correct 288 ms 56504 KB Output is correct
9 Correct 285 ms 58156 KB Output is correct
10 Correct 276 ms 55884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 708 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 860 KB Output is correct
17 Correct 1 ms 856 KB Output is correct
18 Correct 1 ms 860 KB Output is correct
19 Correct 1 ms 792 KB Output is correct
20 Correct 1 ms 856 KB Output is correct
21 Correct 1 ms 860 KB Output is correct
22 Correct 2 ms 860 KB Output is correct
23 Correct 1 ms 648 KB Output is correct
24 Correct 2 ms 1116 KB Output is correct
25 Correct 2 ms 1116 KB Output is correct
26 Correct 2 ms 1116 KB Output is correct
27 Correct 1 ms 928 KB Output is correct
28 Correct 3 ms 1012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 95 ms 33108 KB Output is correct
11 Correct 132 ms 40272 KB Output is correct
12 Correct 117 ms 39644 KB Output is correct
13 Correct 128 ms 42000 KB Output is correct
14 Correct 173 ms 47408 KB Output is correct
15 Correct 1 ms 708 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 860 KB Output is correct
22 Correct 1 ms 856 KB Output is correct
23 Correct 1 ms 860 KB Output is correct
24 Correct 1 ms 792 KB Output is correct
25 Correct 1 ms 856 KB Output is correct
26 Correct 1 ms 860 KB Output is correct
27 Correct 2 ms 860 KB Output is correct
28 Correct 1 ms 648 KB Output is correct
29 Correct 2 ms 1116 KB Output is correct
30 Correct 2 ms 1116 KB Output is correct
31 Correct 2 ms 1116 KB Output is correct
32 Correct 1 ms 928 KB Output is correct
33 Correct 3 ms 1012 KB Output is correct
34 Correct 12 ms 6232 KB Output is correct
35 Correct 125 ms 42584 KB Output is correct
36 Correct 147 ms 42580 KB Output is correct
37 Correct 120 ms 42432 KB Output is correct
38 Correct 122 ms 41924 KB Output is correct
39 Correct 126 ms 41808 KB Output is correct
40 Correct 115 ms 38712 KB Output is correct
41 Correct 134 ms 42652 KB Output is correct
42 Correct 139 ms 42580 KB Output is correct
43 Correct 166 ms 49632 KB Output is correct
44 Correct 143 ms 43748 KB Output is correct
45 Correct 180 ms 62292 KB Output is correct
46 Correct 123 ms 42472 KB Output is correct
47 Correct 156 ms 42668 KB Output is correct
48 Correct 167 ms 49296 KB Output is correct
49 Correct 115 ms 59216 KB Output is correct
50 Correct 110 ms 45652 KB Output is correct
51 Correct 150 ms 54100 KB Output is correct
52 Correct 209 ms 66160 KB Output is correct
53 Correct 248 ms 70224 KB Output is correct
54 Correct 262 ms 80212 KB Output is correct
55 Correct 124 ms 49160 KB Output is correct
56 Correct 249 ms 73344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 95 ms 33108 KB Output is correct
10 Correct 132 ms 40272 KB Output is correct
11 Correct 117 ms 39644 KB Output is correct
12 Correct 128 ms 42000 KB Output is correct
13 Correct 173 ms 47408 KB Output is correct
14 Correct 113 ms 33356 KB Output is correct
15 Correct 226 ms 43364 KB Output is correct
16 Correct 237 ms 42308 KB Output is correct
17 Correct 284 ms 45032 KB Output is correct
18 Correct 326 ms 50512 KB Output is correct
19 Correct 309 ms 52904 KB Output is correct
20 Correct 307 ms 59428 KB Output is correct
21 Correct 289 ms 58012 KB Output is correct
22 Correct 313 ms 59252 KB Output is correct
23 Correct 311 ms 58756 KB Output is correct
24 Correct 288 ms 56504 KB Output is correct
25 Correct 285 ms 58156 KB Output is correct
26 Correct 276 ms 55884 KB Output is correct
27 Correct 1 ms 708 KB Output is correct
28 Correct 1 ms 600 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 860 KB Output is correct
34 Correct 1 ms 856 KB Output is correct
35 Correct 1 ms 860 KB Output is correct
36 Correct 12 ms 6232 KB Output is correct
37 Correct 125 ms 42584 KB Output is correct
38 Correct 147 ms 42580 KB Output is correct
39 Correct 120 ms 42432 KB Output is correct
40 Correct 122 ms 41924 KB Output is correct
41 Correct 126 ms 41808 KB Output is correct
42 Correct 115 ms 38712 KB Output is correct
43 Correct 134 ms 42652 KB Output is correct
44 Correct 139 ms 42580 KB Output is correct
45 Correct 166 ms 49632 KB Output is correct
46 Correct 143 ms 43748 KB Output is correct
47 Correct 19 ms 6236 KB Output is correct
48 Correct 237 ms 46900 KB Output is correct
49 Correct 231 ms 46896 KB Output is correct
50 Correct 228 ms 46896 KB Output is correct
51 Correct 225 ms 46672 KB Output is correct
52 Correct 227 ms 44432 KB Output is correct
53 Correct 191 ms 34628 KB Output is correct
54 Correct 267 ms 47920 KB Output is correct
55 Correct 239 ms 46872 KB Output is correct
56 Correct 350 ms 52680 KB Output is correct
57 Correct 282 ms 48824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 95 ms 33108 KB Output is correct
11 Correct 132 ms 40272 KB Output is correct
12 Correct 117 ms 39644 KB Output is correct
13 Correct 128 ms 42000 KB Output is correct
14 Correct 173 ms 47408 KB Output is correct
15 Correct 113 ms 33356 KB Output is correct
16 Correct 226 ms 43364 KB Output is correct
17 Correct 237 ms 42308 KB Output is correct
18 Correct 284 ms 45032 KB Output is correct
19 Correct 326 ms 50512 KB Output is correct
20 Correct 309 ms 52904 KB Output is correct
21 Correct 307 ms 59428 KB Output is correct
22 Correct 289 ms 58012 KB Output is correct
23 Correct 313 ms 59252 KB Output is correct
24 Correct 311 ms 58756 KB Output is correct
25 Correct 288 ms 56504 KB Output is correct
26 Correct 285 ms 58156 KB Output is correct
27 Correct 276 ms 55884 KB Output is correct
28 Correct 1 ms 708 KB Output is correct
29 Correct 1 ms 600 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 1 ms 860 KB Output is correct
35 Correct 1 ms 856 KB Output is correct
36 Correct 1 ms 860 KB Output is correct
37 Correct 12 ms 6232 KB Output is correct
38 Correct 125 ms 42584 KB Output is correct
39 Correct 147 ms 42580 KB Output is correct
40 Correct 120 ms 42432 KB Output is correct
41 Correct 122 ms 41924 KB Output is correct
42 Correct 126 ms 41808 KB Output is correct
43 Correct 115 ms 38712 KB Output is correct
44 Correct 134 ms 42652 KB Output is correct
45 Correct 139 ms 42580 KB Output is correct
46 Correct 166 ms 49632 KB Output is correct
47 Correct 143 ms 43748 KB Output is correct
48 Correct 19 ms 6236 KB Output is correct
49 Correct 237 ms 46900 KB Output is correct
50 Correct 231 ms 46896 KB Output is correct
51 Correct 228 ms 46896 KB Output is correct
52 Correct 225 ms 46672 KB Output is correct
53 Correct 227 ms 44432 KB Output is correct
54 Correct 191 ms 34628 KB Output is correct
55 Correct 267 ms 47920 KB Output is correct
56 Correct 239 ms 46872 KB Output is correct
57 Correct 350 ms 52680 KB Output is correct
58 Correct 282 ms 48824 KB Output is correct
59 Correct 72 ms 11344 KB Output is correct
60 Correct 231 ms 44476 KB Output is correct
61 Correct 286 ms 43168 KB Output is correct
62 Correct 252 ms 46032 KB Output is correct
63 Correct 402 ms 51400 KB Output is correct
64 Correct 1 ms 792 KB Output is correct
65 Correct 1 ms 856 KB Output is correct
66 Correct 1 ms 860 KB Output is correct
67 Correct 2 ms 860 KB Output is correct
68 Correct 1 ms 648 KB Output is correct
69 Correct 2 ms 1116 KB Output is correct
70 Correct 2 ms 1116 KB Output is correct
71 Correct 2 ms 1116 KB Output is correct
72 Correct 1 ms 928 KB Output is correct
73 Correct 3 ms 1012 KB Output is correct
74 Correct 180 ms 62292 KB Output is correct
75 Correct 123 ms 42472 KB Output is correct
76 Correct 156 ms 42668 KB Output is correct
77 Correct 167 ms 49296 KB Output is correct
78 Correct 115 ms 59216 KB Output is correct
79 Correct 110 ms 45652 KB Output is correct
80 Correct 150 ms 54100 KB Output is correct
81 Correct 209 ms 66160 KB Output is correct
82 Correct 248 ms 70224 KB Output is correct
83 Correct 262 ms 80212 KB Output is correct
84 Correct 124 ms 49160 KB Output is correct
85 Correct 249 ms 73344 KB Output is correct
86 Correct 80 ms 22608 KB Output is correct
87 Correct 292 ms 47104 KB Output is correct
88 Correct 276 ms 46896 KB Output is correct
89 Correct 350 ms 51524 KB Output is correct
90 Correct 211 ms 64596 KB Output is correct
91 Correct 214 ms 59868 KB Output is correct
92 Correct 332 ms 56272 KB Output is correct
93 Correct 354 ms 69308 KB Output is correct
94 Correct 450 ms 74100 KB Output is correct
95 Correct 381 ms 84560 KB Output is correct
96 Correct 390 ms 53172 KB Output is correct
97 Correct 392 ms 64388 KB Output is correct