Submission #444599

# Submission time Handle Problem Language Result Execution time Memory
444599 2021-07-14T10:50:45 Z nonsensenonsense1 Swapping Cities (APIO20_swap) C++17
100 / 100
537 ms 42260 KB
#include "swap.h"
#include <cstring>
#include <utility>
#include <vector>
#include <algorithm>

const int INF = 0x3f3f3f3f;

struct dsu {
	dsu *p;
	int size;
	bool over;
	std::vector<int> v;
	dsu() {
		over = false;
		p = 0;
		size = 1;
	}
	dsu *find() {
		if (p) return p = p->find();
		return this;
	}
};

const int N = 100000;
const int M = 200000;
const int LG = 17;
int in[N], out[N], f[LG][N], up[LG][N], fi[N], deg[N];
std::vector<std::pair<int, int> > g[N];
std::pair<int, std::pair<int, int> > e[M];
dsu d[N];

void update(std::vector<int> &v, int w) 
{
	while (!v.empty()) {
		fi[v.back()] = w;
		v.pop_back();
	}
}

bool unite(dsu *a, dsu *b, int w) 
{
	a = a->find();
	b = b->find();
	if (a == b) return 0;
	if (a->size < b->size) std::swap(a, b);
	b->p = a;
	a->size += b->size;
	a->v.insert(a->v.end(), b->v.begin(), b->v.end());
	if (a->over || b->over) {
		a->over = true;
		update(a->v, w);
	}
	return 1;
}

void rm(dsu *a, int w) 
{
	a = a->find();
	if (!a->over) {
		a->over = true;
		update(a->v, w);
	}
}

void dfs(int v) 
{
	static int dt = 0;
	in[v] = dt++;
	for (int i = 0; i < (int)g[v].size(); ++i) if (g[v][i].first != f[0][v]) {
		f[0][g[v][i].first] = v;
		up[0][g[v][i].first] = g[v][i].second;
		dfs(g[v][i].first);
	}
	out[v] = dt;
}

bool parent(int u, int v) 
{
	return in[u] <= in[v] && out[u] >= out[v];
}

void init(int n, int m, std::vector<int> u, std::vector<int> v, std::vector<int> w) 
{
	memset(fi, 0x3f, n << 2);
	for (int i = 0; i < n; ++i) d[i].v.push_back(i);
	for (int i = 0; i < m; ++i) e[i] = std::make_pair(w[i], std::make_pair(u[i], v[i]));
	std::sort(e, e + m);
	for (int i = 0; i < m; ++i) {
		bool way = false;
		if (unite(d + e[i].second.first, d + e[i].second.second, e[i].first)) {
			g[e[i].second.first].push_back(std::make_pair(e[i].second.second, e[i].first));
			g[e[i].second.second].push_back(std::make_pair(e[i].second.first, e[i].first));
		}
		else way = true;
		++deg[e[i].second.first];
		++deg[e[i].second.second];
		if (way || deg[e[i].second.first] == 3 || deg[e[i].second.second] == 3) rm(d + e[i].second.first, e[i].first);
	}
	dfs(0);
	for (int j = 1; j < LG; ++j) for (int i = 0; i < n; ++i) {
		f[j][i] = f[j - 1][f[j - 1][i]];
		up[j][i] = std::max(up[j - 1][i], up[j - 1][f[j - 1][i]]);
	}
}

int getMinimumFuelCapacity(int u, int v) 
{
	int edge = std::min(fi[u], fi[v]);
	for (int t = 0; t < 2; ++t) {
		if (!parent(v, u)) {
			for (int i = LG - 1; i >= 0; --i) if (!parent(f[i][v], u)) {
				edge = std::max(edge, up[i][v]);
				v = f[i][v];
			}
			edge = std::max(edge, up[0][v]);
		}
		std::swap(u, v);
	}
	if (edge == INF) return -1;
	return edge;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6732 KB Output is correct
2 Correct 4 ms 6604 KB Output is correct
3 Correct 4 ms 6620 KB Output is correct
4 Correct 4 ms 6860 KB Output is correct
5 Correct 5 ms 6988 KB Output is correct
6 Correct 5 ms 6988 KB Output is correct
7 Correct 5 ms 6988 KB Output is correct
8 Correct 5 ms 6988 KB Output is correct
9 Correct 113 ms 31628 KB Output is correct
10 Correct 133 ms 38056 KB Output is correct
11 Correct 136 ms 37112 KB Output is correct
12 Correct 156 ms 39276 KB Output is correct
13 Correct 111 ms 37852 KB Output is correct
14 Correct 135 ms 31260 KB Output is correct
15 Correct 450 ms 40144 KB Output is correct
16 Correct 479 ms 37844 KB Output is correct
17 Correct 492 ms 42260 KB Output is correct
18 Correct 403 ms 38616 KB Output is correct
19 Correct 128 ms 15552 KB Output is correct
20 Correct 454 ms 40568 KB Output is correct
21 Correct 463 ms 38952 KB Output is correct
22 Correct 461 ms 41896 KB Output is correct
23 Correct 411 ms 39584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6732 KB Output is correct
2 Correct 4 ms 6604 KB Output is correct
3 Correct 211 ms 33804 KB Output is correct
4 Correct 268 ms 34796 KB Output is correct
5 Correct 220 ms 34324 KB Output is correct
6 Correct 231 ms 34700 KB Output is correct
7 Correct 223 ms 34616 KB Output is correct
8 Correct 242 ms 33556 KB Output is correct
9 Correct 228 ms 34440 KB Output is correct
10 Correct 227 ms 33416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6732 KB Output is correct
2 Correct 4 ms 6604 KB Output is correct
3 Correct 4 ms 6620 KB Output is correct
4 Correct 4 ms 6860 KB Output is correct
5 Correct 5 ms 6988 KB Output is correct
6 Correct 5 ms 6988 KB Output is correct
7 Correct 5 ms 6988 KB Output is correct
8 Correct 5 ms 6988 KB Output is correct
9 Correct 4 ms 6732 KB Output is correct
10 Correct 5 ms 6860 KB Output is correct
11 Correct 4 ms 6988 KB Output is correct
12 Correct 5 ms 6860 KB Output is correct
13 Correct 5 ms 6860 KB Output is correct
14 Correct 5 ms 6860 KB Output is correct
15 Correct 5 ms 6896 KB Output is correct
16 Correct 5 ms 6988 KB Output is correct
17 Correct 5 ms 6988 KB Output is correct
18 Correct 5 ms 6860 KB Output is correct
19 Correct 5 ms 6912 KB Output is correct
20 Correct 5 ms 6988 KB Output is correct
21 Correct 5 ms 6860 KB Output is correct
22 Correct 4 ms 6732 KB Output is correct
23 Correct 4 ms 6860 KB Output is correct
24 Correct 5 ms 6988 KB Output is correct
25 Correct 5 ms 6988 KB Output is correct
26 Correct 5 ms 6988 KB Output is correct
27 Correct 5 ms 6988 KB Output is correct
28 Correct 5 ms 6988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6732 KB Output is correct
2 Correct 4 ms 6732 KB Output is correct
3 Correct 4 ms 6604 KB Output is correct
4 Correct 4 ms 6620 KB Output is correct
5 Correct 4 ms 6860 KB Output is correct
6 Correct 5 ms 6988 KB Output is correct
7 Correct 5 ms 6988 KB Output is correct
8 Correct 5 ms 6988 KB Output is correct
9 Correct 5 ms 6988 KB Output is correct
10 Correct 113 ms 31628 KB Output is correct
11 Correct 133 ms 38056 KB Output is correct
12 Correct 136 ms 37112 KB Output is correct
13 Correct 156 ms 39276 KB Output is correct
14 Correct 111 ms 37852 KB Output is correct
15 Correct 5 ms 6860 KB Output is correct
16 Correct 4 ms 6988 KB Output is correct
17 Correct 5 ms 6860 KB Output is correct
18 Correct 5 ms 6860 KB Output is correct
19 Correct 5 ms 6860 KB Output is correct
20 Correct 5 ms 6896 KB Output is correct
21 Correct 5 ms 6988 KB Output is correct
22 Correct 5 ms 6988 KB Output is correct
23 Correct 5 ms 6860 KB Output is correct
24 Correct 5 ms 6912 KB Output is correct
25 Correct 5 ms 6988 KB Output is correct
26 Correct 5 ms 6860 KB Output is correct
27 Correct 4 ms 6732 KB Output is correct
28 Correct 4 ms 6860 KB Output is correct
29 Correct 5 ms 6988 KB Output is correct
30 Correct 5 ms 6988 KB Output is correct
31 Correct 5 ms 6988 KB Output is correct
32 Correct 5 ms 6988 KB Output is correct
33 Correct 5 ms 6988 KB Output is correct
34 Correct 16 ms 10060 KB Output is correct
35 Correct 140 ms 38108 KB Output is correct
36 Correct 149 ms 35728 KB Output is correct
37 Correct 143 ms 33212 KB Output is correct
38 Correct 138 ms 32196 KB Output is correct
39 Correct 146 ms 31208 KB Output is correct
40 Correct 125 ms 29256 KB Output is correct
41 Correct 144 ms 37040 KB Output is correct
42 Correct 154 ms 38268 KB Output is correct
43 Correct 115 ms 35968 KB Output is correct
44 Correct 137 ms 31792 KB Output is correct
45 Correct 133 ms 28888 KB Output is correct
46 Correct 144 ms 34768 KB Output is correct
47 Correct 141 ms 32060 KB Output is correct
48 Correct 144 ms 31428 KB Output is correct
49 Correct 66 ms 12508 KB Output is correct
50 Correct 55 ms 12472 KB Output is correct
51 Correct 102 ms 25028 KB Output is correct
52 Correct 174 ms 37168 KB Output is correct
53 Correct 179 ms 35020 KB Output is correct
54 Correct 186 ms 40600 KB Output is correct
55 Correct 112 ms 37048 KB Output is correct
56 Correct 162 ms 34264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6732 KB Output is correct
2 Correct 4 ms 6604 KB Output is correct
3 Correct 4 ms 6620 KB Output is correct
4 Correct 4 ms 6860 KB Output is correct
5 Correct 5 ms 6988 KB Output is correct
6 Correct 5 ms 6988 KB Output is correct
7 Correct 5 ms 6988 KB Output is correct
8 Correct 5 ms 6988 KB Output is correct
9 Correct 113 ms 31628 KB Output is correct
10 Correct 133 ms 38056 KB Output is correct
11 Correct 136 ms 37112 KB Output is correct
12 Correct 156 ms 39276 KB Output is correct
13 Correct 111 ms 37852 KB Output is correct
14 Correct 135 ms 31260 KB Output is correct
15 Correct 450 ms 40144 KB Output is correct
16 Correct 479 ms 37844 KB Output is correct
17 Correct 492 ms 42260 KB Output is correct
18 Correct 403 ms 38616 KB Output is correct
19 Correct 211 ms 33804 KB Output is correct
20 Correct 268 ms 34796 KB Output is correct
21 Correct 220 ms 34324 KB Output is correct
22 Correct 231 ms 34700 KB Output is correct
23 Correct 223 ms 34616 KB Output is correct
24 Correct 242 ms 33556 KB Output is correct
25 Correct 228 ms 34440 KB Output is correct
26 Correct 227 ms 33416 KB Output is correct
27 Correct 5 ms 6860 KB Output is correct
28 Correct 4 ms 6988 KB Output is correct
29 Correct 5 ms 6860 KB Output is correct
30 Correct 5 ms 6860 KB Output is correct
31 Correct 5 ms 6860 KB Output is correct
32 Correct 5 ms 6896 KB Output is correct
33 Correct 5 ms 6988 KB Output is correct
34 Correct 5 ms 6988 KB Output is correct
35 Correct 5 ms 6860 KB Output is correct
36 Correct 16 ms 10060 KB Output is correct
37 Correct 140 ms 38108 KB Output is correct
38 Correct 149 ms 35728 KB Output is correct
39 Correct 143 ms 33212 KB Output is correct
40 Correct 138 ms 32196 KB Output is correct
41 Correct 146 ms 31208 KB Output is correct
42 Correct 125 ms 29256 KB Output is correct
43 Correct 144 ms 37040 KB Output is correct
44 Correct 154 ms 38268 KB Output is correct
45 Correct 115 ms 35968 KB Output is correct
46 Correct 137 ms 31792 KB Output is correct
47 Correct 27 ms 10100 KB Output is correct
48 Correct 442 ms 39548 KB Output is correct
49 Correct 442 ms 37164 KB Output is correct
50 Correct 459 ms 35884 KB Output is correct
51 Correct 475 ms 34864 KB Output is correct
52 Correct 343 ms 32924 KB Output is correct
53 Correct 258 ms 26932 KB Output is correct
54 Correct 473 ms 39408 KB Output is correct
55 Correct 440 ms 40304 KB Output is correct
56 Correct 431 ms 39000 KB Output is correct
57 Correct 357 ms 35116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6732 KB Output is correct
2 Correct 4 ms 6732 KB Output is correct
3 Correct 4 ms 6604 KB Output is correct
4 Correct 4 ms 6620 KB Output is correct
5 Correct 4 ms 6860 KB Output is correct
6 Correct 5 ms 6988 KB Output is correct
7 Correct 5 ms 6988 KB Output is correct
8 Correct 5 ms 6988 KB Output is correct
9 Correct 5 ms 6988 KB Output is correct
10 Correct 113 ms 31628 KB Output is correct
11 Correct 133 ms 38056 KB Output is correct
12 Correct 136 ms 37112 KB Output is correct
13 Correct 156 ms 39276 KB Output is correct
14 Correct 111 ms 37852 KB Output is correct
15 Correct 135 ms 31260 KB Output is correct
16 Correct 450 ms 40144 KB Output is correct
17 Correct 479 ms 37844 KB Output is correct
18 Correct 492 ms 42260 KB Output is correct
19 Correct 403 ms 38616 KB Output is correct
20 Correct 211 ms 33804 KB Output is correct
21 Correct 268 ms 34796 KB Output is correct
22 Correct 220 ms 34324 KB Output is correct
23 Correct 231 ms 34700 KB Output is correct
24 Correct 223 ms 34616 KB Output is correct
25 Correct 242 ms 33556 KB Output is correct
26 Correct 228 ms 34440 KB Output is correct
27 Correct 227 ms 33416 KB Output is correct
28 Correct 5 ms 6860 KB Output is correct
29 Correct 4 ms 6988 KB Output is correct
30 Correct 5 ms 6860 KB Output is correct
31 Correct 5 ms 6860 KB Output is correct
32 Correct 5 ms 6860 KB Output is correct
33 Correct 5 ms 6896 KB Output is correct
34 Correct 5 ms 6988 KB Output is correct
35 Correct 5 ms 6988 KB Output is correct
36 Correct 5 ms 6860 KB Output is correct
37 Correct 16 ms 10060 KB Output is correct
38 Correct 140 ms 38108 KB Output is correct
39 Correct 149 ms 35728 KB Output is correct
40 Correct 143 ms 33212 KB Output is correct
41 Correct 138 ms 32196 KB Output is correct
42 Correct 146 ms 31208 KB Output is correct
43 Correct 125 ms 29256 KB Output is correct
44 Correct 144 ms 37040 KB Output is correct
45 Correct 154 ms 38268 KB Output is correct
46 Correct 115 ms 35968 KB Output is correct
47 Correct 137 ms 31792 KB Output is correct
48 Correct 27 ms 10100 KB Output is correct
49 Correct 442 ms 39548 KB Output is correct
50 Correct 442 ms 37164 KB Output is correct
51 Correct 459 ms 35884 KB Output is correct
52 Correct 475 ms 34864 KB Output is correct
53 Correct 343 ms 32924 KB Output is correct
54 Correct 258 ms 26932 KB Output is correct
55 Correct 473 ms 39408 KB Output is correct
56 Correct 440 ms 40304 KB Output is correct
57 Correct 431 ms 39000 KB Output is correct
58 Correct 357 ms 35116 KB Output is correct
59 Correct 128 ms 15552 KB Output is correct
60 Correct 454 ms 40568 KB Output is correct
61 Correct 463 ms 38952 KB Output is correct
62 Correct 461 ms 41896 KB Output is correct
63 Correct 411 ms 39584 KB Output is correct
64 Correct 5 ms 6912 KB Output is correct
65 Correct 5 ms 6988 KB Output is correct
66 Correct 5 ms 6860 KB Output is correct
67 Correct 4 ms 6732 KB Output is correct
68 Correct 4 ms 6860 KB Output is correct
69 Correct 5 ms 6988 KB Output is correct
70 Correct 5 ms 6988 KB Output is correct
71 Correct 5 ms 6988 KB Output is correct
72 Correct 5 ms 6988 KB Output is correct
73 Correct 5 ms 6988 KB Output is correct
74 Correct 133 ms 28888 KB Output is correct
75 Correct 144 ms 34768 KB Output is correct
76 Correct 141 ms 32060 KB Output is correct
77 Correct 144 ms 31428 KB Output is correct
78 Correct 66 ms 12508 KB Output is correct
79 Correct 55 ms 12472 KB Output is correct
80 Correct 102 ms 25028 KB Output is correct
81 Correct 174 ms 37168 KB Output is correct
82 Correct 179 ms 35020 KB Output is correct
83 Correct 186 ms 40600 KB Output is correct
84 Correct 112 ms 37048 KB Output is correct
85 Correct 162 ms 34264 KB Output is correct
86 Correct 73 ms 15644 KB Output is correct
87 Correct 432 ms 36764 KB Output is correct
88 Correct 470 ms 36708 KB Output is correct
89 Correct 328 ms 32388 KB Output is correct
90 Correct 163 ms 15008 KB Output is correct
91 Correct 187 ms 16876 KB Output is correct
92 Correct 261 ms 27736 KB Output is correct
93 Correct 500 ms 39424 KB Output is correct
94 Correct 377 ms 37192 KB Output is correct
95 Correct 537 ms 42100 KB Output is correct
96 Correct 415 ms 37544 KB Output is correct
97 Correct 353 ms 35720 KB Output is correct