Submission #403113

# Submission time Handle Problem Language Result Execution time Memory
403113 2021-05-12T18:59:51 Z saarang123 Swapping Cities (APIO20_swap) C++17
100 / 100
407 ms 60688 KB
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;

const int MXN = 100 * 1000 +3;
const int MXM = 200 * 1000 + 3;
const int LGN = 19;

int par[MXN + MXM], deg[MXN + MXM], cost[MXN + MXM], up[MXN + MXM][LGN], dt[MXN + MXM];
bool valid[MXN + MXM];
vector<int> g[MXN + MXM];
vector<array<int, 3>> ed;
int node;

void dfs(int v = node - 1, int p = node - 1) {
	up[v][0] = p;
	for(int i = 1; i < LGN; i++)
		up[v][i] = up[up[v][i - 1]][i - 1];
	for(int u : g[v]) if(u != p) {
		dt[u] = dt[v] + 1;
		dfs(u, v);
	}
}

int kth(int u, int difference) {
	for(int i = 0; i < LGN; i++)
		if(difference >> i & 1)
			u = up[u][i];
	return u;
}

int LCA(int u, int v) {
	if(dt[u] > dt[v])
		swap(u, v);
	v = kth(v, dt[v] - dt[u]);
	if(u == v)
		return u;

	for(int i = LGN - 1; i >= 0; i--)
		if(up[u][i] != up[v][i])
			u = up[u][i], v = up[v][i];
	return up[u][0];
}

int find(int v) {
	return par[v] == v ? v : par[v] = find(par[v]);
}

void unite(int u, int v, int w) {
	deg[u]++; deg[v]++;
	valid[node] = deg[u] >= 3 || deg[v] >= 3;

	u = find(u); v = find(v);
	g[node].push_back(u);

	if(u == v) {
		valid[node] = true;
		par[u] = node;
		cost[node++] = w;
		return;
	}

	g[node].push_back(v);
	par[v] = par[u] = node;
	cost[node] = w;
	valid[node] = valid[node] | valid[u] | valid[v];
	node++;
}

int query(int u) {
	if(valid[u])
		return cost[u];
	for(int i = LGN - 1; i >= 0; i--)
		if(!valid[up[u][i]])
			u = up[u][i];
	return valid[up[u][0]] ? cost[up[u][0]] : -1;
}

void init(int N, int M, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
	for(int i = 0; i < M; i++)
		ed.push_back({W[i], U[i], V[i]});
	sort(ed.begin(), ed.end());
	iota(par, par + N + M, 0);
	node = N;

	for(auto &[w, u, v] : ed)
		unite(u, v, w);

	dfs();
}


int getMinimumFuelCapacity(int X, int Y) {
	return query(LCA(X, Y));
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 5 ms 7352 KB Output is correct
3 Correct 5 ms 7372 KB Output is correct
4 Correct 5 ms 7500 KB Output is correct
5 Correct 6 ms 7488 KB Output is correct
6 Correct 5 ms 7500 KB Output is correct
7 Correct 6 ms 7616 KB Output is correct
8 Correct 6 ms 7628 KB Output is correct
9 Correct 102 ms 27864 KB Output is correct
10 Correct 125 ms 32428 KB Output is correct
11 Correct 125 ms 32016 KB Output is correct
12 Correct 151 ms 33456 KB Output is correct
13 Correct 116 ms 36608 KB Output is correct
14 Correct 123 ms 28084 KB Output is correct
15 Correct 279 ms 36616 KB Output is correct
16 Correct 274 ms 35996 KB Output is correct
17 Correct 289 ms 37440 KB Output is correct
18 Correct 366 ms 40616 KB Output is correct
19 Correct 110 ms 16568 KB Output is correct
20 Correct 274 ms 37784 KB Output is correct
21 Correct 281 ms 37172 KB Output is correct
22 Correct 297 ms 38860 KB Output is correct
23 Correct 399 ms 42008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 5 ms 7352 KB Output is correct
3 Correct 333 ms 42724 KB Output is correct
4 Correct 320 ms 44196 KB Output is correct
5 Correct 378 ms 43540 KB Output is correct
6 Correct 298 ms 43984 KB Output is correct
7 Correct 328 ms 43992 KB Output is correct
8 Correct 303 ms 42596 KB Output is correct
9 Correct 301 ms 43576 KB Output is correct
10 Correct 291 ms 42268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 5 ms 7352 KB Output is correct
3 Correct 5 ms 7372 KB Output is correct
4 Correct 5 ms 7500 KB Output is correct
5 Correct 6 ms 7488 KB Output is correct
6 Correct 5 ms 7500 KB Output is correct
7 Correct 6 ms 7616 KB Output is correct
8 Correct 6 ms 7628 KB Output is correct
9 Correct 5 ms 7372 KB Output is correct
10 Correct 7 ms 7620 KB Output is correct
11 Correct 7 ms 7744 KB Output is correct
12 Correct 7 ms 7620 KB Output is correct
13 Correct 6 ms 7500 KB Output is correct
14 Correct 7 ms 7568 KB Output is correct
15 Correct 5 ms 7628 KB Output is correct
16 Correct 5 ms 7628 KB Output is correct
17 Correct 6 ms 7628 KB Output is correct
18 Correct 6 ms 7616 KB Output is correct
19 Correct 6 ms 7628 KB Output is correct
20 Correct 6 ms 7640 KB Output is correct
21 Correct 7 ms 7628 KB Output is correct
22 Correct 8 ms 7744 KB Output is correct
23 Correct 5 ms 7500 KB Output is correct
24 Correct 6 ms 7756 KB Output is correct
25 Correct 6 ms 7756 KB Output is correct
26 Correct 7 ms 7756 KB Output is correct
27 Correct 6 ms 7616 KB Output is correct
28 Correct 8 ms 7884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 5 ms 7372 KB Output is correct
3 Correct 5 ms 7352 KB Output is correct
4 Correct 5 ms 7372 KB Output is correct
5 Correct 5 ms 7500 KB Output is correct
6 Correct 6 ms 7488 KB Output is correct
7 Correct 5 ms 7500 KB Output is correct
8 Correct 6 ms 7616 KB Output is correct
9 Correct 6 ms 7628 KB Output is correct
10 Correct 102 ms 27864 KB Output is correct
11 Correct 125 ms 32428 KB Output is correct
12 Correct 125 ms 32016 KB Output is correct
13 Correct 151 ms 33456 KB Output is correct
14 Correct 116 ms 36608 KB Output is correct
15 Correct 7 ms 7620 KB Output is correct
16 Correct 7 ms 7744 KB Output is correct
17 Correct 7 ms 7620 KB Output is correct
18 Correct 6 ms 7500 KB Output is correct
19 Correct 7 ms 7568 KB Output is correct
20 Correct 5 ms 7628 KB Output is correct
21 Correct 5 ms 7628 KB Output is correct
22 Correct 6 ms 7628 KB Output is correct
23 Correct 6 ms 7616 KB Output is correct
24 Correct 6 ms 7628 KB Output is correct
25 Correct 6 ms 7640 KB Output is correct
26 Correct 7 ms 7628 KB Output is correct
27 Correct 8 ms 7744 KB Output is correct
28 Correct 5 ms 7500 KB Output is correct
29 Correct 6 ms 7756 KB Output is correct
30 Correct 6 ms 7756 KB Output is correct
31 Correct 7 ms 7756 KB Output is correct
32 Correct 6 ms 7616 KB Output is correct
33 Correct 8 ms 7884 KB Output is correct
34 Correct 18 ms 10884 KB Output is correct
35 Correct 134 ms 33180 KB Output is correct
36 Correct 163 ms 33088 KB Output is correct
37 Correct 142 ms 33152 KB Output is correct
38 Correct 131 ms 32696 KB Output is correct
39 Correct 137 ms 32668 KB Output is correct
40 Correct 125 ms 30524 KB Output is correct
41 Correct 134 ms 33468 KB Output is correct
42 Correct 137 ms 33344 KB Output is correct
43 Correct 120 ms 37300 KB Output is correct
44 Correct 138 ms 33536 KB Output is correct
45 Correct 181 ms 45552 KB Output is correct
46 Correct 138 ms 33080 KB Output is correct
47 Correct 137 ms 33200 KB Output is correct
48 Correct 164 ms 37240 KB Output is correct
49 Correct 127 ms 44204 KB Output is correct
50 Correct 104 ms 36064 KB Output is correct
51 Correct 146 ms 40628 KB Output is correct
52 Correct 210 ms 47996 KB Output is correct
53 Correct 227 ms 50888 KB Output is correct
54 Correct 228 ms 56904 KB Output is correct
55 Correct 124 ms 36924 KB Output is correct
56 Correct 210 ms 52336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 5 ms 7352 KB Output is correct
3 Correct 5 ms 7372 KB Output is correct
4 Correct 5 ms 7500 KB Output is correct
5 Correct 6 ms 7488 KB Output is correct
6 Correct 5 ms 7500 KB Output is correct
7 Correct 6 ms 7616 KB Output is correct
8 Correct 6 ms 7628 KB Output is correct
9 Correct 102 ms 27864 KB Output is correct
10 Correct 125 ms 32428 KB Output is correct
11 Correct 125 ms 32016 KB Output is correct
12 Correct 151 ms 33456 KB Output is correct
13 Correct 116 ms 36608 KB Output is correct
14 Correct 123 ms 28084 KB Output is correct
15 Correct 279 ms 36616 KB Output is correct
16 Correct 274 ms 35996 KB Output is correct
17 Correct 289 ms 37440 KB Output is correct
18 Correct 366 ms 40616 KB Output is correct
19 Correct 333 ms 42724 KB Output is correct
20 Correct 320 ms 44196 KB Output is correct
21 Correct 378 ms 43540 KB Output is correct
22 Correct 298 ms 43984 KB Output is correct
23 Correct 328 ms 43992 KB Output is correct
24 Correct 303 ms 42596 KB Output is correct
25 Correct 301 ms 43576 KB Output is correct
26 Correct 291 ms 42268 KB Output is correct
27 Correct 7 ms 7620 KB Output is correct
28 Correct 7 ms 7744 KB Output is correct
29 Correct 7 ms 7620 KB Output is correct
30 Correct 6 ms 7500 KB Output is correct
31 Correct 7 ms 7568 KB Output is correct
32 Correct 5 ms 7628 KB Output is correct
33 Correct 5 ms 7628 KB Output is correct
34 Correct 6 ms 7628 KB Output is correct
35 Correct 6 ms 7616 KB Output is correct
36 Correct 18 ms 10884 KB Output is correct
37 Correct 134 ms 33180 KB Output is correct
38 Correct 163 ms 33088 KB Output is correct
39 Correct 142 ms 33152 KB Output is correct
40 Correct 131 ms 32696 KB Output is correct
41 Correct 137 ms 32668 KB Output is correct
42 Correct 125 ms 30524 KB Output is correct
43 Correct 134 ms 33468 KB Output is correct
44 Correct 137 ms 33344 KB Output is correct
45 Correct 120 ms 37300 KB Output is correct
46 Correct 138 ms 33536 KB Output is correct
47 Correct 28 ms 11108 KB Output is correct
48 Correct 279 ms 38064 KB Output is correct
49 Correct 288 ms 38052 KB Output is correct
50 Correct 271 ms 37920 KB Output is correct
51 Correct 306 ms 37836 KB Output is correct
52 Correct 287 ms 36412 KB Output is correct
53 Correct 233 ms 30508 KB Output is correct
54 Correct 294 ms 38932 KB Output is correct
55 Correct 286 ms 37964 KB Output is correct
56 Correct 376 ms 41356 KB Output is correct
57 Correct 304 ms 38964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 5 ms 7372 KB Output is correct
3 Correct 5 ms 7352 KB Output is correct
4 Correct 5 ms 7372 KB Output is correct
5 Correct 5 ms 7500 KB Output is correct
6 Correct 6 ms 7488 KB Output is correct
7 Correct 5 ms 7500 KB Output is correct
8 Correct 6 ms 7616 KB Output is correct
9 Correct 6 ms 7628 KB Output is correct
10 Correct 102 ms 27864 KB Output is correct
11 Correct 125 ms 32428 KB Output is correct
12 Correct 125 ms 32016 KB Output is correct
13 Correct 151 ms 33456 KB Output is correct
14 Correct 116 ms 36608 KB Output is correct
15 Correct 123 ms 28084 KB Output is correct
16 Correct 279 ms 36616 KB Output is correct
17 Correct 274 ms 35996 KB Output is correct
18 Correct 289 ms 37440 KB Output is correct
19 Correct 366 ms 40616 KB Output is correct
20 Correct 333 ms 42724 KB Output is correct
21 Correct 320 ms 44196 KB Output is correct
22 Correct 378 ms 43540 KB Output is correct
23 Correct 298 ms 43984 KB Output is correct
24 Correct 328 ms 43992 KB Output is correct
25 Correct 303 ms 42596 KB Output is correct
26 Correct 301 ms 43576 KB Output is correct
27 Correct 291 ms 42268 KB Output is correct
28 Correct 7 ms 7620 KB Output is correct
29 Correct 7 ms 7744 KB Output is correct
30 Correct 7 ms 7620 KB Output is correct
31 Correct 6 ms 7500 KB Output is correct
32 Correct 7 ms 7568 KB Output is correct
33 Correct 5 ms 7628 KB Output is correct
34 Correct 5 ms 7628 KB Output is correct
35 Correct 6 ms 7628 KB Output is correct
36 Correct 6 ms 7616 KB Output is correct
37 Correct 18 ms 10884 KB Output is correct
38 Correct 134 ms 33180 KB Output is correct
39 Correct 163 ms 33088 KB Output is correct
40 Correct 142 ms 33152 KB Output is correct
41 Correct 131 ms 32696 KB Output is correct
42 Correct 137 ms 32668 KB Output is correct
43 Correct 125 ms 30524 KB Output is correct
44 Correct 134 ms 33468 KB Output is correct
45 Correct 137 ms 33344 KB Output is correct
46 Correct 120 ms 37300 KB Output is correct
47 Correct 138 ms 33536 KB Output is correct
48 Correct 28 ms 11108 KB Output is correct
49 Correct 279 ms 38064 KB Output is correct
50 Correct 288 ms 38052 KB Output is correct
51 Correct 271 ms 37920 KB Output is correct
52 Correct 306 ms 37836 KB Output is correct
53 Correct 287 ms 36412 KB Output is correct
54 Correct 233 ms 30508 KB Output is correct
55 Correct 294 ms 38932 KB Output is correct
56 Correct 286 ms 37964 KB Output is correct
57 Correct 376 ms 41356 KB Output is correct
58 Correct 304 ms 38964 KB Output is correct
59 Correct 110 ms 16568 KB Output is correct
60 Correct 274 ms 37784 KB Output is correct
61 Correct 281 ms 37172 KB Output is correct
62 Correct 297 ms 38860 KB Output is correct
63 Correct 399 ms 42008 KB Output is correct
64 Correct 6 ms 7628 KB Output is correct
65 Correct 6 ms 7640 KB Output is correct
66 Correct 7 ms 7628 KB Output is correct
67 Correct 8 ms 7744 KB Output is correct
68 Correct 5 ms 7500 KB Output is correct
69 Correct 6 ms 7756 KB Output is correct
70 Correct 6 ms 7756 KB Output is correct
71 Correct 7 ms 7756 KB Output is correct
72 Correct 6 ms 7616 KB Output is correct
73 Correct 8 ms 7884 KB Output is correct
74 Correct 181 ms 45552 KB Output is correct
75 Correct 138 ms 33080 KB Output is correct
76 Correct 137 ms 33200 KB Output is correct
77 Correct 164 ms 37240 KB Output is correct
78 Correct 127 ms 44204 KB Output is correct
79 Correct 104 ms 36064 KB Output is correct
80 Correct 146 ms 40628 KB Output is correct
81 Correct 210 ms 47996 KB Output is correct
82 Correct 227 ms 50888 KB Output is correct
83 Correct 228 ms 56904 KB Output is correct
84 Correct 124 ms 36924 KB Output is correct
85 Correct 210 ms 52336 KB Output is correct
86 Correct 87 ms 21540 KB Output is correct
87 Correct 270 ms 38044 KB Output is correct
88 Correct 279 ms 37952 KB Output is correct
89 Correct 313 ms 40700 KB Output is correct
90 Correct 229 ms 50440 KB Output is correct
91 Correct 238 ms 47244 KB Output is correct
92 Correct 355 ms 44308 KB Output is correct
93 Correct 339 ms 52128 KB Output is correct
94 Correct 407 ms 55248 KB Output is correct
95 Correct 391 ms 60688 KB Output is correct
96 Correct 341 ms 41636 KB Output is correct
97 Correct 346 ms 48524 KB Output is correct