Submission #288238

# Submission time Handle Problem Language Result Execution time Memory
288238 2020-09-01T10:44:50 Z admin Swapping Cities (APIO20_swap) C++17
100 / 100
718 ms 52464 KB
// https://raw.githubusercontent.com/koosaga/olympiad/master/APIO/apio20_swap.cpp

#include "swap.h"
#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
using namespace std;
using lint = long long;
using pi = pair<int, int>;
const int MAXN = 200005;
const int mod = 1e9 + 7;

struct disj{
	struct node{
		int pa, max_deg, cycle, updated;
		bool operator<(const node &n)const{
			return updated < n.updated;
		}
	};
	// persistent
	vector<node> nodes[MAXN];

	// only used in creation
	vector<int> memb[MAXN];
	int deg[MAXN];
	void init(int n){
		for(int i=0; i<n; i++){
			memb[i].push_back(i);
			nodes[i].push_back({i, 0, 0, -1});
		}
	}
	int find(int x){
		return nodes[x].back().pa;
	}
	void add_edge(int x, int y, int i){
		deg[x]++; deg[y]++;
		int new_deg = max(deg[x], deg[y]);
		x = find(x); y = find(y);
		if(x != y){
			if(sz(memb[x]) < sz(memb[y])) swap(x, y);
			for(auto &v : memb[y]){
				node prv = nodes[v].back();
				prv.pa = x;
				prv.updated = i;
				nodes[v].push_back(prv);
				memb[x].push_back(v);
			}
			node prv = nodes[x].back();
			prv.max_deg = max(prv.max_deg, nodes[y].back().max_deg);
			prv.cycle |= nodes[y].back().cycle;
			prv.updated = i;
			nodes[x].push_back(prv);
			memb[y].clear();
		}
		node prv = nodes[x].back();
		if(x == y) prv.cycle = 1;
		prv.max_deg = max(prv.max_deg, new_deg);
		prv.updated = i;
		nodes[x].push_back(prv);
	}
	node get_node(int x, int t){
		return *--upper_bound(all(nodes[x]), (node){-1, -1, 0, t});
	}
}disj;

bool isGood(int u, int v, int m){
	auto gnx = disj.get_node(u, m);
	auto gny = disj.get_node(v, m);
	if(gnx.pa != gny.pa) return 0;
	auto val = disj.get_node(gnx.pa, m);
	return val.max_deg >= 3 || val.cycle;
}

int n, m;

struct edg{
	int s, e, x;
}a[MAXN];

void init(int N, int M,
		std::vector<int> U, std::vector<int> V, std::vector<int> W) {
	n = N; m = M;
	disj.init(n);
	for(int i=0; i<M; i++) a[i] = {U[i], V[i], W[i]};
	sort(a, a + M, [&](const edg &a, const edg &b){
		return a.x < b.x;
	});
	for(int i=0; i<M; i++) disj.add_edge(a[i].s, a[i].e, i);
}

int getMinimumFuelCapacity(int X, int Y) {
	int s = 0, e = m;
	while(s != e){
		int m = (s + e) / 2;
		if(isGood(X, Y, m)) e = m;
		else s = m + 1;
	}
	if(s == m) return -1;
	return a[s].x;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 7 ms 9728 KB Output is correct
4 Correct 8 ms 9984 KB Output is correct
5 Correct 8 ms 9984 KB Output is correct
6 Correct 8 ms 9984 KB Output is correct
7 Correct 9 ms 10112 KB Output is correct
8 Correct 7 ms 9984 KB Output is correct
9 Correct 218 ms 36716 KB Output is correct
10 Correct 267 ms 42096 KB Output is correct
11 Correct 263 ms 41452 KB Output is correct
12 Correct 287 ms 43632 KB Output is correct
13 Correct 144 ms 30808 KB Output is correct
14 Correct 238 ms 36508 KB Output is correct
15 Correct 515 ms 45052 KB Output is correct
16 Correct 516 ms 43936 KB Output is correct
17 Correct 526 ms 46052 KB Output is correct
18 Correct 346 ms 32704 KB Output is correct
19 Correct 180 ms 18168 KB Output is correct
20 Correct 501 ms 46060 KB Output is correct
21 Correct 511 ms 45008 KB Output is correct
22 Correct 534 ms 47316 KB Output is correct
23 Correct 348 ms 34112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 448 ms 30536 KB Output is correct
4 Correct 450 ms 31048 KB Output is correct
5 Correct 463 ms 30836 KB Output is correct
6 Correct 443 ms 30896 KB Output is correct
7 Correct 465 ms 31084 KB Output is correct
8 Correct 456 ms 30236 KB Output is correct
9 Correct 437 ms 30764 KB Output is correct
10 Correct 449 ms 29980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 7 ms 9728 KB Output is correct
4 Correct 7 ms 9728 KB Output is correct
5 Correct 8 ms 9984 KB Output is correct
6 Correct 8 ms 9984 KB Output is correct
7 Correct 8 ms 9984 KB Output is correct
8 Correct 9 ms 10112 KB Output is correct
9 Correct 7 ms 9984 KB Output is correct
10 Correct 8 ms 9984 KB Output is correct
11 Correct 8 ms 9984 KB Output is correct
12 Correct 8 ms 9984 KB Output is correct
13 Correct 9 ms 9984 KB Output is correct
14 Correct 8 ms 9984 KB Output is correct
15 Correct 8 ms 9984 KB Output is correct
16 Correct 8 ms 9984 KB Output is correct
17 Correct 8 ms 9984 KB Output is correct
18 Correct 9 ms 9984 KB Output is correct
19 Correct 8 ms 9984 KB Output is correct
20 Correct 8 ms 9984 KB Output is correct
21 Correct 8 ms 9984 KB Output is correct
22 Correct 8 ms 9856 KB Output is correct
23 Correct 8 ms 9856 KB Output is correct
24 Correct 8 ms 9984 KB Output is correct
25 Correct 8 ms 9984 KB Output is correct
26 Correct 8 ms 10112 KB Output is correct
27 Correct 8 ms 9984 KB Output is correct
28 Correct 9 ms 9984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 7 ms 9728 KB Output is correct
4 Correct 7 ms 9728 KB Output is correct
5 Correct 8 ms 9984 KB Output is correct
6 Correct 8 ms 9984 KB Output is correct
7 Correct 8 ms 9984 KB Output is correct
8 Correct 9 ms 10112 KB Output is correct
9 Correct 7 ms 9984 KB Output is correct
10 Correct 218 ms 36716 KB Output is correct
11 Correct 267 ms 42096 KB Output is correct
12 Correct 263 ms 41452 KB Output is correct
13 Correct 287 ms 43632 KB Output is correct
14 Correct 144 ms 30808 KB Output is correct
15 Correct 8 ms 9984 KB Output is correct
16 Correct 8 ms 9984 KB Output is correct
17 Correct 8 ms 9984 KB Output is correct
18 Correct 9 ms 9984 KB Output is correct
19 Correct 8 ms 9984 KB Output is correct
20 Correct 8 ms 9984 KB Output is correct
21 Correct 8 ms 9984 KB Output is correct
22 Correct 8 ms 9984 KB Output is correct
23 Correct 9 ms 9984 KB Output is correct
24 Correct 32 ms 13560 KB Output is correct
25 Correct 301 ms 43376 KB Output is correct
26 Correct 295 ms 42740 KB Output is correct
27 Correct 272 ms 41712 KB Output is correct
28 Correct 273 ms 40164 KB Output is correct
29 Correct 284 ms 39536 KB Output is correct
30 Correct 228 ms 36336 KB Output is correct
31 Correct 306 ms 44528 KB Output is correct
32 Correct 278 ms 44396 KB Output is correct
33 Correct 146 ms 30312 KB Output is correct
34 Correct 250 ms 39152 KB Output is correct
35 Correct 8 ms 9984 KB Output is correct
36 Correct 8 ms 9984 KB Output is correct
37 Correct 8 ms 9984 KB Output is correct
38 Correct 8 ms 9856 KB Output is correct
39 Correct 8 ms 9856 KB Output is correct
40 Correct 8 ms 9984 KB Output is correct
41 Correct 8 ms 9984 KB Output is correct
42 Correct 8 ms 10112 KB Output is correct
43 Correct 8 ms 9984 KB Output is correct
44 Correct 9 ms 9984 KB Output is correct
45 Correct 203 ms 29976 KB Output is correct
46 Correct 278 ms 42864 KB Output is correct
47 Correct 241 ms 36980 KB Output is correct
48 Correct 216 ms 31460 KB Output is correct
49 Correct 95 ms 19688 KB Output is correct
50 Correct 82 ms 17328 KB Output is correct
51 Correct 160 ms 26860 KB Output is correct
52 Correct 319 ms 44432 KB Output is correct
53 Correct 289 ms 38536 KB Output is correct
54 Correct 377 ms 50032 KB Output is correct
55 Correct 151 ms 30388 KB Output is correct
56 Correct 268 ms 36704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 7 ms 9728 KB Output is correct
4 Correct 8 ms 9984 KB Output is correct
5 Correct 8 ms 9984 KB Output is correct
6 Correct 8 ms 9984 KB Output is correct
7 Correct 9 ms 10112 KB Output is correct
8 Correct 7 ms 9984 KB Output is correct
9 Correct 218 ms 36716 KB Output is correct
10 Correct 267 ms 42096 KB Output is correct
11 Correct 263 ms 41452 KB Output is correct
12 Correct 287 ms 43632 KB Output is correct
13 Correct 144 ms 30808 KB Output is correct
14 Correct 238 ms 36508 KB Output is correct
15 Correct 515 ms 45052 KB Output is correct
16 Correct 516 ms 43936 KB Output is correct
17 Correct 526 ms 46052 KB Output is correct
18 Correct 346 ms 32704 KB Output is correct
19 Correct 448 ms 30536 KB Output is correct
20 Correct 450 ms 31048 KB Output is correct
21 Correct 463 ms 30836 KB Output is correct
22 Correct 443 ms 30896 KB Output is correct
23 Correct 465 ms 31084 KB Output is correct
24 Correct 456 ms 30236 KB Output is correct
25 Correct 437 ms 30764 KB Output is correct
26 Correct 449 ms 29980 KB Output is correct
27 Correct 8 ms 9984 KB Output is correct
28 Correct 8 ms 9984 KB Output is correct
29 Correct 8 ms 9984 KB Output is correct
30 Correct 9 ms 9984 KB Output is correct
31 Correct 8 ms 9984 KB Output is correct
32 Correct 8 ms 9984 KB Output is correct
33 Correct 8 ms 9984 KB Output is correct
34 Correct 8 ms 9984 KB Output is correct
35 Correct 9 ms 9984 KB Output is correct
36 Correct 32 ms 13560 KB Output is correct
37 Correct 301 ms 43376 KB Output is correct
38 Correct 295 ms 42740 KB Output is correct
39 Correct 272 ms 41712 KB Output is correct
40 Correct 273 ms 40164 KB Output is correct
41 Correct 284 ms 39536 KB Output is correct
42 Correct 228 ms 36336 KB Output is correct
43 Correct 306 ms 44528 KB Output is correct
44 Correct 278 ms 44396 KB Output is correct
45 Correct 146 ms 30312 KB Output is correct
46 Correct 250 ms 39152 KB Output is correct
47 Correct 47 ms 13560 KB Output is correct
48 Correct 539 ms 46124 KB Output is correct
49 Correct 530 ms 44760 KB Output is correct
50 Correct 538 ms 44504 KB Output is correct
51 Correct 514 ms 43492 KB Output is correct
52 Correct 499 ms 40736 KB Output is correct
53 Correct 416 ms 32380 KB Output is correct
54 Correct 547 ms 46552 KB Output is correct
55 Correct 583 ms 47460 KB Output is correct
56 Correct 425 ms 33348 KB Output is correct
57 Correct 567 ms 41936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 7 ms 9728 KB Output is correct
4 Correct 7 ms 9728 KB Output is correct
5 Correct 8 ms 9984 KB Output is correct
6 Correct 8 ms 9984 KB Output is correct
7 Correct 8 ms 9984 KB Output is correct
8 Correct 9 ms 10112 KB Output is correct
9 Correct 7 ms 9984 KB Output is correct
10 Correct 218 ms 36716 KB Output is correct
11 Correct 267 ms 42096 KB Output is correct
12 Correct 263 ms 41452 KB Output is correct
13 Correct 287 ms 43632 KB Output is correct
14 Correct 144 ms 30808 KB Output is correct
15 Correct 238 ms 36508 KB Output is correct
16 Correct 515 ms 45052 KB Output is correct
17 Correct 516 ms 43936 KB Output is correct
18 Correct 526 ms 46052 KB Output is correct
19 Correct 346 ms 32704 KB Output is correct
20 Correct 448 ms 30536 KB Output is correct
21 Correct 450 ms 31048 KB Output is correct
22 Correct 463 ms 30836 KB Output is correct
23 Correct 443 ms 30896 KB Output is correct
24 Correct 465 ms 31084 KB Output is correct
25 Correct 456 ms 30236 KB Output is correct
26 Correct 437 ms 30764 KB Output is correct
27 Correct 449 ms 29980 KB Output is correct
28 Correct 8 ms 9984 KB Output is correct
29 Correct 8 ms 9984 KB Output is correct
30 Correct 8 ms 9984 KB Output is correct
31 Correct 9 ms 9984 KB Output is correct
32 Correct 8 ms 9984 KB Output is correct
33 Correct 8 ms 9984 KB Output is correct
34 Correct 8 ms 9984 KB Output is correct
35 Correct 8 ms 9984 KB Output is correct
36 Correct 9 ms 9984 KB Output is correct
37 Correct 32 ms 13560 KB Output is correct
38 Correct 301 ms 43376 KB Output is correct
39 Correct 295 ms 42740 KB Output is correct
40 Correct 272 ms 41712 KB Output is correct
41 Correct 273 ms 40164 KB Output is correct
42 Correct 284 ms 39536 KB Output is correct
43 Correct 228 ms 36336 KB Output is correct
44 Correct 306 ms 44528 KB Output is correct
45 Correct 278 ms 44396 KB Output is correct
46 Correct 146 ms 30312 KB Output is correct
47 Correct 250 ms 39152 KB Output is correct
48 Correct 47 ms 13560 KB Output is correct
49 Correct 539 ms 46124 KB Output is correct
50 Correct 530 ms 44760 KB Output is correct
51 Correct 538 ms 44504 KB Output is correct
52 Correct 514 ms 43492 KB Output is correct
53 Correct 499 ms 40736 KB Output is correct
54 Correct 416 ms 32380 KB Output is correct
55 Correct 547 ms 46552 KB Output is correct
56 Correct 583 ms 47460 KB Output is correct
57 Correct 425 ms 33348 KB Output is correct
58 Correct 567 ms 41936 KB Output is correct
59 Correct 180 ms 18168 KB Output is correct
60 Correct 501 ms 46060 KB Output is correct
61 Correct 511 ms 45008 KB Output is correct
62 Correct 534 ms 47316 KB Output is correct
63 Correct 348 ms 34112 KB Output is correct
64 Correct 8 ms 9984 KB Output is correct
65 Correct 8 ms 9984 KB Output is correct
66 Correct 8 ms 9984 KB Output is correct
67 Correct 8 ms 9856 KB Output is correct
68 Correct 8 ms 9856 KB Output is correct
69 Correct 8 ms 9984 KB Output is correct
70 Correct 8 ms 9984 KB Output is correct
71 Correct 8 ms 10112 KB Output is correct
72 Correct 8 ms 9984 KB Output is correct
73 Correct 9 ms 9984 KB Output is correct
74 Correct 203 ms 29976 KB Output is correct
75 Correct 278 ms 42864 KB Output is correct
76 Correct 241 ms 36980 KB Output is correct
77 Correct 216 ms 31460 KB Output is correct
78 Correct 95 ms 19688 KB Output is correct
79 Correct 82 ms 17328 KB Output is correct
80 Correct 160 ms 26860 KB Output is correct
81 Correct 319 ms 44432 KB Output is correct
82 Correct 289 ms 38536 KB Output is correct
83 Correct 377 ms 50032 KB Output is correct
84 Correct 151 ms 30388 KB Output is correct
85 Correct 268 ms 36704 KB Output is correct
86 Correct 153 ms 18128 KB Output is correct
87 Correct 542 ms 43948 KB Output is correct
88 Correct 547 ms 44248 KB Output is correct
89 Correct 560 ms 32760 KB Output is correct
90 Correct 383 ms 21700 KB Output is correct
91 Correct 410 ms 22636 KB Output is correct
92 Correct 560 ms 29152 KB Output is correct
93 Correct 677 ms 45892 KB Output is correct
94 Correct 692 ms 40932 KB Output is correct
95 Correct 718 ms 52464 KB Output is correct
96 Correct 450 ms 33348 KB Output is correct
97 Correct 627 ms 36252 KB Output is correct