Submission #535973

# Submission time Handle Problem Language Result Execution time Memory
535973 2022-03-12T02:46:41 Z dutinmeow Swapping Cities (APIO20_swap) C++17
53 / 100
2000 ms 37936 KB
#include <bits/stdc++.h>
using namespace std;

//#include "swap.h"

const int LOGN = 17;

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, siz; bool lin;
		node() = default;
		node(int _par, int _val, int _siz, bool _lin) : par(_par), val(_val), siz(_siz), 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, 1, 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, T[up].siz + T[vp].siz, true);
				if (T[up].siz > 1)
					ep[u] = false;
				if (T[vp].siz > 1)
					ep[v] = false;
			} else {
				T[up].par = T[vp].par = T.size();
				T.emplace_back(c, w, T[up].siz + T[vp].siz, false);
			}
		} else if (T[up].lin) {
			T[up].par = c;
			T.emplace_back(c, w, T[up].siz + T[vp].siz, 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 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 340 KB Output is correct
5 Correct 1 ms 584 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 580 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 86 ms 25268 KB Output is correct
10 Correct 126 ms 30924 KB Output is correct
11 Correct 104 ms 30436 KB Output is correct
12 Correct 112 ms 32168 KB Output is correct
13 Correct 112 ms 33404 KB Output is correct
14 Correct 121 ms 25516 KB Output is correct
15 Correct 220 ms 35016 KB Output is correct
16 Correct 269 ms 34312 KB Output is correct
17 Correct 231 ms 36028 KB Output is correct
18 Correct 346 ms 37288 KB Output is correct
19 Correct 76 ms 8772 KB Output is correct
20 Correct 208 ms 34960 KB Output is correct
21 Correct 245 ms 33848 KB Output is correct
22 Correct 221 ms 36020 KB Output is correct
23 Correct 330 ms 37364 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 297 ms 36224 KB Output is correct
4 Correct 262 ms 37936 KB Output is correct
5 Correct 334 ms 36896 KB Output is correct
6 Correct 265 ms 37672 KB Output is correct
7 Correct 291 ms 37400 KB Output is correct
8 Correct 250 ms 35936 KB Output is correct
9 Correct 257 ms 37104 KB Output is correct
10 Correct 267 ms 35664 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 340 KB Output is correct
5 Correct 1 ms 584 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 580 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 572 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 596 KB Output is correct
22 Correct 2 ms 444 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 2 ms 596 KB Output is correct
25 Correct 3 ms 692 KB Output is correct
26 Correct 2 ms 596 KB Output is correct
27 Correct 1 ms 568 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 340 KB Output is correct
6 Correct 1 ms 584 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 580 KB Output is correct
9 Correct 1 ms 596 KB Output is correct
10 Correct 86 ms 25268 KB Output is correct
11 Correct 126 ms 30924 KB Output is correct
12 Correct 104 ms 30436 KB Output is correct
13 Correct 112 ms 32168 KB Output is correct
14 Correct 112 ms 33404 KB Output is correct
15 Correct 1 ms 572 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 596 KB Output is correct
27 Correct 2 ms 444 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 2 ms 596 KB Output is correct
30 Correct 3 ms 692 KB Output is correct
31 Correct 2 ms 596 KB Output is correct
32 Correct 1 ms 568 KB Output is correct
33 Correct 2 ms 596 KB Output is correct
34 Correct 10 ms 4564 KB Output is correct
35 Correct 109 ms 31820 KB Output is correct
36 Correct 115 ms 31908 KB Output is correct
37 Correct 111 ms 31884 KB Output is correct
38 Correct 110 ms 31392 KB Output is correct
39 Correct 127 ms 31284 KB Output is correct
40 Correct 99 ms 28604 KB Output is correct
41 Correct 116 ms 32180 KB Output is correct
42 Correct 107 ms 31796 KB Output is correct
43 Correct 100 ms 33556 KB Output is correct
44 Correct 132 ms 32204 KB Output is correct
45 Execution timed out 2072 ms 11324 KB Time limit exceeded
46 Halted 0 ms 0 KB -
# 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 340 KB Output is correct
5 Correct 1 ms 584 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 580 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 86 ms 25268 KB Output is correct
10 Correct 126 ms 30924 KB Output is correct
11 Correct 104 ms 30436 KB Output is correct
12 Correct 112 ms 32168 KB Output is correct
13 Correct 112 ms 33404 KB Output is correct
14 Correct 121 ms 25516 KB Output is correct
15 Correct 220 ms 35016 KB Output is correct
16 Correct 269 ms 34312 KB Output is correct
17 Correct 231 ms 36028 KB Output is correct
18 Correct 346 ms 37288 KB Output is correct
19 Correct 297 ms 36224 KB Output is correct
20 Correct 262 ms 37936 KB Output is correct
21 Correct 334 ms 36896 KB Output is correct
22 Correct 265 ms 37672 KB Output is correct
23 Correct 291 ms 37400 KB Output is correct
24 Correct 250 ms 35936 KB Output is correct
25 Correct 257 ms 37104 KB Output is correct
26 Correct 267 ms 35664 KB Output is correct
27 Correct 1 ms 572 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 10 ms 4564 KB Output is correct
37 Correct 109 ms 31820 KB Output is correct
38 Correct 115 ms 31908 KB Output is correct
39 Correct 111 ms 31884 KB Output is correct
40 Correct 110 ms 31392 KB Output is correct
41 Correct 127 ms 31284 KB Output is correct
42 Correct 99 ms 28604 KB Output is correct
43 Correct 116 ms 32180 KB Output is correct
44 Correct 107 ms 31796 KB Output is correct
45 Correct 100 ms 33556 KB Output is correct
46 Correct 132 ms 32204 KB Output is correct
47 Correct 18 ms 4544 KB Output is correct
48 Correct 212 ms 35768 KB Output is correct
49 Correct 232 ms 35760 KB Output is correct
50 Correct 214 ms 35624 KB Output is correct
51 Correct 236 ms 35488 KB Output is correct
52 Correct 197 ms 33632 KB Output is correct
53 Correct 191 ms 25164 KB Output is correct
54 Correct 215 ms 36020 KB Output is correct
55 Correct 243 ms 35660 KB Output is correct
56 Correct 275 ms 37112 KB Output is correct
57 Correct 235 ms 36020 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 340 KB Output is correct
6 Correct 1 ms 584 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 580 KB Output is correct
9 Correct 1 ms 596 KB Output is correct
10 Correct 86 ms 25268 KB Output is correct
11 Correct 126 ms 30924 KB Output is correct
12 Correct 104 ms 30436 KB Output is correct
13 Correct 112 ms 32168 KB Output is correct
14 Correct 112 ms 33404 KB Output is correct
15 Correct 121 ms 25516 KB Output is correct
16 Correct 220 ms 35016 KB Output is correct
17 Correct 269 ms 34312 KB Output is correct
18 Correct 231 ms 36028 KB Output is correct
19 Correct 346 ms 37288 KB Output is correct
20 Correct 297 ms 36224 KB Output is correct
21 Correct 262 ms 37936 KB Output is correct
22 Correct 334 ms 36896 KB Output is correct
23 Correct 265 ms 37672 KB Output is correct
24 Correct 291 ms 37400 KB Output is correct
25 Correct 250 ms 35936 KB Output is correct
26 Correct 257 ms 37104 KB Output is correct
27 Correct 267 ms 35664 KB Output is correct
28 Correct 1 ms 572 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 10 ms 4564 KB Output is correct
38 Correct 109 ms 31820 KB Output is correct
39 Correct 115 ms 31908 KB Output is correct
40 Correct 111 ms 31884 KB Output is correct
41 Correct 110 ms 31392 KB Output is correct
42 Correct 127 ms 31284 KB Output is correct
43 Correct 99 ms 28604 KB Output is correct
44 Correct 116 ms 32180 KB Output is correct
45 Correct 107 ms 31796 KB Output is correct
46 Correct 100 ms 33556 KB Output is correct
47 Correct 132 ms 32204 KB Output is correct
48 Correct 18 ms 4544 KB Output is correct
49 Correct 212 ms 35768 KB Output is correct
50 Correct 232 ms 35760 KB Output is correct
51 Correct 214 ms 35624 KB Output is correct
52 Correct 236 ms 35488 KB Output is correct
53 Correct 197 ms 33632 KB Output is correct
54 Correct 191 ms 25164 KB Output is correct
55 Correct 215 ms 36020 KB Output is correct
56 Correct 243 ms 35660 KB Output is correct
57 Correct 275 ms 37112 KB Output is correct
58 Correct 235 ms 36020 KB Output is correct
59 Correct 76 ms 8772 KB Output is correct
60 Correct 208 ms 34960 KB Output is correct
61 Correct 245 ms 33848 KB Output is correct
62 Correct 221 ms 36020 KB Output is correct
63 Correct 330 ms 37364 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 596 KB Output is correct
67 Correct 2 ms 444 KB Output is correct
68 Correct 1 ms 468 KB Output is correct
69 Correct 2 ms 596 KB Output is correct
70 Correct 3 ms 692 KB Output is correct
71 Correct 2 ms 596 KB Output is correct
72 Correct 1 ms 568 KB Output is correct
73 Correct 2 ms 596 KB Output is correct
74 Execution timed out 2072 ms 11324 KB Time limit exceeded
75 Halted 0 ms 0 KB -