Submission #870728

# Submission time Handle Problem Language Result Execution time Memory
870728 2023-11-09T02:13:41 Z marvinthang Swapping Cities (APIO20_swap) C++17
100 / 100
407 ms 65932 KB
/*************************************
*    author: marvinthang             *
*    created: 09.11.2023 08:43:17    *
*************************************/

#include "swap.h"
#include <bits/stdc++.h>

using namespace std;

#define                  fi  first
#define                  se  second
#define                left  ___left
#define               right  ___right
#define                TIME  (1.0 * clock() / CLOCKS_PER_SEC)
#define             MASK(i)  (1LL << (i))
#define           BIT(x, i)  ((x) >> (i) & 1)
#define  __builtin_popcount  __builtin_popcountll
#define              ALL(v)  (v).begin(), (v).end()
#define           REP(i, n)  for (int i = 0, _n = (n); i < _n; ++i)
#define          REPD(i, n)  for (int i = (n); i-- > 0; )
#define        FOR(i, a, b)  for (int i = (a), _b = (b); i < _b; ++i) 
#define       FORD(i, b, a)  for (int i = (b), _a = (a); --i >= _a; ) 
#define       FORE(i, a, b)  for (int i = (a), _b = (b); i <= _b; ++i) 
#define      FORDE(i, b, a)  for (int i = (b), _a = (a); i >= _a; --i) 
#define        scan_op(...)  istream & operator >> (istream &in, __VA_ARGS__ &u)
#define       print_op(...)  ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
		#include "debug.h"
#else
		#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
		#define DB(...) 23
		#define db(...) 23
		#define debug(...) 23
#endif

template <class U, class V> scan_op(pair <U, V>)  { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>)  { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>)  { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")";  else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }

// end of template

const int LOG = 20;

vector <vector <int>> adj;
vector <bool> can_connect;
vector <int> weights, anc[20], depth;

void init(int n, int m, vector <int> U, vector <int> V, vector <int> W) {
	vector <tuple <int, int, int>> edges;
	REP(i, m) edges.emplace_back(W[i], U[i], V[i]);
	sort(ALL(edges));
	vector <int> par(n + m);
	iota(ALL(par), 0);
	vector <int> deg(n);
	adj.resize(n + m);
	can_connect.resize(n + m);
	weights.resize(n + m);
	function <int(int)> find = [&] (int u) {
		return par[u] == u ? u : par[u] = find(par[u]);
	};
	auto join = [&] (int u, int v) {
		int pu = find(u), pv = find(v);
		par[pu] = par[pv] = n;
		adj[n].push_back(pu);
		can_connect[n] = can_connect[pu] || can_connect[pv];
		if (pu == pv) {
			can_connect[n] = true;
		} else {
			adj[n].push_back(pv);
			if (max(++deg[u], ++deg[v]) > 2) can_connect[n] = true;
		}
		++n;
	};

	for (auto [w, u, v]: edges) {
		weights[n] = w;
		join(u, v);
	}
	REP(i, LOG) anc[i].resize(n);
	depth.resize(n);
	function<void(int)> dfs = [&] (int u) {
		FOR(i, 1, LOG) anc[i][u] = anc[i - 1][anc[i - 1][u]];
		for (int v: adj[u]) if (v != anc[0][u]) {
			depth[v] = depth[u] + 1;
			anc[0][v] = u;
			dfs(v);
		}
	};
	anc[0][n - 1] = n - 1;
	dfs(n - 1);
}

int getMinimumFuelCapacity(int u, int v) {
	if (depth[u] < depth[v]) swap(u, v);
	REPD(i, LOG) if (depth[anc[i][u]] >= depth[v]) u = anc[i][u];
	if (u != v) {
		REPD(i, LOG) if (anc[i][u] != anc[i][v]) u = anc[i][u], v = anc[i][v];
		u = anc[0][u];
	}
	if (can_connect[u]) return weights[u];
	REPD(i, LOG) if (!can_connect[anc[i][u]]) u = anc[i][u];
	return anc[0][u] == u ? -1 : weights[anc[0][u]];
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 73 ms 25228 KB Output is correct
10 Correct 97 ms 31056 KB Output is correct
11 Correct 90 ms 30472 KB Output is correct
12 Correct 112 ms 32380 KB Output is correct
13 Correct 108 ms 37084 KB Output is correct
14 Correct 90 ms 25540 KB Output is correct
15 Correct 341 ms 35144 KB Output is correct
16 Correct 305 ms 34040 KB Output is correct
17 Correct 368 ms 36296 KB Output is correct
18 Correct 366 ms 41028 KB Output is correct
19 Correct 115 ms 10324 KB Output is correct
20 Correct 365 ms 35016 KB Output is correct
21 Correct 340 ms 33740 KB Output is correct
22 Correct 317 ms 36344 KB Output is correct
23 Correct 387 ms 40900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 271 ms 42560 KB Output is correct
4 Correct 268 ms 44800 KB Output is correct
5 Correct 334 ms 43940 KB Output is correct
6 Correct 298 ms 44644 KB Output is correct
7 Correct 333 ms 44044 KB Output is correct
8 Correct 314 ms 42440 KB Output is correct
9 Correct 287 ms 43788 KB Output is correct
10 Correct 310 ms 41916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 700 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 700 KB Output is correct
22 Correct 1 ms 860 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 2 ms 860 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 2 ms 860 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 2 ms 1004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 73 ms 25228 KB Output is correct
11 Correct 97 ms 31056 KB Output is correct
12 Correct 90 ms 30472 KB Output is correct
13 Correct 112 ms 32380 KB Output is correct
14 Correct 108 ms 37084 KB Output is correct
15 Correct 1 ms 700 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 700 KB Output is correct
27 Correct 1 ms 860 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 2 ms 860 KB Output is correct
30 Correct 1 ms 860 KB Output is correct
31 Correct 2 ms 860 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 2 ms 1004 KB Output is correct
34 Correct 10 ms 4636 KB Output is correct
35 Correct 108 ms 32084 KB Output is correct
36 Correct 114 ms 32092 KB Output is correct
37 Correct 111 ms 32080 KB Output is correct
38 Correct 91 ms 31568 KB Output is correct
39 Correct 117 ms 31596 KB Output is correct
40 Correct 88 ms 28728 KB Output is correct
41 Correct 109 ms 32176 KB Output is correct
42 Correct 103 ms 31940 KB Output is correct
43 Correct 109 ms 38132 KB Output is correct
44 Correct 104 ms 32308 KB Output is correct
45 Correct 118 ms 48540 KB Output is correct
46 Correct 107 ms 31940 KB Output is correct
47 Correct 107 ms 32200 KB Output is correct
48 Correct 118 ms 37548 KB Output is correct
49 Correct 94 ms 46276 KB Output is correct
50 Correct 79 ms 36344 KB Output is correct
51 Correct 115 ms 41832 KB Output is correct
52 Correct 139 ms 51280 KB Output is correct
53 Correct 162 ms 53396 KB Output is correct
54 Correct 181 ms 61368 KB Output is correct
55 Correct 103 ms 37592 KB Output is correct
56 Correct 153 ms 56348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 73 ms 25228 KB Output is correct
10 Correct 97 ms 31056 KB Output is correct
11 Correct 90 ms 30472 KB Output is correct
12 Correct 112 ms 32380 KB Output is correct
13 Correct 108 ms 37084 KB Output is correct
14 Correct 90 ms 25540 KB Output is correct
15 Correct 341 ms 35144 KB Output is correct
16 Correct 305 ms 34040 KB Output is correct
17 Correct 368 ms 36296 KB Output is correct
18 Correct 366 ms 41028 KB Output is correct
19 Correct 271 ms 42560 KB Output is correct
20 Correct 268 ms 44800 KB Output is correct
21 Correct 334 ms 43940 KB Output is correct
22 Correct 298 ms 44644 KB Output is correct
23 Correct 333 ms 44044 KB Output is correct
24 Correct 314 ms 42440 KB Output is correct
25 Correct 287 ms 43788 KB Output is correct
26 Correct 310 ms 41916 KB Output is correct
27 Correct 1 ms 700 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 10 ms 4636 KB Output is correct
37 Correct 108 ms 32084 KB Output is correct
38 Correct 114 ms 32092 KB Output is correct
39 Correct 111 ms 32080 KB Output is correct
40 Correct 91 ms 31568 KB Output is correct
41 Correct 117 ms 31596 KB Output is correct
42 Correct 88 ms 28728 KB Output is correct
43 Correct 109 ms 32176 KB Output is correct
44 Correct 103 ms 31940 KB Output is correct
45 Correct 109 ms 38132 KB Output is correct
46 Correct 104 ms 32308 KB Output is correct
47 Correct 19 ms 4732 KB Output is correct
48 Correct 302 ms 35780 KB Output is correct
49 Correct 329 ms 35788 KB Output is correct
50 Correct 244 ms 35780 KB Output is correct
51 Correct 258 ms 35528 KB Output is correct
52 Correct 227 ms 34260 KB Output is correct
53 Correct 185 ms 26540 KB Output is correct
54 Correct 290 ms 36108 KB Output is correct
55 Correct 264 ms 35780 KB Output is correct
56 Correct 283 ms 40640 KB Output is correct
57 Correct 293 ms 36312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 73 ms 25228 KB Output is correct
11 Correct 97 ms 31056 KB Output is correct
12 Correct 90 ms 30472 KB Output is correct
13 Correct 112 ms 32380 KB Output is correct
14 Correct 108 ms 37084 KB Output is correct
15 Correct 90 ms 25540 KB Output is correct
16 Correct 341 ms 35144 KB Output is correct
17 Correct 305 ms 34040 KB Output is correct
18 Correct 368 ms 36296 KB Output is correct
19 Correct 366 ms 41028 KB Output is correct
20 Correct 271 ms 42560 KB Output is correct
21 Correct 268 ms 44800 KB Output is correct
22 Correct 334 ms 43940 KB Output is correct
23 Correct 298 ms 44644 KB Output is correct
24 Correct 333 ms 44044 KB Output is correct
25 Correct 314 ms 42440 KB Output is correct
26 Correct 287 ms 43788 KB Output is correct
27 Correct 310 ms 41916 KB Output is correct
28 Correct 1 ms 700 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 1 ms 604 KB Output is correct
37 Correct 10 ms 4636 KB Output is correct
38 Correct 108 ms 32084 KB Output is correct
39 Correct 114 ms 32092 KB Output is correct
40 Correct 111 ms 32080 KB Output is correct
41 Correct 91 ms 31568 KB Output is correct
42 Correct 117 ms 31596 KB Output is correct
43 Correct 88 ms 28728 KB Output is correct
44 Correct 109 ms 32176 KB Output is correct
45 Correct 103 ms 31940 KB Output is correct
46 Correct 109 ms 38132 KB Output is correct
47 Correct 104 ms 32308 KB Output is correct
48 Correct 19 ms 4732 KB Output is correct
49 Correct 302 ms 35780 KB Output is correct
50 Correct 329 ms 35788 KB Output is correct
51 Correct 244 ms 35780 KB Output is correct
52 Correct 258 ms 35528 KB Output is correct
53 Correct 227 ms 34260 KB Output is correct
54 Correct 185 ms 26540 KB Output is correct
55 Correct 290 ms 36108 KB Output is correct
56 Correct 264 ms 35780 KB Output is correct
57 Correct 283 ms 40640 KB Output is correct
58 Correct 293 ms 36312 KB Output is correct
59 Correct 115 ms 10324 KB Output is correct
60 Correct 365 ms 35016 KB Output is correct
61 Correct 340 ms 33740 KB Output is correct
62 Correct 317 ms 36344 KB Output is correct
63 Correct 387 ms 40900 KB Output is correct
64 Correct 1 ms 604 KB Output is correct
65 Correct 1 ms 604 KB Output is correct
66 Correct 1 ms 700 KB Output is correct
67 Correct 1 ms 860 KB Output is correct
68 Correct 1 ms 604 KB Output is correct
69 Correct 2 ms 860 KB Output is correct
70 Correct 1 ms 860 KB Output is correct
71 Correct 2 ms 860 KB Output is correct
72 Correct 1 ms 604 KB Output is correct
73 Correct 2 ms 1004 KB Output is correct
74 Correct 118 ms 48540 KB Output is correct
75 Correct 107 ms 31940 KB Output is correct
76 Correct 107 ms 32200 KB Output is correct
77 Correct 118 ms 37548 KB Output is correct
78 Correct 94 ms 46276 KB Output is correct
79 Correct 79 ms 36344 KB Output is correct
80 Correct 115 ms 41832 KB Output is correct
81 Correct 139 ms 51280 KB Output is correct
82 Correct 162 ms 53396 KB Output is correct
83 Correct 181 ms 61368 KB Output is correct
84 Correct 103 ms 37592 KB Output is correct
85 Correct 153 ms 56348 KB Output is correct
86 Correct 71 ms 17612 KB Output is correct
87 Correct 255 ms 36148 KB Output is correct
88 Correct 254 ms 35896 KB Output is correct
89 Correct 239 ms 39824 KB Output is correct
90 Correct 203 ms 52420 KB Output is correct
91 Correct 216 ms 48064 KB Output is correct
92 Correct 271 ms 43448 KB Output is correct
93 Correct 332 ms 53700 KB Output is correct
94 Correct 361 ms 58404 KB Output is correct
95 Correct 407 ms 65932 KB Output is correct
96 Correct 319 ms 40900 KB Output is correct
97 Correct 341 ms 50452 KB Output is correct