Submission #743695

# Submission time Handle Problem Language Result Execution time Memory
743695 2023-05-17T17:50:23 Z RushB Swapping Cities (APIO20_swap) C++17
100 / 100
447 ms 57076 KB
//happier ending for folks like us? 
//yani in AC nashe, yak yak kesaii ke in APIO dadan dast ma
//PAIN
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 15, M = 2e5 + 5, L = 21, INF = 0x3f3f3f3f;
int p[N], dpr[N], pr[N][L], ext[N], P[N], h[N], rt, D[N], deg[N];
vector<int> adj[N], Wtmp;
inline void smin(int &a, int b) {a = min(a, b);}
void dfs(int v) {
	for (int i = 1; i < L; i++) pr[v][i] = pr[pr[v][i - 1]][i - 1];
	for (auto u: adj[v]) {
		pr[u][0] = v;
		h[u] = h[v] + 1;
		dfs(u);
		smin(ext[v], ext[u]);
	}
}
int gpr(int u) {return dpr[u] == u ? u : dpr[u] = gpr(dpr[u]);}
bool merge(int u, int v, int id) {
	int tu = u, tv = v;
	deg[tu]++, deg[tv]++;
	u = gpr(u), v = gpr(v);
	if (u == v) return false;
	dpr[u] = dpr[v] = id + M;
	D[id + M] = max({D[u], D[v], deg[tu], deg[tv]});
	adj[id + M].push_back(u);
	adj[id + M].push_back(v);
	return true;
}
void init(int n, int m, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
	iota(p, p + m, 0);
	sort(p, p + m, [&](const int x, const int y) {return W[x] < W[y];});
	iota(dpr, dpr + N, 0);
	memset(ext, 0x3f, sizeof ext);
	Wtmp = W;
	for (int i = 0; i < m; i++) {
		const int id = p[i];
		if (!merge(U[id], V[id], id)) {
			smin(ext[U[id]], W[id]);
			smin(ext[V[id]], W[id]);
		}
	}
	rt = gpr(0), pr[rt][0] = rt, dfs(rt);
}
int LCA(int X, int Y) {
	for (int i = L - 1; i >= 0; i--) if (pr[Y][i] != pr[X][i]) Y = pr[Y][i], X = pr[X][i];
	return pr[X][0];
}
int getMinimumFuelCapacity(int X, int Y) {
	if (h[X] < h[Y]) swap(X, Y);
	while (h[Y] < h[X]) X = pr[X][__lg(h[X] - h[Y])];
	int w = INF, lca = LCA(X, Y);
	for (int i = L - 1; i >= 0; i--) if (pr[Y][i] != pr[X][i] || D[pr[X][i]] <= 2) Y = pr[Y][i], X = pr[X][i];
	smin(w, (D[pr[X][0]] > 2 ? Wtmp[pr[X][0] - M] : INF));
	if (h[X] <= h[lca]) smin(w, max(Wtmp[X - M], ext[X]));
	return w >= INF ? -1 : w;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 12884 KB Output is correct
2 Correct 7 ms 12768 KB Output is correct
3 Correct 7 ms 12756 KB Output is correct
4 Correct 8 ms 12904 KB Output is correct
5 Correct 8 ms 13080 KB Output is correct
6 Correct 8 ms 13012 KB Output is correct
7 Correct 8 ms 13056 KB Output is correct
8 Correct 8 ms 13168 KB Output is correct
9 Correct 102 ms 33012 KB Output is correct
10 Correct 124 ms 37104 KB Output is correct
11 Correct 123 ms 36728 KB Output is correct
12 Correct 155 ms 38156 KB Output is correct
13 Correct 150 ms 42048 KB Output is correct
14 Correct 120 ms 32948 KB Output is correct
15 Correct 252 ms 39608 KB Output is correct
16 Correct 270 ms 38896 KB Output is correct
17 Correct 266 ms 40392 KB Output is correct
18 Correct 381 ms 44312 KB Output is correct
19 Correct 103 ms 21096 KB Output is correct
20 Correct 279 ms 40040 KB Output is correct
21 Correct 253 ms 39508 KB Output is correct
22 Correct 272 ms 41268 KB Output is correct
23 Correct 423 ms 45104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 12884 KB Output is correct
2 Correct 7 ms 12768 KB Output is correct
3 Correct 409 ms 47444 KB Output is correct
4 Correct 367 ms 48660 KB Output is correct
5 Correct 409 ms 47872 KB Output is correct
6 Correct 377 ms 48636 KB Output is correct
7 Correct 379 ms 48440 KB Output is correct
8 Correct 423 ms 47040 KB Output is correct
9 Correct 376 ms 48056 KB Output is correct
10 Correct 410 ms 46676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 12884 KB Output is correct
2 Correct 7 ms 12768 KB Output is correct
3 Correct 7 ms 12756 KB Output is correct
4 Correct 8 ms 12904 KB Output is correct
5 Correct 8 ms 13080 KB Output is correct
6 Correct 8 ms 13012 KB Output is correct
7 Correct 8 ms 13056 KB Output is correct
8 Correct 8 ms 13168 KB Output is correct
9 Correct 7 ms 12852 KB Output is correct
10 Correct 7 ms 13012 KB Output is correct
11 Correct 8 ms 13136 KB Output is correct
12 Correct 7 ms 13040 KB Output is correct
13 Correct 9 ms 13012 KB Output is correct
14 Correct 9 ms 13040 KB Output is correct
15 Correct 8 ms 13044 KB Output is correct
16 Correct 8 ms 13096 KB Output is correct
17 Correct 8 ms 13140 KB Output is correct
18 Correct 8 ms 13044 KB Output is correct
19 Correct 7 ms 13012 KB Output is correct
20 Correct 7 ms 13012 KB Output is correct
21 Correct 9 ms 13140 KB Output is correct
22 Correct 8 ms 13044 KB Output is correct
23 Correct 8 ms 13012 KB Output is correct
24 Correct 10 ms 13268 KB Output is correct
25 Correct 9 ms 13168 KB Output is correct
26 Correct 9 ms 13268 KB Output is correct
27 Correct 8 ms 13176 KB Output is correct
28 Correct 9 ms 13268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 12852 KB Output is correct
2 Correct 7 ms 12884 KB Output is correct
3 Correct 7 ms 12768 KB Output is correct
4 Correct 7 ms 12756 KB Output is correct
5 Correct 8 ms 12904 KB Output is correct
6 Correct 8 ms 13080 KB Output is correct
7 Correct 8 ms 13012 KB Output is correct
8 Correct 8 ms 13056 KB Output is correct
9 Correct 8 ms 13168 KB Output is correct
10 Correct 102 ms 33012 KB Output is correct
11 Correct 124 ms 37104 KB Output is correct
12 Correct 123 ms 36728 KB Output is correct
13 Correct 155 ms 38156 KB Output is correct
14 Correct 150 ms 42048 KB Output is correct
15 Correct 7 ms 13012 KB Output is correct
16 Correct 8 ms 13136 KB Output is correct
17 Correct 7 ms 13040 KB Output is correct
18 Correct 9 ms 13012 KB Output is correct
19 Correct 9 ms 13040 KB Output is correct
20 Correct 8 ms 13044 KB Output is correct
21 Correct 8 ms 13096 KB Output is correct
22 Correct 8 ms 13140 KB Output is correct
23 Correct 8 ms 13044 KB Output is correct
24 Correct 7 ms 13012 KB Output is correct
25 Correct 7 ms 13012 KB Output is correct
26 Correct 9 ms 13140 KB Output is correct
27 Correct 8 ms 13044 KB Output is correct
28 Correct 8 ms 13012 KB Output is correct
29 Correct 10 ms 13268 KB Output is correct
30 Correct 9 ms 13168 KB Output is correct
31 Correct 9 ms 13268 KB Output is correct
32 Correct 8 ms 13176 KB Output is correct
33 Correct 9 ms 13268 KB Output is correct
34 Correct 20 ms 16372 KB Output is correct
35 Correct 137 ms 37616 KB Output is correct
36 Correct 139 ms 37600 KB Output is correct
37 Correct 159 ms 37512 KB Output is correct
38 Correct 131 ms 37380 KB Output is correct
39 Correct 136 ms 37088 KB Output is correct
40 Correct 124 ms 35160 KB Output is correct
41 Correct 136 ms 37648 KB Output is correct
42 Correct 149 ms 37692 KB Output is correct
43 Correct 160 ms 42828 KB Output is correct
44 Correct 141 ms 37700 KB Output is correct
45 Correct 172 ms 42640 KB Output is correct
46 Correct 132 ms 37624 KB Output is correct
47 Correct 140 ms 37708 KB Output is correct
48 Correct 163 ms 40616 KB Output is correct
49 Correct 75 ms 23500 KB Output is correct
50 Correct 66 ms 28612 KB Output is correct
51 Correct 141 ms 38288 KB Output is correct
52 Correct 191 ms 45016 KB Output is correct
53 Correct 195 ms 48188 KB Output is correct
54 Correct 198 ms 49776 KB Output is correct
55 Correct 154 ms 42220 KB Output is correct
56 Correct 215 ms 48728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 12884 KB Output is correct
2 Correct 7 ms 12768 KB Output is correct
3 Correct 7 ms 12756 KB Output is correct
4 Correct 8 ms 12904 KB Output is correct
5 Correct 8 ms 13080 KB Output is correct
6 Correct 8 ms 13012 KB Output is correct
7 Correct 8 ms 13056 KB Output is correct
8 Correct 8 ms 13168 KB Output is correct
9 Correct 102 ms 33012 KB Output is correct
10 Correct 124 ms 37104 KB Output is correct
11 Correct 123 ms 36728 KB Output is correct
12 Correct 155 ms 38156 KB Output is correct
13 Correct 150 ms 42048 KB Output is correct
14 Correct 120 ms 32948 KB Output is correct
15 Correct 252 ms 39608 KB Output is correct
16 Correct 270 ms 38896 KB Output is correct
17 Correct 266 ms 40392 KB Output is correct
18 Correct 381 ms 44312 KB Output is correct
19 Correct 409 ms 47444 KB Output is correct
20 Correct 367 ms 48660 KB Output is correct
21 Correct 409 ms 47872 KB Output is correct
22 Correct 377 ms 48636 KB Output is correct
23 Correct 379 ms 48440 KB Output is correct
24 Correct 423 ms 47040 KB Output is correct
25 Correct 376 ms 48056 KB Output is correct
26 Correct 410 ms 46676 KB Output is correct
27 Correct 7 ms 13012 KB Output is correct
28 Correct 8 ms 13136 KB Output is correct
29 Correct 7 ms 13040 KB Output is correct
30 Correct 9 ms 13012 KB Output is correct
31 Correct 9 ms 13040 KB Output is correct
32 Correct 8 ms 13044 KB Output is correct
33 Correct 8 ms 13096 KB Output is correct
34 Correct 8 ms 13140 KB Output is correct
35 Correct 8 ms 13044 KB Output is correct
36 Correct 20 ms 16372 KB Output is correct
37 Correct 137 ms 37616 KB Output is correct
38 Correct 139 ms 37600 KB Output is correct
39 Correct 159 ms 37512 KB Output is correct
40 Correct 131 ms 37380 KB Output is correct
41 Correct 136 ms 37088 KB Output is correct
42 Correct 124 ms 35160 KB Output is correct
43 Correct 136 ms 37648 KB Output is correct
44 Correct 149 ms 37692 KB Output is correct
45 Correct 160 ms 42828 KB Output is correct
46 Correct 141 ms 37700 KB Output is correct
47 Correct 29 ms 16404 KB Output is correct
48 Correct 256 ms 40396 KB Output is correct
49 Correct 278 ms 40504 KB Output is correct
50 Correct 265 ms 40396 KB Output is correct
51 Correct 311 ms 40260 KB Output is correct
52 Correct 263 ms 38872 KB Output is correct
53 Correct 241 ms 33316 KB Output is correct
54 Correct 294 ms 40984 KB Output is correct
55 Correct 296 ms 40372 KB Output is correct
56 Correct 447 ms 44432 KB Output is correct
57 Correct 296 ms 41088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 12852 KB Output is correct
2 Correct 7 ms 12884 KB Output is correct
3 Correct 7 ms 12768 KB Output is correct
4 Correct 7 ms 12756 KB Output is correct
5 Correct 8 ms 12904 KB Output is correct
6 Correct 8 ms 13080 KB Output is correct
7 Correct 8 ms 13012 KB Output is correct
8 Correct 8 ms 13056 KB Output is correct
9 Correct 8 ms 13168 KB Output is correct
10 Correct 102 ms 33012 KB Output is correct
11 Correct 124 ms 37104 KB Output is correct
12 Correct 123 ms 36728 KB Output is correct
13 Correct 155 ms 38156 KB Output is correct
14 Correct 150 ms 42048 KB Output is correct
15 Correct 120 ms 32948 KB Output is correct
16 Correct 252 ms 39608 KB Output is correct
17 Correct 270 ms 38896 KB Output is correct
18 Correct 266 ms 40392 KB Output is correct
19 Correct 381 ms 44312 KB Output is correct
20 Correct 409 ms 47444 KB Output is correct
21 Correct 367 ms 48660 KB Output is correct
22 Correct 409 ms 47872 KB Output is correct
23 Correct 377 ms 48636 KB Output is correct
24 Correct 379 ms 48440 KB Output is correct
25 Correct 423 ms 47040 KB Output is correct
26 Correct 376 ms 48056 KB Output is correct
27 Correct 410 ms 46676 KB Output is correct
28 Correct 7 ms 13012 KB Output is correct
29 Correct 8 ms 13136 KB Output is correct
30 Correct 7 ms 13040 KB Output is correct
31 Correct 9 ms 13012 KB Output is correct
32 Correct 9 ms 13040 KB Output is correct
33 Correct 8 ms 13044 KB Output is correct
34 Correct 8 ms 13096 KB Output is correct
35 Correct 8 ms 13140 KB Output is correct
36 Correct 8 ms 13044 KB Output is correct
37 Correct 20 ms 16372 KB Output is correct
38 Correct 137 ms 37616 KB Output is correct
39 Correct 139 ms 37600 KB Output is correct
40 Correct 159 ms 37512 KB Output is correct
41 Correct 131 ms 37380 KB Output is correct
42 Correct 136 ms 37088 KB Output is correct
43 Correct 124 ms 35160 KB Output is correct
44 Correct 136 ms 37648 KB Output is correct
45 Correct 149 ms 37692 KB Output is correct
46 Correct 160 ms 42828 KB Output is correct
47 Correct 141 ms 37700 KB Output is correct
48 Correct 29 ms 16404 KB Output is correct
49 Correct 256 ms 40396 KB Output is correct
50 Correct 278 ms 40504 KB Output is correct
51 Correct 265 ms 40396 KB Output is correct
52 Correct 311 ms 40260 KB Output is correct
53 Correct 263 ms 38872 KB Output is correct
54 Correct 241 ms 33316 KB Output is correct
55 Correct 294 ms 40984 KB Output is correct
56 Correct 296 ms 40372 KB Output is correct
57 Correct 447 ms 44432 KB Output is correct
58 Correct 296 ms 41088 KB Output is correct
59 Correct 103 ms 21096 KB Output is correct
60 Correct 279 ms 40040 KB Output is correct
61 Correct 253 ms 39508 KB Output is correct
62 Correct 272 ms 41268 KB Output is correct
63 Correct 423 ms 45104 KB Output is correct
64 Correct 7 ms 13012 KB Output is correct
65 Correct 7 ms 13012 KB Output is correct
66 Correct 9 ms 13140 KB Output is correct
67 Correct 8 ms 13044 KB Output is correct
68 Correct 8 ms 13012 KB Output is correct
69 Correct 10 ms 13268 KB Output is correct
70 Correct 9 ms 13168 KB Output is correct
71 Correct 9 ms 13268 KB Output is correct
72 Correct 8 ms 13176 KB Output is correct
73 Correct 9 ms 13268 KB Output is correct
74 Correct 172 ms 42640 KB Output is correct
75 Correct 132 ms 37624 KB Output is correct
76 Correct 140 ms 37708 KB Output is correct
77 Correct 163 ms 40616 KB Output is correct
78 Correct 75 ms 23500 KB Output is correct
79 Correct 66 ms 28612 KB Output is correct
80 Correct 141 ms 38288 KB Output is correct
81 Correct 191 ms 45016 KB Output is correct
82 Correct 195 ms 48188 KB Output is correct
83 Correct 198 ms 49776 KB Output is correct
84 Correct 154 ms 42220 KB Output is correct
85 Correct 215 ms 48728 KB Output is correct
86 Correct 96 ms 25508 KB Output is correct
87 Correct 281 ms 43864 KB Output is correct
88 Correct 265 ms 43740 KB Output is correct
89 Correct 384 ms 45388 KB Output is correct
90 Correct 184 ms 32076 KB Output is correct
91 Correct 238 ms 39628 KB Output is correct
92 Correct 374 ms 44744 KB Output is correct
93 Correct 348 ms 52328 KB Output is correct
94 Correct 422 ms 55472 KB Output is correct
95 Correct 329 ms 57076 KB Output is correct
96 Correct 425 ms 48148 KB Output is correct
97 Correct 419 ms 51376 KB Output is correct