Submission #415175

# Submission time Handle Problem Language Result Execution time Memory
415175 2021-05-31T15:56:04 Z Return_0 Swapping Cities (APIO20_swap) C++17
100 / 100
384 ms 38824 KB
#include "swap.h"

#pragma GCC optimize("Ofast,unroll-loops")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef int ll;
typedef long double ld;
typedef pair <ll, ll> pll;

#ifdef SINA
#define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << std::endl; }
template <typename Arg1, typename... Args> void __f (const char* names, Arg1&& arg1, Args&&... args) {
	const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); }
#define dbg2(x, j, n) cout<< #x << " : "; output((j), (n), x, 1); cout.flush();
#else
#define dbg(...) 0
#define dbg2(x, j, n) 0
#endif
#define SZ(x) ((ll)((x).size()))
#define File(s, t) freopen(s ".txt", "r", stdin); freopen(t ".txt", "w", stdout);
#define input(j, n, a) for (int _i = (j); _i < (n)+(j); _i++) cin>> a[_i];
#define output(j, n, a, t) for (int _i = (j); _i < (n)+(j); _i++) cout<< a[_i] << (((t) && _i != (n)+(j)-1)? ' ' : '\n');
#define kill(x) return cout<< (x) << endl, 0
#define cl const ll
#define fr first
#define sc second
#define lc (v << 1)
#define rc (lc | 1)
#define mid ((l + r) >> 1)
#define All(x) (x).begin(), (x).end()

cl inf = sizeof(ll) == 4 ? (1e9 + 10) : (3e18), mod = 1e9 + 7, MOD = 998244353;

template <class A,class B> ostream& operator << (ostream& out,const pair<A,B>&a){return out<<'('<<a.first<<", "<<a.second<<')';}
template <class A> ostream& operator << (ostream& out, const vector<A> &a) {
  out<< '['; for (int i = -1; ++i < int(a.size());) out<< a[i] << (i + 1 < int(a.size()) ? ", " : ""); return out<<']'; }
template <class T, typename _t = less <T> > using Tree = tree <T, null_type, _t, rb_tree_tag, tree_order_statistics_node_update>;

cl NX = 2e5 + 7, lg = 18;

ll n, m, par [NX], id [NX], deg [NX], cost [NX], anc [NX][lg], h [NX];
vector <ll> adj [NX];
bool ok [NX];

ll root (cl &v) { return ~par[v] ? par[v] = root(par[v]) : v; }
inline void unite (cl &v, cl &u, cl &w) {
	ll _v = root(v), _u = root(u);
	if (_v != _u) {
		par[_v] = par[_u] = n;
		adj[n].push_back(_v);
		adj[n].push_back(_u);
		deg[v]++; deg[u]++;
		ok[n] = ok[_v] || ok[_u] || (max(deg[v], deg[u]) > 2);
		cost[n] = w;
		n++;
	} else if (!ok[_v]) {
		deg[v]++; deg[u]++;
		ok[_v] = 1, cost[_v] = w;
	}
}

void DFS (cl &v = n, cl &pr = n) {
	h[v] = h[pr] + 1;
	anc[v][0] = pr;
	par[v] = v;
	for (ll i = 1; i < lg; i++) anc[v][i] = anc[anc[v][i - 1]][i - 1];
	for (auto &u : adj[v]) if (u != pr) DFS(u, v);
}

inline ll up (ll v, cl &d) { for (ll i = lg; i--;) if (d >> i & 1) v = anc[v][i]; return v; }
inline ll LCA (ll v, ll u) {
	if (h[v] > h[u]) swap(v, u);
	u = up(u, h[u] - h[v]);
	if (v == u) return v;
	for (ll i = lg; i--;) if (anc[v][i] ^ anc[u][i]) v = anc[v][i], u = anc[u][i];
	return anc[v][0];
}

ll fnd (cl &v) { return par[v] = (ok[par[v]] ? par[v] : fnd(anc[par[v]][0])); }

void init (int N, int M, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
	n = N, m = M;
	memset(par, -1, sizeof par);
	iota(id, id + m, 0);
	sort(id, id + m, [&](cl &x, cl &y) { return W[x] < W[y]; });
	for (ll i = 0; i < m; i++) unite(V[id[i]], U[id[i]], W[id[i]]);
	ok[n] = true; adj[n].push_back(n - 1); cost[n] = -1;
	DFS();
}

int getMinimumFuelCapacity (int X, int Y) { return cost[fnd(LCA(X, Y))]; }
/*

*/
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5708 KB Output is correct
2 Correct 5 ms 5708 KB Output is correct
3 Correct 4 ms 5728 KB Output is correct
4 Correct 4 ms 5836 KB Output is correct
5 Correct 5 ms 6020 KB Output is correct
6 Correct 5 ms 5964 KB Output is correct
7 Correct 6 ms 5964 KB Output is correct
8 Correct 5 ms 6056 KB Output is correct
9 Correct 125 ms 24440 KB Output is correct
10 Correct 136 ms 28568 KB Output is correct
11 Correct 128 ms 28120 KB Output is correct
12 Correct 154 ms 29468 KB Output is correct
13 Correct 141 ms 31884 KB Output is correct
14 Correct 118 ms 24752 KB Output is correct
15 Correct 256 ms 32760 KB Output is correct
16 Correct 260 ms 32236 KB Output is correct
17 Correct 261 ms 33552 KB Output is correct
18 Correct 352 ms 35960 KB Output is correct
19 Correct 100 ms 14660 KB Output is correct
20 Correct 262 ms 33948 KB Output is correct
21 Correct 283 ms 33528 KB Output is correct
22 Correct 283 ms 35004 KB Output is correct
23 Correct 384 ms 37352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5708 KB Output is correct
2 Correct 5 ms 5708 KB Output is correct
3 Correct 331 ms 37544 KB Output is correct
4 Correct 335 ms 38824 KB Output is correct
5 Correct 344 ms 38276 KB Output is correct
6 Correct 324 ms 38548 KB Output is correct
7 Correct 287 ms 38464 KB Output is correct
8 Correct 277 ms 37372 KB Output is correct
9 Correct 281 ms 38212 KB Output is correct
10 Correct 273 ms 37116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5708 KB Output is correct
2 Correct 5 ms 5708 KB Output is correct
3 Correct 4 ms 5728 KB Output is correct
4 Correct 4 ms 5836 KB Output is correct
5 Correct 5 ms 6020 KB Output is correct
6 Correct 5 ms 5964 KB Output is correct
7 Correct 6 ms 5964 KB Output is correct
8 Correct 5 ms 6056 KB Output is correct
9 Correct 4 ms 5816 KB Output is correct
10 Correct 5 ms 5964 KB Output is correct
11 Correct 6 ms 5964 KB Output is correct
12 Correct 5 ms 5916 KB Output is correct
13 Correct 5 ms 5908 KB Output is correct
14 Correct 6 ms 5964 KB Output is correct
15 Correct 5 ms 5964 KB Output is correct
16 Correct 5 ms 5964 KB Output is correct
17 Correct 5 ms 6044 KB Output is correct
18 Correct 6 ms 5932 KB Output is correct
19 Correct 5 ms 5964 KB Output is correct
20 Correct 5 ms 5964 KB Output is correct
21 Correct 5 ms 5916 KB Output is correct
22 Correct 6 ms 5836 KB Output is correct
23 Correct 5 ms 5964 KB Output is correct
24 Correct 5 ms 5964 KB Output is correct
25 Correct 5 ms 5964 KB Output is correct
26 Correct 6 ms 5964 KB Output is correct
27 Correct 5 ms 5964 KB Output is correct
28 Correct 7 ms 5964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5816 KB Output is correct
2 Correct 5 ms 5708 KB Output is correct
3 Correct 5 ms 5708 KB Output is correct
4 Correct 4 ms 5728 KB Output is correct
5 Correct 4 ms 5836 KB Output is correct
6 Correct 5 ms 6020 KB Output is correct
7 Correct 5 ms 5964 KB Output is correct
8 Correct 6 ms 5964 KB Output is correct
9 Correct 5 ms 6056 KB Output is correct
10 Correct 125 ms 24440 KB Output is correct
11 Correct 136 ms 28568 KB Output is correct
12 Correct 128 ms 28120 KB Output is correct
13 Correct 154 ms 29468 KB Output is correct
14 Correct 141 ms 31884 KB Output is correct
15 Correct 5 ms 5964 KB Output is correct
16 Correct 6 ms 5964 KB Output is correct
17 Correct 5 ms 5916 KB Output is correct
18 Correct 5 ms 5908 KB Output is correct
19 Correct 6 ms 5964 KB Output is correct
20 Correct 5 ms 5964 KB Output is correct
21 Correct 5 ms 5964 KB Output is correct
22 Correct 5 ms 6044 KB Output is correct
23 Correct 6 ms 5932 KB Output is correct
24 Correct 5 ms 5964 KB Output is correct
25 Correct 5 ms 5964 KB Output is correct
26 Correct 5 ms 5916 KB Output is correct
27 Correct 6 ms 5836 KB Output is correct
28 Correct 5 ms 5964 KB Output is correct
29 Correct 5 ms 5964 KB Output is correct
30 Correct 5 ms 5964 KB Output is correct
31 Correct 6 ms 5964 KB Output is correct
32 Correct 5 ms 5964 KB Output is correct
33 Correct 7 ms 5964 KB Output is correct
34 Correct 19 ms 8908 KB Output is correct
35 Correct 187 ms 29208 KB Output is correct
36 Correct 137 ms 29208 KB Output is correct
37 Correct 196 ms 29124 KB Output is correct
38 Correct 140 ms 28812 KB Output is correct
39 Correct 142 ms 28872 KB Output is correct
40 Correct 143 ms 26840 KB Output is correct
41 Correct 137 ms 29508 KB Output is correct
42 Correct 136 ms 29332 KB Output is correct
43 Correct 137 ms 32408 KB Output is correct
44 Correct 166 ms 29548 KB Output is correct
45 Correct 180 ms 28872 KB Output is correct
46 Correct 162 ms 29236 KB Output is correct
47 Correct 137 ms 29128 KB Output is correct
48 Correct 143 ms 30356 KB Output is correct
49 Correct 79 ms 13068 KB Output is correct
50 Correct 66 ms 12816 KB Output is correct
51 Correct 148 ms 24988 KB Output is correct
52 Correct 176 ms 32484 KB Output is correct
53 Correct 188 ms 33832 KB Output is correct
54 Correct 224 ms 33988 KB Output is correct
55 Correct 120 ms 31940 KB Output is correct
56 Correct 229 ms 34404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5708 KB Output is correct
2 Correct 5 ms 5708 KB Output is correct
3 Correct 4 ms 5728 KB Output is correct
4 Correct 4 ms 5836 KB Output is correct
5 Correct 5 ms 6020 KB Output is correct
6 Correct 5 ms 5964 KB Output is correct
7 Correct 6 ms 5964 KB Output is correct
8 Correct 5 ms 6056 KB Output is correct
9 Correct 125 ms 24440 KB Output is correct
10 Correct 136 ms 28568 KB Output is correct
11 Correct 128 ms 28120 KB Output is correct
12 Correct 154 ms 29468 KB Output is correct
13 Correct 141 ms 31884 KB Output is correct
14 Correct 118 ms 24752 KB Output is correct
15 Correct 256 ms 32760 KB Output is correct
16 Correct 260 ms 32236 KB Output is correct
17 Correct 261 ms 33552 KB Output is correct
18 Correct 352 ms 35960 KB Output is correct
19 Correct 331 ms 37544 KB Output is correct
20 Correct 335 ms 38824 KB Output is correct
21 Correct 344 ms 38276 KB Output is correct
22 Correct 324 ms 38548 KB Output is correct
23 Correct 287 ms 38464 KB Output is correct
24 Correct 277 ms 37372 KB Output is correct
25 Correct 281 ms 38212 KB Output is correct
26 Correct 273 ms 37116 KB Output is correct
27 Correct 5 ms 5964 KB Output is correct
28 Correct 6 ms 5964 KB Output is correct
29 Correct 5 ms 5916 KB Output is correct
30 Correct 5 ms 5908 KB Output is correct
31 Correct 6 ms 5964 KB Output is correct
32 Correct 5 ms 5964 KB Output is correct
33 Correct 5 ms 5964 KB Output is correct
34 Correct 5 ms 6044 KB Output is correct
35 Correct 6 ms 5932 KB Output is correct
36 Correct 19 ms 8908 KB Output is correct
37 Correct 187 ms 29208 KB Output is correct
38 Correct 137 ms 29208 KB Output is correct
39 Correct 196 ms 29124 KB Output is correct
40 Correct 140 ms 28812 KB Output is correct
41 Correct 142 ms 28872 KB Output is correct
42 Correct 143 ms 26840 KB Output is correct
43 Correct 137 ms 29508 KB Output is correct
44 Correct 136 ms 29332 KB Output is correct
45 Correct 137 ms 32408 KB Output is correct
46 Correct 166 ms 29548 KB Output is correct
47 Correct 29 ms 9204 KB Output is correct
48 Correct 307 ms 34052 KB Output is correct
49 Correct 308 ms 34100 KB Output is correct
50 Correct 299 ms 33940 KB Output is correct
51 Correct 272 ms 33924 KB Output is correct
52 Correct 250 ms 32624 KB Output is correct
53 Correct 223 ms 27288 KB Output is correct
54 Correct 307 ms 35020 KB Output is correct
55 Correct 306 ms 34068 KB Output is correct
56 Correct 372 ms 36496 KB Output is correct
57 Correct 305 ms 35116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5816 KB Output is correct
2 Correct 5 ms 5708 KB Output is correct
3 Correct 5 ms 5708 KB Output is correct
4 Correct 4 ms 5728 KB Output is correct
5 Correct 4 ms 5836 KB Output is correct
6 Correct 5 ms 6020 KB Output is correct
7 Correct 5 ms 5964 KB Output is correct
8 Correct 6 ms 5964 KB Output is correct
9 Correct 5 ms 6056 KB Output is correct
10 Correct 125 ms 24440 KB Output is correct
11 Correct 136 ms 28568 KB Output is correct
12 Correct 128 ms 28120 KB Output is correct
13 Correct 154 ms 29468 KB Output is correct
14 Correct 141 ms 31884 KB Output is correct
15 Correct 118 ms 24752 KB Output is correct
16 Correct 256 ms 32760 KB Output is correct
17 Correct 260 ms 32236 KB Output is correct
18 Correct 261 ms 33552 KB Output is correct
19 Correct 352 ms 35960 KB Output is correct
20 Correct 331 ms 37544 KB Output is correct
21 Correct 335 ms 38824 KB Output is correct
22 Correct 344 ms 38276 KB Output is correct
23 Correct 324 ms 38548 KB Output is correct
24 Correct 287 ms 38464 KB Output is correct
25 Correct 277 ms 37372 KB Output is correct
26 Correct 281 ms 38212 KB Output is correct
27 Correct 273 ms 37116 KB Output is correct
28 Correct 5 ms 5964 KB Output is correct
29 Correct 6 ms 5964 KB Output is correct
30 Correct 5 ms 5916 KB Output is correct
31 Correct 5 ms 5908 KB Output is correct
32 Correct 6 ms 5964 KB Output is correct
33 Correct 5 ms 5964 KB Output is correct
34 Correct 5 ms 5964 KB Output is correct
35 Correct 5 ms 6044 KB Output is correct
36 Correct 6 ms 5932 KB Output is correct
37 Correct 19 ms 8908 KB Output is correct
38 Correct 187 ms 29208 KB Output is correct
39 Correct 137 ms 29208 KB Output is correct
40 Correct 196 ms 29124 KB Output is correct
41 Correct 140 ms 28812 KB Output is correct
42 Correct 142 ms 28872 KB Output is correct
43 Correct 143 ms 26840 KB Output is correct
44 Correct 137 ms 29508 KB Output is correct
45 Correct 136 ms 29332 KB Output is correct
46 Correct 137 ms 32408 KB Output is correct
47 Correct 166 ms 29548 KB Output is correct
48 Correct 29 ms 9204 KB Output is correct
49 Correct 307 ms 34052 KB Output is correct
50 Correct 308 ms 34100 KB Output is correct
51 Correct 299 ms 33940 KB Output is correct
52 Correct 272 ms 33924 KB Output is correct
53 Correct 250 ms 32624 KB Output is correct
54 Correct 223 ms 27288 KB Output is correct
55 Correct 307 ms 35020 KB Output is correct
56 Correct 306 ms 34068 KB Output is correct
57 Correct 372 ms 36496 KB Output is correct
58 Correct 305 ms 35116 KB Output is correct
59 Correct 100 ms 14660 KB Output is correct
60 Correct 262 ms 33948 KB Output is correct
61 Correct 283 ms 33528 KB Output is correct
62 Correct 283 ms 35004 KB Output is correct
63 Correct 384 ms 37352 KB Output is correct
64 Correct 5 ms 5964 KB Output is correct
65 Correct 5 ms 5964 KB Output is correct
66 Correct 5 ms 5916 KB Output is correct
67 Correct 6 ms 5836 KB Output is correct
68 Correct 5 ms 5964 KB Output is correct
69 Correct 5 ms 5964 KB Output is correct
70 Correct 5 ms 5964 KB Output is correct
71 Correct 6 ms 5964 KB Output is correct
72 Correct 5 ms 5964 KB Output is correct
73 Correct 7 ms 5964 KB Output is correct
74 Correct 180 ms 28872 KB Output is correct
75 Correct 162 ms 29236 KB Output is correct
76 Correct 137 ms 29128 KB Output is correct
77 Correct 143 ms 30356 KB Output is correct
78 Correct 79 ms 13068 KB Output is correct
79 Correct 66 ms 12816 KB Output is correct
80 Correct 148 ms 24988 KB Output is correct
81 Correct 176 ms 32484 KB Output is correct
82 Correct 188 ms 33832 KB Output is correct
83 Correct 224 ms 33988 KB Output is correct
84 Correct 120 ms 31940 KB Output is correct
85 Correct 229 ms 34404 KB Output is correct
86 Correct 86 ms 15556 KB Output is correct
87 Correct 261 ms 34052 KB Output is correct
88 Correct 281 ms 34172 KB Output is correct
89 Correct 299 ms 33792 KB Output is correct
90 Correct 178 ms 17528 KB Output is correct
91 Correct 188 ms 19432 KB Output is correct
92 Correct 276 ms 29980 KB Output is correct
93 Correct 331 ms 37116 KB Output is correct
94 Correct 348 ms 38176 KB Output is correct
95 Correct 359 ms 38068 KB Output is correct
96 Correct 324 ms 36732 KB Output is correct
97 Correct 373 ms 37580 KB Output is correct