Submission #401681

# Submission time Handle Problem Language Result Execution time Memory
401681 2021-05-10T17:04:40 Z hoaphat1 Swapping Cities (APIO20_swap) C++17
100 / 100
728 ms 46848 KB
#include<bits/stdc++.h>
 
using namespace std;
 
struct graph {
	int n, nlog;
	vector<vector<pair<int,int>>> g;
	vector<vector<int>> pr;
	vector<vector<int>> val;
	vector<int> pos;
	vector<int> end;
	graph() {
		
	}
	void resize(int n) {
		g.resize(n);
		pos.resize(n);
		end.resize(n);
		nlog = 0;
		while (1 << nlog <= n) nlog++;
		pr.resize(n, vector<int> (nlog));
		val.resize(n, vector<int> (nlog));
	}
	void add(int u, int v, int w) {
		g[u].emplace_back(v, w);
		g[v].emplace_back(u, w);
	}
	void dfs(int v, int p = 0) {
		static int count = -1;
		pos[v] = ++count;
		pr[v][0] = p;
		for (int i = 1; i < nlog; i++) {
			pr[v][i] = pr[pr[v][i-1]][i-1];
			val[v][i] = max(val[v][i-1], val[pr[v][i-1]][i-1]);
		}
		for (auto&x : g[v]) {
			int u = x.first;
			if (u != p) {
				val[u][0] = x.second;
				dfs(u, v);
			}
		}
		end[v] = count;
	}
	bool anc(int x, int y) {
		return pos[x] <= pos[y] && end[y] <= end[x];
	}
	int lca(int x, int y) {
		if (anc(x, y)) return x;
		if (anc(y, x)) return y;
		for (int i = nlog - 1; i >= 0; i--) {
			if (pr[x][i] && !anc(pr[x][i], y)) x = pr[x][i];
		}
		return pr[x][0];
	}
	int get(int x, int y) {
		int p = lca(x, y);
		int ans = 0;
		for (int i = nlog - 1; i >= 0; i--) {
			if (pr[x][i] && anc(p, pr[x][i])) {
				ans = max(ans, val[x][i]);
				x = pr[x][i];
			}
			if (pr[y][i] && anc(p, pr[y][i])) {
				ans = max(ans, val[y][i]);
				y = pr[y][i];
			}
		}
		return ans;
	}
};
 
struct dsu {
	vector<int> p;
	int n;
	dsu(int n) {
		resize(n);
	}
	dsu() {
	}
	void resize(int _n) {
		n = _n;
		p = vector<int> (n, -1);
	}
	int get(int x) {
		return p[x] < 0 ? x : p[x] = get(p[x]);
	}
	bool unite(int x, int y) {
		x = get(x); y = get(y);
		if (x != y) {
			if (p[x] > p[y]) swap(x, y);
			p[y] += p[x];
			p[x] = y;
			return true;
		}
		return false;
	}
} d;
 
const int N = 1e5 + 3;
int n, m;
vector<tuple<int,int,int>> edges;
int deg[N];
int event[N];
vector<int> belong[N];
graph g;
 
void init(int _n, int _m, vector<int> u, vector<int> v, vector<int> w) {
	n = _n;
	m = _m;
	for (int i = 0; i < m; i++) edges.emplace_back(w[i], u[i], v[i]);
	sort(edges.begin(), edges.end());
	d.resize(n);
	g.resize(n);
	for (int i = 0; i < n; i++) belong[i] = {i};
	for (auto&x : edges) {
		deg[get<1>(x)]++;
		deg[get<2>(x)]++;
		int u = d.get(get<1>(x)), v = d.get(get<2>(x));
		bool to = (deg[get<1>(x)] >= 3 || deg[get<2>(x)] >= 3);
		if (u == v) {
			if (!event[u]) {
				for (auto&y : belong[u]) event[y] = get<0> (x);
				belong[u].clear();
			}
		}
		else {
			d.unite(u, v);
			g.add(get<1>(x), get<2>(x), get<0>(x));
			if (!event[u] && !event[v]) {
				if (belong[u].size() < belong[v].size()) swap(u, v);
				for (auto&x : belong[v]) belong[u].push_back(x);
				belong[v].clear();
				if (to) {
					for (auto&y : belong[u]) event[y] = get<0>(x);
					belong[u].clear();
				}
				else {
					if (u != d.get(u)) swap(belong[u], belong[v]);
				}
			}
			else {
				for (auto&y : belong[u]) event[y] = get<0>(x);
				belong[u].clear();
				for (auto&y : belong[v]) event[y] = get<0>(x);
				belong[v].clear();
			}
		}
	}
	g.dfs(0, 0);
}
 
int getMinimumFuelCapacity(int x, int y) {
	if (!event[x] || !event[y]) return -1;
	return max({event[x], event[y], g.get(x, y)});
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 3 ms 2764 KB Output is correct
5 Correct 3 ms 2892 KB Output is correct
6 Correct 3 ms 2892 KB Output is correct
7 Correct 3 ms 2892 KB Output is correct
8 Correct 3 ms 2904 KB Output is correct
9 Correct 192 ms 34080 KB Output is correct
10 Correct 269 ms 41144 KB Output is correct
11 Correct 248 ms 40388 KB Output is correct
12 Correct 273 ms 42720 KB Output is correct
13 Correct 275 ms 40824 KB Output is correct
14 Correct 211 ms 33728 KB Output is correct
15 Correct 394 ms 43420 KB Output is correct
16 Correct 313 ms 41524 KB Output is correct
17 Correct 344 ms 45392 KB Output is correct
18 Correct 297 ms 42016 KB Output is correct
19 Correct 138 ms 11952 KB Output is correct
20 Correct 632 ms 44860 KB Output is correct
21 Correct 616 ms 42988 KB Output is correct
22 Correct 662 ms 46304 KB Output is correct
23 Correct 629 ms 43564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 273 ms 38916 KB Output is correct
4 Correct 286 ms 40676 KB Output is correct
5 Correct 280 ms 39656 KB Output is correct
6 Correct 287 ms 40456 KB Output is correct
7 Correct 283 ms 40160 KB Output is correct
8 Correct 265 ms 38828 KB Output is correct
9 Correct 278 ms 39832 KB Output is correct
10 Correct 287 ms 38388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 3 ms 2764 KB Output is correct
5 Correct 3 ms 2892 KB Output is correct
6 Correct 3 ms 2892 KB Output is correct
7 Correct 3 ms 2892 KB Output is correct
8 Correct 3 ms 2904 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 3 ms 2892 KB Output is correct
11 Correct 4 ms 2892 KB Output is correct
12 Correct 3 ms 2892 KB Output is correct
13 Correct 3 ms 2764 KB Output is correct
14 Correct 3 ms 2892 KB Output is correct
15 Correct 3 ms 2892 KB Output is correct
16 Correct 3 ms 2892 KB Output is correct
17 Correct 3 ms 2892 KB Output is correct
18 Correct 3 ms 2892 KB Output is correct
19 Correct 3 ms 2764 KB Output is correct
20 Correct 3 ms 2892 KB Output is correct
21 Correct 3 ms 2892 KB Output is correct
22 Correct 3 ms 2764 KB Output is correct
23 Correct 3 ms 2764 KB Output is correct
24 Correct 3 ms 2892 KB Output is correct
25 Correct 4 ms 2920 KB Output is correct
26 Correct 4 ms 2996 KB Output is correct
27 Correct 3 ms 2892 KB Output is correct
28 Correct 3 ms 2892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 3 ms 2764 KB Output is correct
6 Correct 3 ms 2892 KB Output is correct
7 Correct 3 ms 2892 KB Output is correct
8 Correct 3 ms 2892 KB Output is correct
9 Correct 3 ms 2904 KB Output is correct
10 Correct 192 ms 34080 KB Output is correct
11 Correct 269 ms 41144 KB Output is correct
12 Correct 248 ms 40388 KB Output is correct
13 Correct 273 ms 42720 KB Output is correct
14 Correct 275 ms 40824 KB Output is correct
15 Correct 3 ms 2892 KB Output is correct
16 Correct 4 ms 2892 KB Output is correct
17 Correct 3 ms 2892 KB Output is correct
18 Correct 3 ms 2764 KB Output is correct
19 Correct 3 ms 2892 KB Output is correct
20 Correct 3 ms 2892 KB Output is correct
21 Correct 3 ms 2892 KB Output is correct
22 Correct 3 ms 2892 KB Output is correct
23 Correct 3 ms 2892 KB Output is correct
24 Correct 3 ms 2764 KB Output is correct
25 Correct 3 ms 2892 KB Output is correct
26 Correct 3 ms 2892 KB Output is correct
27 Correct 3 ms 2764 KB Output is correct
28 Correct 3 ms 2764 KB Output is correct
29 Correct 3 ms 2892 KB Output is correct
30 Correct 4 ms 2920 KB Output is correct
31 Correct 4 ms 2996 KB Output is correct
32 Correct 3 ms 2892 KB Output is correct
33 Correct 3 ms 2892 KB Output is correct
34 Correct 19 ms 6924 KB Output is correct
35 Correct 305 ms 41900 KB Output is correct
36 Correct 267 ms 40376 KB Output is correct
37 Correct 258 ms 38812 KB Output is correct
38 Correct 256 ms 37608 KB Output is correct
39 Correct 223 ms 36872 KB Output is correct
40 Correct 192 ms 34032 KB Output is correct
41 Correct 270 ms 41972 KB Output is correct
42 Correct 271 ms 42848 KB Output is correct
43 Correct 230 ms 39660 KB Output is correct
44 Correct 224 ms 37872 KB Output is correct
45 Correct 201 ms 32536 KB Output is correct
46 Correct 263 ms 39968 KB Output is correct
47 Correct 234 ms 37720 KB Output is correct
48 Correct 207 ms 36980 KB Output is correct
49 Correct 75 ms 9568 KB Output is correct
50 Correct 63 ms 8920 KB Output is correct
51 Correct 159 ms 27264 KB Output is correct
52 Correct 290 ms 42476 KB Output is correct
53 Correct 277 ms 40836 KB Output is correct
54 Correct 320 ms 45224 KB Output is correct
55 Correct 237 ms 40120 KB Output is correct
56 Correct 260 ms 40112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 3 ms 2764 KB Output is correct
5 Correct 3 ms 2892 KB Output is correct
6 Correct 3 ms 2892 KB Output is correct
7 Correct 3 ms 2892 KB Output is correct
8 Correct 3 ms 2904 KB Output is correct
9 Correct 192 ms 34080 KB Output is correct
10 Correct 269 ms 41144 KB Output is correct
11 Correct 248 ms 40388 KB Output is correct
12 Correct 273 ms 42720 KB Output is correct
13 Correct 275 ms 40824 KB Output is correct
14 Correct 211 ms 33728 KB Output is correct
15 Correct 394 ms 43420 KB Output is correct
16 Correct 313 ms 41524 KB Output is correct
17 Correct 344 ms 45392 KB Output is correct
18 Correct 297 ms 42016 KB Output is correct
19 Correct 273 ms 38916 KB Output is correct
20 Correct 286 ms 40676 KB Output is correct
21 Correct 280 ms 39656 KB Output is correct
22 Correct 287 ms 40456 KB Output is correct
23 Correct 283 ms 40160 KB Output is correct
24 Correct 265 ms 38828 KB Output is correct
25 Correct 278 ms 39832 KB Output is correct
26 Correct 287 ms 38388 KB Output is correct
27 Correct 3 ms 2892 KB Output is correct
28 Correct 4 ms 2892 KB Output is correct
29 Correct 3 ms 2892 KB Output is correct
30 Correct 3 ms 2764 KB Output is correct
31 Correct 3 ms 2892 KB Output is correct
32 Correct 3 ms 2892 KB Output is correct
33 Correct 3 ms 2892 KB Output is correct
34 Correct 3 ms 2892 KB Output is correct
35 Correct 3 ms 2892 KB Output is correct
36 Correct 19 ms 6924 KB Output is correct
37 Correct 305 ms 41900 KB Output is correct
38 Correct 267 ms 40376 KB Output is correct
39 Correct 258 ms 38812 KB Output is correct
40 Correct 256 ms 37608 KB Output is correct
41 Correct 223 ms 36872 KB Output is correct
42 Correct 192 ms 34032 KB Output is correct
43 Correct 270 ms 41972 KB Output is correct
44 Correct 271 ms 42848 KB Output is correct
45 Correct 230 ms 39660 KB Output is correct
46 Correct 224 ms 37872 KB Output is correct
47 Correct 32 ms 6924 KB Output is correct
48 Correct 668 ms 43968 KB Output is correct
49 Correct 674 ms 42316 KB Output is correct
50 Correct 657 ms 41368 KB Output is correct
51 Correct 631 ms 40416 KB Output is correct
52 Correct 539 ms 37820 KB Output is correct
53 Correct 380 ms 29444 KB Output is correct
54 Correct 660 ms 44536 KB Output is correct
55 Correct 692 ms 45216 KB Output is correct
56 Correct 640 ms 42444 KB Output is correct
57 Correct 499 ms 40908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 3 ms 2764 KB Output is correct
6 Correct 3 ms 2892 KB Output is correct
7 Correct 3 ms 2892 KB Output is correct
8 Correct 3 ms 2892 KB Output is correct
9 Correct 3 ms 2904 KB Output is correct
10 Correct 192 ms 34080 KB Output is correct
11 Correct 269 ms 41144 KB Output is correct
12 Correct 248 ms 40388 KB Output is correct
13 Correct 273 ms 42720 KB Output is correct
14 Correct 275 ms 40824 KB Output is correct
15 Correct 211 ms 33728 KB Output is correct
16 Correct 394 ms 43420 KB Output is correct
17 Correct 313 ms 41524 KB Output is correct
18 Correct 344 ms 45392 KB Output is correct
19 Correct 297 ms 42016 KB Output is correct
20 Correct 273 ms 38916 KB Output is correct
21 Correct 286 ms 40676 KB Output is correct
22 Correct 280 ms 39656 KB Output is correct
23 Correct 287 ms 40456 KB Output is correct
24 Correct 283 ms 40160 KB Output is correct
25 Correct 265 ms 38828 KB Output is correct
26 Correct 278 ms 39832 KB Output is correct
27 Correct 287 ms 38388 KB Output is correct
28 Correct 3 ms 2892 KB Output is correct
29 Correct 4 ms 2892 KB Output is correct
30 Correct 3 ms 2892 KB Output is correct
31 Correct 3 ms 2764 KB Output is correct
32 Correct 3 ms 2892 KB Output is correct
33 Correct 3 ms 2892 KB Output is correct
34 Correct 3 ms 2892 KB Output is correct
35 Correct 3 ms 2892 KB Output is correct
36 Correct 3 ms 2892 KB Output is correct
37 Correct 19 ms 6924 KB Output is correct
38 Correct 305 ms 41900 KB Output is correct
39 Correct 267 ms 40376 KB Output is correct
40 Correct 258 ms 38812 KB Output is correct
41 Correct 256 ms 37608 KB Output is correct
42 Correct 223 ms 36872 KB Output is correct
43 Correct 192 ms 34032 KB Output is correct
44 Correct 270 ms 41972 KB Output is correct
45 Correct 271 ms 42848 KB Output is correct
46 Correct 230 ms 39660 KB Output is correct
47 Correct 224 ms 37872 KB Output is correct
48 Correct 32 ms 6924 KB Output is correct
49 Correct 668 ms 43968 KB Output is correct
50 Correct 674 ms 42316 KB Output is correct
51 Correct 657 ms 41368 KB Output is correct
52 Correct 631 ms 40416 KB Output is correct
53 Correct 539 ms 37820 KB Output is correct
54 Correct 380 ms 29444 KB Output is correct
55 Correct 660 ms 44536 KB Output is correct
56 Correct 692 ms 45216 KB Output is correct
57 Correct 640 ms 42444 KB Output is correct
58 Correct 499 ms 40908 KB Output is correct
59 Correct 138 ms 11952 KB Output is correct
60 Correct 632 ms 44860 KB Output is correct
61 Correct 616 ms 42988 KB Output is correct
62 Correct 662 ms 46304 KB Output is correct
63 Correct 629 ms 43564 KB Output is correct
64 Correct 3 ms 2764 KB Output is correct
65 Correct 3 ms 2892 KB Output is correct
66 Correct 3 ms 2892 KB Output is correct
67 Correct 3 ms 2764 KB Output is correct
68 Correct 3 ms 2764 KB Output is correct
69 Correct 3 ms 2892 KB Output is correct
70 Correct 4 ms 2920 KB Output is correct
71 Correct 4 ms 2996 KB Output is correct
72 Correct 3 ms 2892 KB Output is correct
73 Correct 3 ms 2892 KB Output is correct
74 Correct 201 ms 32536 KB Output is correct
75 Correct 263 ms 39968 KB Output is correct
76 Correct 234 ms 37720 KB Output is correct
77 Correct 207 ms 36980 KB Output is correct
78 Correct 75 ms 9568 KB Output is correct
79 Correct 63 ms 8920 KB Output is correct
80 Correct 159 ms 27264 KB Output is correct
81 Correct 290 ms 42476 KB Output is correct
82 Correct 277 ms 40836 KB Output is correct
83 Correct 320 ms 45224 KB Output is correct
84 Correct 237 ms 40120 KB Output is correct
85 Correct 260 ms 40112 KB Output is correct
86 Correct 100 ms 14616 KB Output is correct
87 Correct 651 ms 42068 KB Output is correct
88 Correct 687 ms 41964 KB Output is correct
89 Correct 430 ms 37424 KB Output is correct
90 Correct 176 ms 11260 KB Output is correct
91 Correct 203 ms 13512 KB Output is correct
92 Correct 362 ms 30080 KB Output is correct
93 Correct 718 ms 44520 KB Output is correct
94 Correct 505 ms 43228 KB Output is correct
95 Correct 728 ms 46848 KB Output is correct
96 Correct 637 ms 41668 KB Output is correct
97 Correct 496 ms 41796 KB Output is correct