Submission #676529

# Submission time Handle Problem Language Result Execution time Memory
676529 2022-12-31T07:48:05 Z NothingXD Swapping Cities (APIO20_swap) C++14
100 / 100
312 ms 57600 KB
#include "swap.h"

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
/*typedef __uint128_t L;
struct FastMod {
	ull b, m;
	FastMod(ull b) : b(b), m(ull((L(1) << 64) / b)) {}
	ull reduce(ull a) {
		ull q = (ull)((L(m) * a) >> 64);
		ull r = a - q * b; // can be proven that 0 <= r < 2*b
		return r >= b ? r - b : r;
	}
};
FastMod FM(2);*/
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	cerr << " " << H;
	debug_out(T...);
}

#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define MP(x, y) make_pair(x, y)
#define F first
#define S second

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int maxn = 3e5 + 10;
const int lg = 20;

int n, m, a[maxn], dsu[maxn], d[maxn], leaf[maxn], par[maxn][lg], h[maxn], vercnt;
bool cycle[maxn], ok[maxn];
vector<int> g[maxn];

int getdsu(int v){
	return (dsu[v] == -1? v: dsu[v] = getdsu(dsu[v]));
}

void merge(int u, int v, int w){
	int U = getdsu(u), V = getdsu(v);
	if (U == V){
		vercnt++;
		cycle[vercnt] = true;
		dsu[U] = vercnt;
		g[vercnt].push_back(U);
		leaf[vercnt] = leaf[U];
	}
	else{
		vercnt++;
		dsu[U] = dsu[V] = vercnt;
		g[vercnt].push_back(U);
		g[vercnt].push_back(V);
		leaf[vercnt] = leaf[U] + leaf[V];
		cycle[vercnt] = (cycle[U] || cycle[V]);
	}
	if (d[u] == 1) leaf[vercnt]--;
	if (d[v] == 1) leaf[vercnt]--;
	d[u]++;
	d[v]++;
	ok[vercnt] = (cycle[vercnt] || (leaf[vercnt] > 2));
	a[vercnt] = w;
}

void dfs(int v, int p = -1){
	if (!ok[v]){
		if (p == -1) a[v] = -1;
		else a[v] = a[p];
	}
	par[v][0] = p;
	for (int i = 1; i < lg; i++){
		if (par[v][i-1] != -1) par[v][i] = par[par[v][i-1]][i-1];
	}
	for (auto u: g[v]){
		h[u] = h[v] + 1;
		dfs(u, v);
	}
}

int getpar(int v, int x){
	for (int i = 0; i < lg; i++) if ((x >> i) & 1) v = par[v][i];
	return v;
}

int lca(int u, int v){
	if (h[v] > h[u]) swap(u, v);
	u = getpar(u, h[u]-h[v]);
	if (u == v) return u;
	for (int i = lg-1; ~i; i--){
		if (par[v][i] != par[u][i]){
			v = par[v][i];
			u = par[u][i];
		}
	}
	return par[v][0];
}

void init(int N, int M,
          std::vector<int> U, std::vector<int> V, std::vector<int> W) {
	memset(dsu, -1, sizeof dsu);
	memset(par, -1, sizeof par);
	n = N, m = M;
	for (int i = 1; i <= n; i++) leaf[i] = 1;
	vector<pair<int,pii>> edge;
	for (int i = 0; i < m; i++) edge.push_back({W[i], {U[i]+1, V[i]+1}});
	sort(all(edge));
	vercnt = n;
	for (auto [w, x]: edge){
		merge(x.F, x.S, w);
	}
	dfs(vercnt);
}

int getMinimumFuelCapacity(int X, int Y) {
	int v = lca(X+1, Y+1);
	return a[v];
}

Compilation message

swap.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
swap.cpp:118:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  118 |  for (auto [w, x]: edge){
      |            ^
# Verdict Execution time Memory Grader output
1 Correct 15 ms 32008 KB Output is correct
2 Correct 15 ms 31932 KB Output is correct
3 Correct 15 ms 31956 KB Output is correct
4 Correct 15 ms 32084 KB Output is correct
5 Correct 15 ms 32104 KB Output is correct
6 Correct 15 ms 32084 KB Output is correct
7 Correct 15 ms 32112 KB Output is correct
8 Correct 15 ms 32084 KB Output is correct
9 Correct 84 ms 39692 KB Output is correct
10 Correct 101 ms 41396 KB Output is correct
11 Correct 106 ms 41212 KB Output is correct
12 Correct 119 ms 41792 KB Output is correct
13 Correct 102 ms 44164 KB Output is correct
14 Correct 94 ms 39836 KB Output is correct
15 Correct 247 ms 43260 KB Output is correct
16 Correct 206 ms 42932 KB Output is correct
17 Correct 208 ms 43536 KB Output is correct
18 Correct 298 ms 45968 KB Output is correct
19 Correct 94 ms 36996 KB Output is correct
20 Correct 202 ms 43568 KB Output is correct
21 Correct 208 ms 43472 KB Output is correct
22 Correct 245 ms 43968 KB Output is correct
23 Correct 304 ms 46348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 32008 KB Output is correct
2 Correct 15 ms 31932 KB Output is correct
3 Correct 255 ms 47856 KB Output is correct
4 Correct 298 ms 48416 KB Output is correct
5 Correct 266 ms 48320 KB Output is correct
6 Correct 274 ms 48376 KB Output is correct
7 Correct 276 ms 48256 KB Output is correct
8 Correct 260 ms 50192 KB Output is correct
9 Correct 238 ms 50452 KB Output is correct
10 Correct 247 ms 50076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 32008 KB Output is correct
2 Correct 15 ms 31932 KB Output is correct
3 Correct 15 ms 31956 KB Output is correct
4 Correct 15 ms 32084 KB Output is correct
5 Correct 15 ms 32104 KB Output is correct
6 Correct 15 ms 32084 KB Output is correct
7 Correct 15 ms 32112 KB Output is correct
8 Correct 15 ms 32084 KB Output is correct
9 Correct 15 ms 31936 KB Output is correct
10 Correct 15 ms 32072 KB Output is correct
11 Correct 16 ms 32084 KB Output is correct
12 Correct 15 ms 32084 KB Output is correct
13 Correct 14 ms 32104 KB Output is correct
14 Correct 15 ms 32072 KB Output is correct
15 Correct 15 ms 32072 KB Output is correct
16 Correct 14 ms 32068 KB Output is correct
17 Correct 15 ms 32112 KB Output is correct
18 Correct 15 ms 32148 KB Output is correct
19 Correct 15 ms 32068 KB Output is correct
20 Correct 16 ms 32084 KB Output is correct
21 Correct 18 ms 32100 KB Output is correct
22 Correct 17 ms 32212 KB Output is correct
23 Correct 15 ms 32084 KB Output is correct
24 Correct 15 ms 32256 KB Output is correct
25 Correct 17 ms 32156 KB Output is correct
26 Correct 15 ms 32248 KB Output is correct
27 Correct 15 ms 32084 KB Output is correct
28 Correct 15 ms 32240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 31936 KB Output is correct
2 Correct 15 ms 32008 KB Output is correct
3 Correct 15 ms 31932 KB Output is correct
4 Correct 15 ms 31956 KB Output is correct
5 Correct 15 ms 32084 KB Output is correct
6 Correct 15 ms 32104 KB Output is correct
7 Correct 15 ms 32084 KB Output is correct
8 Correct 15 ms 32112 KB Output is correct
9 Correct 15 ms 32084 KB Output is correct
10 Correct 84 ms 39692 KB Output is correct
11 Correct 101 ms 41396 KB Output is correct
12 Correct 106 ms 41212 KB Output is correct
13 Correct 119 ms 41792 KB Output is correct
14 Correct 102 ms 44164 KB Output is correct
15 Correct 15 ms 32072 KB Output is correct
16 Correct 16 ms 32084 KB Output is correct
17 Correct 15 ms 32084 KB Output is correct
18 Correct 14 ms 32104 KB Output is correct
19 Correct 15 ms 32072 KB Output is correct
20 Correct 15 ms 32072 KB Output is correct
21 Correct 14 ms 32068 KB Output is correct
22 Correct 15 ms 32112 KB Output is correct
23 Correct 15 ms 32148 KB Output is correct
24 Correct 15 ms 32068 KB Output is correct
25 Correct 16 ms 32084 KB Output is correct
26 Correct 18 ms 32100 KB Output is correct
27 Correct 17 ms 32212 KB Output is correct
28 Correct 15 ms 32084 KB Output is correct
29 Correct 15 ms 32256 KB Output is correct
30 Correct 17 ms 32156 KB Output is correct
31 Correct 15 ms 32248 KB Output is correct
32 Correct 15 ms 32084 KB Output is correct
33 Correct 15 ms 32240 KB Output is correct
34 Correct 23 ms 33504 KB Output is correct
35 Correct 108 ms 41772 KB Output is correct
36 Correct 132 ms 41932 KB Output is correct
37 Correct 105 ms 41796 KB Output is correct
38 Correct 111 ms 41692 KB Output is correct
39 Correct 97 ms 41596 KB Output is correct
40 Correct 99 ms 40768 KB Output is correct
41 Correct 113 ms 41780 KB Output is correct
42 Correct 106 ms 41776 KB Output is correct
43 Correct 91 ms 44844 KB Output is correct
44 Correct 105 ms 41860 KB Output is correct
45 Correct 148 ms 49980 KB Output is correct
46 Correct 108 ms 41792 KB Output is correct
47 Correct 108 ms 41924 KB Output is correct
48 Correct 118 ms 44388 KB Output is correct
49 Correct 99 ms 52612 KB Output is correct
50 Correct 84 ms 47384 KB Output is correct
51 Correct 137 ms 47700 KB Output is correct
52 Correct 160 ms 49848 KB Output is correct
53 Correct 176 ms 50964 KB Output is correct
54 Correct 187 ms 54572 KB Output is correct
55 Correct 93 ms 44548 KB Output is correct
56 Correct 162 ms 52432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 32008 KB Output is correct
2 Correct 15 ms 31932 KB Output is correct
3 Correct 15 ms 31956 KB Output is correct
4 Correct 15 ms 32084 KB Output is correct
5 Correct 15 ms 32104 KB Output is correct
6 Correct 15 ms 32084 KB Output is correct
7 Correct 15 ms 32112 KB Output is correct
8 Correct 15 ms 32084 KB Output is correct
9 Correct 84 ms 39692 KB Output is correct
10 Correct 101 ms 41396 KB Output is correct
11 Correct 106 ms 41212 KB Output is correct
12 Correct 119 ms 41792 KB Output is correct
13 Correct 102 ms 44164 KB Output is correct
14 Correct 94 ms 39836 KB Output is correct
15 Correct 247 ms 43260 KB Output is correct
16 Correct 206 ms 42932 KB Output is correct
17 Correct 208 ms 43536 KB Output is correct
18 Correct 298 ms 45968 KB Output is correct
19 Correct 255 ms 47856 KB Output is correct
20 Correct 298 ms 48416 KB Output is correct
21 Correct 266 ms 48320 KB Output is correct
22 Correct 274 ms 48376 KB Output is correct
23 Correct 276 ms 48256 KB Output is correct
24 Correct 260 ms 50192 KB Output is correct
25 Correct 238 ms 50452 KB Output is correct
26 Correct 247 ms 50076 KB Output is correct
27 Correct 15 ms 32072 KB Output is correct
28 Correct 16 ms 32084 KB Output is correct
29 Correct 15 ms 32084 KB Output is correct
30 Correct 14 ms 32104 KB Output is correct
31 Correct 15 ms 32072 KB Output is correct
32 Correct 15 ms 32072 KB Output is correct
33 Correct 14 ms 32068 KB Output is correct
34 Correct 15 ms 32112 KB Output is correct
35 Correct 15 ms 32148 KB Output is correct
36 Correct 23 ms 33504 KB Output is correct
37 Correct 108 ms 41772 KB Output is correct
38 Correct 132 ms 41932 KB Output is correct
39 Correct 105 ms 41796 KB Output is correct
40 Correct 111 ms 41692 KB Output is correct
41 Correct 97 ms 41596 KB Output is correct
42 Correct 99 ms 40768 KB Output is correct
43 Correct 113 ms 41780 KB Output is correct
44 Correct 106 ms 41776 KB Output is correct
45 Correct 91 ms 44844 KB Output is correct
46 Correct 105 ms 41860 KB Output is correct
47 Correct 31 ms 33824 KB Output is correct
48 Correct 212 ms 45824 KB Output is correct
49 Correct 217 ms 45760 KB Output is correct
50 Correct 220 ms 45716 KB Output is correct
51 Correct 209 ms 45612 KB Output is correct
52 Correct 202 ms 45132 KB Output is correct
53 Correct 179 ms 43716 KB Output is correct
54 Correct 208 ms 46084 KB Output is correct
55 Correct 215 ms 45604 KB Output is correct
56 Correct 273 ms 47916 KB Output is correct
57 Correct 271 ms 46064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 31936 KB Output is correct
2 Correct 15 ms 32008 KB Output is correct
3 Correct 15 ms 31932 KB Output is correct
4 Correct 15 ms 31956 KB Output is correct
5 Correct 15 ms 32084 KB Output is correct
6 Correct 15 ms 32104 KB Output is correct
7 Correct 15 ms 32084 KB Output is correct
8 Correct 15 ms 32112 KB Output is correct
9 Correct 15 ms 32084 KB Output is correct
10 Correct 84 ms 39692 KB Output is correct
11 Correct 101 ms 41396 KB Output is correct
12 Correct 106 ms 41212 KB Output is correct
13 Correct 119 ms 41792 KB Output is correct
14 Correct 102 ms 44164 KB Output is correct
15 Correct 94 ms 39836 KB Output is correct
16 Correct 247 ms 43260 KB Output is correct
17 Correct 206 ms 42932 KB Output is correct
18 Correct 208 ms 43536 KB Output is correct
19 Correct 298 ms 45968 KB Output is correct
20 Correct 255 ms 47856 KB Output is correct
21 Correct 298 ms 48416 KB Output is correct
22 Correct 266 ms 48320 KB Output is correct
23 Correct 274 ms 48376 KB Output is correct
24 Correct 276 ms 48256 KB Output is correct
25 Correct 260 ms 50192 KB Output is correct
26 Correct 238 ms 50452 KB Output is correct
27 Correct 247 ms 50076 KB Output is correct
28 Correct 15 ms 32072 KB Output is correct
29 Correct 16 ms 32084 KB Output is correct
30 Correct 15 ms 32084 KB Output is correct
31 Correct 14 ms 32104 KB Output is correct
32 Correct 15 ms 32072 KB Output is correct
33 Correct 15 ms 32072 KB Output is correct
34 Correct 14 ms 32068 KB Output is correct
35 Correct 15 ms 32112 KB Output is correct
36 Correct 15 ms 32148 KB Output is correct
37 Correct 23 ms 33504 KB Output is correct
38 Correct 108 ms 41772 KB Output is correct
39 Correct 132 ms 41932 KB Output is correct
40 Correct 105 ms 41796 KB Output is correct
41 Correct 111 ms 41692 KB Output is correct
42 Correct 97 ms 41596 KB Output is correct
43 Correct 99 ms 40768 KB Output is correct
44 Correct 113 ms 41780 KB Output is correct
45 Correct 106 ms 41776 KB Output is correct
46 Correct 91 ms 44844 KB Output is correct
47 Correct 105 ms 41860 KB Output is correct
48 Correct 31 ms 33824 KB Output is correct
49 Correct 212 ms 45824 KB Output is correct
50 Correct 217 ms 45760 KB Output is correct
51 Correct 220 ms 45716 KB Output is correct
52 Correct 209 ms 45612 KB Output is correct
53 Correct 202 ms 45132 KB Output is correct
54 Correct 179 ms 43716 KB Output is correct
55 Correct 208 ms 46084 KB Output is correct
56 Correct 215 ms 45604 KB Output is correct
57 Correct 273 ms 47916 KB Output is correct
58 Correct 271 ms 46064 KB Output is correct
59 Correct 94 ms 36996 KB Output is correct
60 Correct 202 ms 43568 KB Output is correct
61 Correct 208 ms 43472 KB Output is correct
62 Correct 245 ms 43968 KB Output is correct
63 Correct 304 ms 46348 KB Output is correct
64 Correct 15 ms 32068 KB Output is correct
65 Correct 16 ms 32084 KB Output is correct
66 Correct 18 ms 32100 KB Output is correct
67 Correct 17 ms 32212 KB Output is correct
68 Correct 15 ms 32084 KB Output is correct
69 Correct 15 ms 32256 KB Output is correct
70 Correct 17 ms 32156 KB Output is correct
71 Correct 15 ms 32248 KB Output is correct
72 Correct 15 ms 32084 KB Output is correct
73 Correct 15 ms 32240 KB Output is correct
74 Correct 148 ms 49980 KB Output is correct
75 Correct 108 ms 41792 KB Output is correct
76 Correct 108 ms 41924 KB Output is correct
77 Correct 118 ms 44388 KB Output is correct
78 Correct 99 ms 52612 KB Output is correct
79 Correct 84 ms 47384 KB Output is correct
80 Correct 137 ms 47700 KB Output is correct
81 Correct 160 ms 49848 KB Output is correct
82 Correct 176 ms 50964 KB Output is correct
83 Correct 187 ms 54572 KB Output is correct
84 Correct 93 ms 44548 KB Output is correct
85 Correct 162 ms 52432 KB Output is correct
86 Correct 77 ms 39740 KB Output is correct
87 Correct 219 ms 45256 KB Output is correct
88 Correct 215 ms 45436 KB Output is correct
89 Correct 247 ms 47476 KB Output is correct
90 Correct 191 ms 56868 KB Output is correct
91 Correct 183 ms 54424 KB Output is correct
92 Correct 267 ms 50008 KB Output is correct
93 Correct 275 ms 52464 KB Output is correct
94 Correct 312 ms 54036 KB Output is correct
95 Correct 284 ms 57600 KB Output is correct
96 Correct 295 ms 47420 KB Output is correct
97 Correct 280 ms 50840 KB Output is correct