Submission #535991

# Submission time Handle Problem Language Result Execution time Memory
535991 2022-03-12T03:28:10 Z dutinmeow Swapping Cities (APIO20_swap) C++17
100 / 100
359 ms 45528 KB
#include <bits/stdc++.h>
using namespace std;

//#include "swap.h"

const int LOGN = 19;

vector<int> dep, val;
vector<bool> lin;
vector<array<int, LOGN>> anc;

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
	//auto start = chrono::high_resolution_clock::now();

	vector<tuple<int, int, int>> E(M);
	for (int i = 0; i < M; i++) 
		E[i] = {U[i], V[i], W[i]};
	sort(E.begin(), E.end(), 
		[](const auto &a, const auto &b) {
			return get<2>(a) < get<2>(b);
		}
	);

	struct node { 
		int par, val, com; bool lin;
		node() = default;
		node(int _par, int _val, int _com, bool _lin) : par(_par), val(_val), com(_com), lin(_lin) {}
	};

	vector<node> T; 
	T.reserve(N + M);
	vector<bool> ep(N);

	for (int i = 0; i < N; i++) {
		T.emplace_back(i, 0, i, true);
		ep[i] = true;
	}

	const auto find_com = [&](const auto &self, int u) -> int {
		if (u == T[u].com)
			return u;
		return T[u].com = self(self, T[u].com);
	};

	for (auto [u, v, w] : E) {
		int up = find_com(find_com, u), vp = find_com(find_com, v);

		int c = T.size();
		if (up != vp) {
			if (T[up].lin && T[vp].lin && ep[u] && ep[v]) {
				T[up].par = T[vp].par = c;
				T[up].com = T[vp].com = c;
				T.emplace_back(c, w, c, true);
				if (up != u)
					ep[u] = false;
				if (vp != v)
					ep[v] = false;
			} else {
				T[up].par = T[vp].par = c;
				T[up].com = T[vp].com = c;
				T.emplace_back(c, w, c, false);
			}
		} else if (T[up].lin) {
			T[up].par = c;
			T[up].com = c;
			T.emplace_back(c, w, c, false);
		}

		/*
		cout << "edge " << u << ' ' << v << ' ' << w << '\n';
		for (int i = 0; i < T.size(); i++) 
			cout << i << " : " << T[i].par << ' ' << T[i].val << ' ' << (T[i].lin ? "line" : "not line") << '\n';
		cout << '\n';
		*/
	}

	//auto stop1 = chrono::high_resolution_clock::now();

	int S = T.size();
	anc.resize(S);
	dep.resize(S);
	val.resize(S);
	lin.resize(S);
	vector<vector<int>> tree(S);
	for (int i = 0; i < S; i++) {
		anc[i][0] = T[i].par;
		val[i] = T[i].val;
		lin[i] = T[i].lin;
		if (i != T[i].par)
			tree[T[i].par].push_back(i);
	}
	for (int k = 1; k < LOGN; k++)
		for (int i = 0; i < S; i++)
			anc[i][k] = anc[anc[i][k - 1]][k - 1];

	const auto dfs = [&](const auto &self, int u) -> void {
		for (int v : tree[u]) {
			dep[v] = dep[u] + 1;
			self(self, v);
		}
	};

	for (int i = 0; i < S; i++) 
		if (i == T[i].par) {
			dep[i] = 0;
			dfs(dfs, i);
		}

	//auto stop2 = chrono::high_resolution_clock::now();

	//cout << "duration of 1 = " << duration_cast<chrono::microseconds>(stop1 - start).count() << '\n';
	//cout << "duration of 2 = " << duration_cast<chrono::microseconds>(stop2 - stop1).count() << '\n';
	//cout << "jumps = " << jumps << '\n';
}

int getMinimumFuelCapacity(int u, int v) {
	if (dep[u] > dep[v])
		swap(u, v);
	for (int k = LOGN - 1; k >= 0; k--)
		if ((dep[v] - dep[u]) >> k & 1)
			v = anc[v][k];
	//assert(dep[u] == dep[v]);
	if (u != v) {
		for (int k = LOGN - 1; k >= 0; k--)
			if (anc[u][k] != anc[v][k])
				u = anc[u][k], v = anc[v][k];
		u = anc[u][0], v = anc[v][0];
		if (u != v)
			return -1;
	}

	if (!lin[u]) 
		return val[u];
	for (int k = LOGN - 1; k >= 0; k--)
		if (lin[anc[u][k]])
			u = anc[u][k];
	u = anc[u][0];
	if (lin[u]) {
		return -1;
	} else {
		return val[u];
	}
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 428 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 564 KB Output is correct
9 Correct 90 ms 24632 KB Output is correct
10 Correct 128 ms 30036 KB Output is correct
11 Correct 123 ms 29608 KB Output is correct
12 Correct 117 ms 31320 KB Output is correct
13 Correct 120 ms 32520 KB Output is correct
14 Correct 102 ms 24728 KB Output is correct
15 Correct 244 ms 31988 KB Output is correct
16 Correct 255 ms 30940 KB Output is correct
17 Correct 243 ms 32804 KB Output is correct
18 Correct 359 ms 34088 KB Output is correct
19 Correct 84 ms 7044 KB Output is correct
20 Correct 244 ms 31896 KB Output is correct
21 Correct 222 ms 30648 KB Output is correct
22 Correct 275 ms 32852 KB Output is correct
23 Correct 331 ms 34184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 287 ms 33608 KB Output is correct
4 Correct 286 ms 35308 KB Output is correct
5 Correct 292 ms 34116 KB Output is correct
6 Correct 273 ms 35216 KB Output is correct
7 Correct 311 ms 34636 KB Output is correct
8 Correct 308 ms 33252 KB Output is correct
9 Correct 321 ms 34480 KB Output is correct
10 Correct 310 ms 32888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 428 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 564 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 560 KB Output is correct
15 Correct 1 ms 596 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 1 ms 560 KB Output is correct
18 Correct 1 ms 564 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 576 KB Output is correct
21 Correct 1 ms 596 KB Output is correct
22 Correct 1 ms 444 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 2 ms 568 KB Output is correct
25 Correct 2 ms 588 KB Output is correct
26 Correct 2 ms 596 KB Output is correct
27 Correct 1 ms 596 KB Output is correct
28 Correct 2 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 428 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 1 ms 564 KB Output is correct
10 Correct 90 ms 24632 KB Output is correct
11 Correct 128 ms 30036 KB Output is correct
12 Correct 123 ms 29608 KB Output is correct
13 Correct 117 ms 31320 KB Output is correct
14 Correct 120 ms 32520 KB Output is correct
15 Correct 1 ms 596 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 1 ms 596 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 560 KB Output is correct
20 Correct 1 ms 596 KB Output is correct
21 Correct 1 ms 596 KB Output is correct
22 Correct 1 ms 560 KB Output is correct
23 Correct 1 ms 564 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 576 KB Output is correct
26 Correct 1 ms 596 KB Output is correct
27 Correct 1 ms 444 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 2 ms 568 KB Output is correct
30 Correct 2 ms 588 KB Output is correct
31 Correct 2 ms 596 KB Output is correct
32 Correct 1 ms 596 KB Output is correct
33 Correct 2 ms 596 KB Output is correct
34 Correct 11 ms 4564 KB Output is correct
35 Correct 114 ms 31244 KB Output is correct
36 Correct 122 ms 31248 KB Output is correct
37 Correct 132 ms 31412 KB Output is correct
38 Correct 129 ms 30884 KB Output is correct
39 Correct 121 ms 30656 KB Output is correct
40 Correct 111 ms 28152 KB Output is correct
41 Correct 118 ms 31304 KB Output is correct
42 Correct 114 ms 31296 KB Output is correct
43 Correct 114 ms 32996 KB Output is correct
44 Correct 130 ms 31316 KB Output is correct
45 Correct 118 ms 27688 KB Output is correct
46 Correct 121 ms 33176 KB Output is correct
47 Correct 125 ms 32968 KB Output is correct
48 Correct 133 ms 33560 KB Output is correct
49 Correct 59 ms 8928 KB Output is correct
50 Correct 48 ms 8908 KB Output is correct
51 Correct 103 ms 25424 KB Output is correct
52 Correct 160 ms 36812 KB Output is correct
53 Correct 179 ms 43224 KB Output is correct
54 Correct 163 ms 38732 KB Output is correct
55 Correct 126 ms 34600 KB Output is correct
56 Correct 168 ms 38128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 428 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 564 KB Output is correct
9 Correct 90 ms 24632 KB Output is correct
10 Correct 128 ms 30036 KB Output is correct
11 Correct 123 ms 29608 KB Output is correct
12 Correct 117 ms 31320 KB Output is correct
13 Correct 120 ms 32520 KB Output is correct
14 Correct 102 ms 24728 KB Output is correct
15 Correct 244 ms 31988 KB Output is correct
16 Correct 255 ms 30940 KB Output is correct
17 Correct 243 ms 32804 KB Output is correct
18 Correct 359 ms 34088 KB Output is correct
19 Correct 287 ms 33608 KB Output is correct
20 Correct 286 ms 35308 KB Output is correct
21 Correct 292 ms 34116 KB Output is correct
22 Correct 273 ms 35216 KB Output is correct
23 Correct 311 ms 34636 KB Output is correct
24 Correct 308 ms 33252 KB Output is correct
25 Correct 321 ms 34480 KB Output is correct
26 Correct 310 ms 32888 KB Output is correct
27 Correct 1 ms 596 KB Output is correct
28 Correct 1 ms 596 KB Output is correct
29 Correct 1 ms 596 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 1 ms 560 KB Output is correct
32 Correct 1 ms 596 KB Output is correct
33 Correct 1 ms 596 KB Output is correct
34 Correct 1 ms 560 KB Output is correct
35 Correct 1 ms 564 KB Output is correct
36 Correct 11 ms 4564 KB Output is correct
37 Correct 114 ms 31244 KB Output is correct
38 Correct 122 ms 31248 KB Output is correct
39 Correct 132 ms 31412 KB Output is correct
40 Correct 129 ms 30884 KB Output is correct
41 Correct 121 ms 30656 KB Output is correct
42 Correct 111 ms 28152 KB Output is correct
43 Correct 118 ms 31304 KB Output is correct
44 Correct 114 ms 31296 KB Output is correct
45 Correct 114 ms 32996 KB Output is correct
46 Correct 130 ms 31316 KB Output is correct
47 Correct 18 ms 4308 KB Output is correct
48 Correct 239 ms 32780 KB Output is correct
49 Correct 228 ms 32936 KB Output is correct
50 Correct 239 ms 32844 KB Output is correct
51 Correct 219 ms 32532 KB Output is correct
52 Correct 218 ms 30808 KB Output is correct
53 Correct 177 ms 22420 KB Output is correct
54 Correct 239 ms 32876 KB Output is correct
55 Correct 263 ms 32900 KB Output is correct
56 Correct 329 ms 34188 KB Output is correct
57 Correct 233 ms 32892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 428 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 1 ms 564 KB Output is correct
10 Correct 90 ms 24632 KB Output is correct
11 Correct 128 ms 30036 KB Output is correct
12 Correct 123 ms 29608 KB Output is correct
13 Correct 117 ms 31320 KB Output is correct
14 Correct 120 ms 32520 KB Output is correct
15 Correct 102 ms 24728 KB Output is correct
16 Correct 244 ms 31988 KB Output is correct
17 Correct 255 ms 30940 KB Output is correct
18 Correct 243 ms 32804 KB Output is correct
19 Correct 359 ms 34088 KB Output is correct
20 Correct 287 ms 33608 KB Output is correct
21 Correct 286 ms 35308 KB Output is correct
22 Correct 292 ms 34116 KB Output is correct
23 Correct 273 ms 35216 KB Output is correct
24 Correct 311 ms 34636 KB Output is correct
25 Correct 308 ms 33252 KB Output is correct
26 Correct 321 ms 34480 KB Output is correct
27 Correct 310 ms 32888 KB Output is correct
28 Correct 1 ms 596 KB Output is correct
29 Correct 1 ms 596 KB Output is correct
30 Correct 1 ms 596 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 560 KB Output is correct
33 Correct 1 ms 596 KB Output is correct
34 Correct 1 ms 596 KB Output is correct
35 Correct 1 ms 560 KB Output is correct
36 Correct 1 ms 564 KB Output is correct
37 Correct 11 ms 4564 KB Output is correct
38 Correct 114 ms 31244 KB Output is correct
39 Correct 122 ms 31248 KB Output is correct
40 Correct 132 ms 31412 KB Output is correct
41 Correct 129 ms 30884 KB Output is correct
42 Correct 121 ms 30656 KB Output is correct
43 Correct 111 ms 28152 KB Output is correct
44 Correct 118 ms 31304 KB Output is correct
45 Correct 114 ms 31296 KB Output is correct
46 Correct 114 ms 32996 KB Output is correct
47 Correct 130 ms 31316 KB Output is correct
48 Correct 18 ms 4308 KB Output is correct
49 Correct 239 ms 32780 KB Output is correct
50 Correct 228 ms 32936 KB Output is correct
51 Correct 239 ms 32844 KB Output is correct
52 Correct 219 ms 32532 KB Output is correct
53 Correct 218 ms 30808 KB Output is correct
54 Correct 177 ms 22420 KB Output is correct
55 Correct 239 ms 32876 KB Output is correct
56 Correct 263 ms 32900 KB Output is correct
57 Correct 329 ms 34188 KB Output is correct
58 Correct 233 ms 32892 KB Output is correct
59 Correct 84 ms 7044 KB Output is correct
60 Correct 244 ms 31896 KB Output is correct
61 Correct 222 ms 30648 KB Output is correct
62 Correct 275 ms 32852 KB Output is correct
63 Correct 331 ms 34184 KB Output is correct
64 Correct 1 ms 468 KB Output is correct
65 Correct 1 ms 576 KB Output is correct
66 Correct 1 ms 596 KB Output is correct
67 Correct 1 ms 444 KB Output is correct
68 Correct 1 ms 468 KB Output is correct
69 Correct 2 ms 568 KB Output is correct
70 Correct 2 ms 588 KB Output is correct
71 Correct 2 ms 596 KB Output is correct
72 Correct 1 ms 596 KB Output is correct
73 Correct 2 ms 596 KB Output is correct
74 Correct 118 ms 27688 KB Output is correct
75 Correct 121 ms 33176 KB Output is correct
76 Correct 125 ms 32968 KB Output is correct
77 Correct 133 ms 33560 KB Output is correct
78 Correct 59 ms 8928 KB Output is correct
79 Correct 48 ms 8908 KB Output is correct
80 Correct 103 ms 25424 KB Output is correct
81 Correct 160 ms 36812 KB Output is correct
82 Correct 179 ms 43224 KB Output is correct
83 Correct 163 ms 38732 KB Output is correct
84 Correct 126 ms 34600 KB Output is correct
85 Correct 168 ms 38128 KB Output is correct
86 Correct 61 ms 12720 KB Output is correct
87 Correct 258 ms 36964 KB Output is correct
88 Correct 221 ms 36936 KB Output is correct
89 Correct 315 ms 35404 KB Output is correct
90 Correct 143 ms 13004 KB Output is correct
91 Correct 163 ms 15092 KB Output is correct
92 Correct 215 ms 29236 KB Output is correct
93 Correct 272 ms 40524 KB Output is correct
94 Correct 301 ms 45528 KB Output is correct
95 Correct 301 ms 42596 KB Output is correct
96 Correct 324 ms 38324 KB Output is correct
97 Correct 279 ms 39884 KB Output is correct