Submission #535983

# Submission time Handle Problem Language Result Execution time Memory
535983 2022-03-12T03:07:42 Z dutinmeow Swapping Cities (APIO20_swap) C++17
53 / 100
2000 ms 34832 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) {
	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; bool lin;
		node() = default;
		node(int _par, int _val, bool _lin) : par(_par), val(_val), lin(_lin) {}
	};

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

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

	for (auto [u, v, w] : E) {
		while (rt[u] != T[rt[u]].par)
			rt[u] = T[rt[u]].par;
		int up = rt[u];
		while (rt[v] != T[rt[v]].par)
			rt[v] = T[rt[v]].par;
		int vp = rt[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.emplace_back(c, w, true);
				if (up != u)
					ep[u] = false;
				if (vp != v)
					ep[v] = false;
			} else {
				T[up].par = T[vp].par = T.size();
				T.emplace_back(c, w, false);
			}
		} else if (T[up].lin) {
			T[up].par = c;
			T.emplace_back(c, w, 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';
		*/
	}

	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);
		}
}

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 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 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 101 ms 24264 KB Output is correct
10 Correct 140 ms 29652 KB Output is correct
11 Correct 116 ms 29348 KB Output is correct
12 Correct 141 ms 30860 KB Output is correct
13 Correct 123 ms 32168 KB Output is correct
14 Correct 99 ms 24292 KB Output is correct
15 Correct 271 ms 31636 KB Output is correct
16 Correct 235 ms 30520 KB Output is correct
17 Correct 289 ms 32332 KB Output is correct
18 Correct 352 ms 33712 KB Output is correct
19 Correct 89 ms 7092 KB Output is correct
20 Correct 232 ms 31608 KB Output is correct
21 Correct 280 ms 30344 KB Output is correct
22 Correct 251 ms 32348 KB Output is correct
23 Correct 384 ms 33612 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 305 ms 33188 KB Output is correct
4 Correct 286 ms 34784 KB Output is correct
5 Correct 342 ms 33660 KB Output is correct
6 Correct 267 ms 34832 KB Output is correct
7 Correct 343 ms 34216 KB Output is correct
8 Correct 291 ms 32860 KB Output is correct
9 Correct 324 ms 34032 KB Output is correct
10 Correct 280 ms 32484 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 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 0 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 468 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 596 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 596 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 2 ms 596 KB Output is correct
25 Correct 2 ms 596 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 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 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 1 ms 596 KB Output is correct
10 Correct 101 ms 24264 KB Output is correct
11 Correct 140 ms 29652 KB Output is correct
12 Correct 116 ms 29348 KB Output is correct
13 Correct 141 ms 30860 KB Output is correct
14 Correct 123 ms 32168 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 468 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 596 KB Output is correct
23 Correct 1 ms 596 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 596 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 2 ms 596 KB Output is correct
30 Correct 2 ms 596 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 12 ms 4436 KB Output is correct
35 Correct 124 ms 30752 KB Output is correct
36 Correct 125 ms 30856 KB Output is correct
37 Correct 132 ms 30836 KB Output is correct
38 Correct 113 ms 30312 KB Output is correct
39 Correct 123 ms 30224 KB Output is correct
40 Correct 119 ms 27860 KB Output is correct
41 Correct 130 ms 30832 KB Output is correct
42 Correct 120 ms 30824 KB Output is correct
43 Correct 136 ms 32572 KB Output is correct
44 Correct 130 ms 30796 KB Output is correct
45 Execution timed out 2074 ms 7752 KB Time limit exceeded
46 Halted 0 ms 0 KB -
# 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 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 101 ms 24264 KB Output is correct
10 Correct 140 ms 29652 KB Output is correct
11 Correct 116 ms 29348 KB Output is correct
12 Correct 141 ms 30860 KB Output is correct
13 Correct 123 ms 32168 KB Output is correct
14 Correct 99 ms 24292 KB Output is correct
15 Correct 271 ms 31636 KB Output is correct
16 Correct 235 ms 30520 KB Output is correct
17 Correct 289 ms 32332 KB Output is correct
18 Correct 352 ms 33712 KB Output is correct
19 Correct 305 ms 33188 KB Output is correct
20 Correct 286 ms 34784 KB Output is correct
21 Correct 342 ms 33660 KB Output is correct
22 Correct 267 ms 34832 KB Output is correct
23 Correct 343 ms 34216 KB Output is correct
24 Correct 291 ms 32860 KB Output is correct
25 Correct 324 ms 34032 KB Output is correct
26 Correct 280 ms 32484 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 468 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 596 KB Output is correct
35 Correct 1 ms 596 KB Output is correct
36 Correct 12 ms 4436 KB Output is correct
37 Correct 124 ms 30752 KB Output is correct
38 Correct 125 ms 30856 KB Output is correct
39 Correct 132 ms 30836 KB Output is correct
40 Correct 113 ms 30312 KB Output is correct
41 Correct 123 ms 30224 KB Output is correct
42 Correct 119 ms 27860 KB Output is correct
43 Correct 130 ms 30832 KB Output is correct
44 Correct 120 ms 30824 KB Output is correct
45 Correct 136 ms 32572 KB Output is correct
46 Correct 130 ms 30796 KB Output is correct
47 Correct 20 ms 4180 KB Output is correct
48 Correct 240 ms 32328 KB Output is correct
49 Correct 228 ms 32320 KB Output is correct
50 Correct 259 ms 32352 KB Output is correct
51 Correct 219 ms 32124 KB Output is correct
52 Correct 272 ms 30336 KB Output is correct
53 Correct 196 ms 22100 KB Output is correct
54 Correct 282 ms 32432 KB Output is correct
55 Correct 267 ms 32352 KB Output is correct
56 Correct 305 ms 33760 KB Output is correct
57 Correct 281 ms 32436 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 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 1 ms 596 KB Output is correct
10 Correct 101 ms 24264 KB Output is correct
11 Correct 140 ms 29652 KB Output is correct
12 Correct 116 ms 29348 KB Output is correct
13 Correct 141 ms 30860 KB Output is correct
14 Correct 123 ms 32168 KB Output is correct
15 Correct 99 ms 24292 KB Output is correct
16 Correct 271 ms 31636 KB Output is correct
17 Correct 235 ms 30520 KB Output is correct
18 Correct 289 ms 32332 KB Output is correct
19 Correct 352 ms 33712 KB Output is correct
20 Correct 305 ms 33188 KB Output is correct
21 Correct 286 ms 34784 KB Output is correct
22 Correct 342 ms 33660 KB Output is correct
23 Correct 267 ms 34832 KB Output is correct
24 Correct 343 ms 34216 KB Output is correct
25 Correct 291 ms 32860 KB Output is correct
26 Correct 324 ms 34032 KB Output is correct
27 Correct 280 ms 32484 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 468 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 596 KB Output is correct
36 Correct 1 ms 596 KB Output is correct
37 Correct 12 ms 4436 KB Output is correct
38 Correct 124 ms 30752 KB Output is correct
39 Correct 125 ms 30856 KB Output is correct
40 Correct 132 ms 30836 KB Output is correct
41 Correct 113 ms 30312 KB Output is correct
42 Correct 123 ms 30224 KB Output is correct
43 Correct 119 ms 27860 KB Output is correct
44 Correct 130 ms 30832 KB Output is correct
45 Correct 120 ms 30824 KB Output is correct
46 Correct 136 ms 32572 KB Output is correct
47 Correct 130 ms 30796 KB Output is correct
48 Correct 20 ms 4180 KB Output is correct
49 Correct 240 ms 32328 KB Output is correct
50 Correct 228 ms 32320 KB Output is correct
51 Correct 259 ms 32352 KB Output is correct
52 Correct 219 ms 32124 KB Output is correct
53 Correct 272 ms 30336 KB Output is correct
54 Correct 196 ms 22100 KB Output is correct
55 Correct 282 ms 32432 KB Output is correct
56 Correct 267 ms 32352 KB Output is correct
57 Correct 305 ms 33760 KB Output is correct
58 Correct 281 ms 32436 KB Output is correct
59 Correct 89 ms 7092 KB Output is correct
60 Correct 232 ms 31608 KB Output is correct
61 Correct 280 ms 30344 KB Output is correct
62 Correct 251 ms 32348 KB Output is correct
63 Correct 384 ms 33612 KB Output is correct
64 Correct 1 ms 468 KB Output is correct
65 Correct 1 ms 596 KB Output is correct
66 Correct 1 ms 468 KB Output is correct
67 Correct 1 ms 340 KB Output is correct
68 Correct 1 ms 468 KB Output is correct
69 Correct 2 ms 596 KB Output is correct
70 Correct 2 ms 596 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 Execution timed out 2074 ms 7752 KB Time limit exceeded
75 Halted 0 ms 0 KB -