Submission #406115

# Submission time Handle Problem Language Result Execution time Memory
406115 2021-05-17T08:33:50 Z amunduzbaev Swapping Cities (APIO20_swap) C++14
50 / 100
2000 ms 32964 KB
#include "swap.h"
#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define ff first
#define ss second
#define sz(x) (int)x.size()
#define int long long
template<class T> bool umin(T& a, const T b) { if(a > b) { a = b; return 1; } return 0; }
template<class T> bool umax(T& a, const T b) { if(a < b) { a = b; return 1; } return 0; }

const int NN = 1e5+5;
const int mod = 1e9+7;

int mx = 0, ok, res, d[NN], n, m;
vector<pair<int, int>> edges[NN];
vector<pair<int, int>> mm;
vector<int> tte[NN];
int tree = 0, cyc = 0, rope = 0;

void init(int32_t N, int32_t M,
	vector<int32_t> a, vector<int32_t> b, vector<int32_t> c) {
	n = N, m = M;
	for(int i=0;i<m;i++){
		edges[a[i]].pb({b[i], c[i]});
		edges[b[i]].pb({a[i], c[i]});
		tte[a[i]].pb(c[i]);
		tte[b[i]].pb(c[i]);
		mx = max(mx, (int)c[i]);
	} for(int i=0;i<n;i++) sort(tte[i].begin(), tte[i].end());
	tree = cyc = rope = 1;
	for(int i=0;i<m;i++) d[max(a[i], b[i])] = c[i];
	for(int i=1;i<n;i++) mm.pb({d[i], i});
	sort(mm.begin(), mm.end());
	for(int i=0;i<m;i++) tree &= (min(a[i], b[i]) == 0);
	for(int i=0;i<n;i++) cyc &= (sz(edges[i]) == 2);
	for(int i=0;i<n;i++) rope &= (sz(edges[i]) <= 2);
}

int mn[NN], par[NN], bad[NN];
int mn2[NN];

int32_t getMinimumFuelCapacity(int32_t x, int32_t y) {
	if(cyc) return mx;
	if(rope) return -1;
	if(tree){
		if(n == 2) return -1;
		if(x > y) swap(x, y);
		if(x == 0){
			if(y == mm[0].ss || y == mm[1].ss || y == mm[2].ss) return max({mm[0].ff, mm[1].ff, mm[2].ff});
			else return max({d[y], mm[0].ff, mm[1].ff});
		} else {
			if(n == 3) return -1;
			if((mm[0].ss == y || mm[1].ss == y) && (mm[0].ss == x || mm[1].ss == x)) return max({d[x], d[y], mm[2].ff});
			else if(x == mm[0].ss || y == mm[0].ss) return max({d[x], d[y], mm[1].ff});
			else return max({d[x], d[y], mm[0].ff});
		} 
	} 
	
	priority_queue<pair<int, int>> qq;
	qq.push({0, x});
	memset(mn, 127, sizeof mn);
	memset(mn2, 127, sizeof mn2);
	memset(bad, 0, sizeof bad);
	memset(par, 0, sizeof par);
	par[x] = -1, mn[x] = 0;
	while(!qq.empty()){
		auto u = qq.top(); qq.pop();
		if(mn[u.ss] < -u.ff) continue;
		for(auto x : edges[u.ss]) if(umin(mn[x.ff], max(mn[u.ss], x.ss))) par[x.ff] = u.ss, qq.push({-mn[x.ff], x.ff});
	} for(int i=0;i<n;i++) if(sz(tte[i]) >= 3) mn2[i] = tte[i][2], qq.push({-tte[i][2], i});
	while(!qq.empty()){
		auto u = qq.top(); qq.pop();
		if(mn2[u.ss] < -u.ff) continue;
		for(auto x : edges[u.ss]) if(umin(mn2[x.ff], max(mn2[u.ss], x.ss))) qq.push({-mn2[x.ff], x.ff});
	} int trr = mn[y], tt = y;
	vector<int> bads;
	while(~tt){
		bads.pb(tt);
		bad[tt] = 1;
		tt = par[tt];
	} int rr = mod;
	
	for(int i=0;i<n;i++){
		if(bad[i]){
			if(i == y || i == x) umin(rr, mn2[i]);
			else {
				int mn = mod;
				for(auto x : edges[i]){
					if(bad[x.ff]) continue;
					umin(mn, x.ss);
				} umin(rr, mn);
			}
		}
	} 
	
	int a = x, b = bads[sz(bads)-2];
	qq.push({0, x});
	for(int i=0;i<n;i++) mn[i] = mod;
	mn[x] = 0;
	while(!qq.empty()){
		auto u = qq.top(); qq.pop();
		if(mn[u.ss] < -u.ff) continue;
		for(auto x : edges[u.ss]){
			if((x.ff == a && u.ss == b) || (x.ff == b && u.ss == a)) continue;
			if(umin(mn[x.ff], max(mn[u.ss], x.ss))) qq.push({-mn[x.ff], x.ff});
		}
	} umin(rr, mn[y]); 
	a = y, b = bads[1];
	qq.push({0, x});
	for(int i=0;i<n;i++) mn[i] = mod;
	mn[x] = 0;
	while(!qq.empty()){
		auto u = qq.top(); qq.pop();
		if(mn[u.ss] < -u.ff) continue;
		for(auto x : edges[u.ss]){
			if((x.ff == a && u.ss == b) || (x.ff == b && u.ss == a)) continue;
			if(umin(mn[x.ff], max(mn[u.ss], x.ss))) qq.push({-mn[x.ff], x.ff});
		}
	} umin(rr, mn[y]); 
	
	
	if(rr == mod) return -1;
	return max(rr, trr);
}

# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 5012 KB Output is correct
4 Correct 4 ms 4960 KB Output is correct
5 Correct 5 ms 5068 KB Output is correct
6 Correct 4 ms 5068 KB Output is correct
7 Correct 4 ms 5068 KB Output is correct
8 Correct 4 ms 5068 KB Output is correct
9 Correct 92 ms 16684 KB Output is correct
10 Correct 111 ms 18744 KB Output is correct
11 Correct 106 ms 18564 KB Output is correct
12 Correct 113 ms 19264 KB Output is correct
13 Correct 118 ms 19240 KB Output is correct
14 Correct 93 ms 16808 KB Output is correct
15 Correct 171 ms 20372 KB Output is correct
16 Correct 172 ms 20060 KB Output is correct
17 Correct 179 ms 20796 KB Output is correct
18 Correct 175 ms 20760 KB Output is correct
19 Correct 71 ms 11080 KB Output is correct
20 Correct 169 ms 21120 KB Output is correct
21 Correct 176 ms 21016 KB Output is correct
22 Correct 176 ms 21772 KB Output is correct
23 Correct 187 ms 21796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 184 ms 21096 KB Output is correct
4 Correct 154 ms 23824 KB Output is correct
5 Correct 177 ms 23656 KB Output is correct
6 Correct 152 ms 23828 KB Output is correct
7 Correct 162 ms 23772 KB Output is correct
8 Correct 153 ms 23204 KB Output is correct
9 Correct 156 ms 23676 KB Output is correct
10 Correct 162 ms 23132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 5012 KB Output is correct
4 Correct 4 ms 4960 KB Output is correct
5 Correct 5 ms 5068 KB Output is correct
6 Correct 4 ms 5068 KB Output is correct
7 Correct 4 ms 5068 KB Output is correct
8 Correct 4 ms 5068 KB Output is correct
9 Correct 6 ms 8012 KB Output is correct
10 Correct 8 ms 8268 KB Output is correct
11 Correct 7 ms 8268 KB Output is correct
12 Correct 8 ms 8220 KB Output is correct
13 Correct 7 ms 8216 KB Output is correct
14 Correct 10 ms 8268 KB Output is correct
15 Correct 8 ms 8236 KB Output is correct
16 Correct 8 ms 8224 KB Output is correct
17 Correct 7 ms 8268 KB Output is correct
18 Correct 8 ms 8268 KB Output is correct
19 Correct 8 ms 8268 KB Output is correct
20 Correct 7 ms 8268 KB Output is correct
21 Correct 9 ms 8224 KB Output is correct
22 Correct 8 ms 8268 KB Output is correct
23 Correct 8 ms 8268 KB Output is correct
24 Correct 12 ms 8268 KB Output is correct
25 Correct 11 ms 8404 KB Output is correct
26 Correct 11 ms 8396 KB Output is correct
27 Correct 7 ms 8268 KB Output is correct
28 Correct 11 ms 8396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 8012 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 5012 KB Output is correct
5 Correct 4 ms 4960 KB Output is correct
6 Correct 5 ms 5068 KB Output is correct
7 Correct 4 ms 5068 KB Output is correct
8 Correct 4 ms 5068 KB Output is correct
9 Correct 4 ms 5068 KB Output is correct
10 Correct 92 ms 16684 KB Output is correct
11 Correct 111 ms 18744 KB Output is correct
12 Correct 106 ms 18564 KB Output is correct
13 Correct 113 ms 19264 KB Output is correct
14 Correct 118 ms 19240 KB Output is correct
15 Correct 8 ms 8268 KB Output is correct
16 Correct 7 ms 8268 KB Output is correct
17 Correct 8 ms 8220 KB Output is correct
18 Correct 7 ms 8216 KB Output is correct
19 Correct 10 ms 8268 KB Output is correct
20 Correct 8 ms 8236 KB Output is correct
21 Correct 8 ms 8224 KB Output is correct
22 Correct 7 ms 8268 KB Output is correct
23 Correct 8 ms 8268 KB Output is correct
24 Correct 8 ms 8268 KB Output is correct
25 Correct 7 ms 8268 KB Output is correct
26 Correct 9 ms 8224 KB Output is correct
27 Correct 8 ms 8268 KB Output is correct
28 Correct 8 ms 8268 KB Output is correct
29 Correct 12 ms 8268 KB Output is correct
30 Correct 11 ms 8404 KB Output is correct
31 Correct 11 ms 8396 KB Output is correct
32 Correct 7 ms 8268 KB Output is correct
33 Correct 11 ms 8396 KB Output is correct
34 Correct 23 ms 10144 KB Output is correct
35 Correct 398 ms 21292 KB Output is correct
36 Correct 406 ms 20812 KB Output is correct
37 Correct 389 ms 20452 KB Output is correct
38 Correct 432 ms 20404 KB Output is correct
39 Correct 458 ms 20352 KB Output is correct
40 Correct 428 ms 19996 KB Output is correct
41 Correct 402 ms 21124 KB Output is correct
42 Correct 366 ms 21136 KB Output is correct
43 Correct 414 ms 21260 KB Output is correct
44 Correct 571 ms 22288 KB Output is correct
45 Correct 898 ms 27496 KB Output is correct
46 Correct 469 ms 20896 KB Output is correct
47 Correct 506 ms 20476 KB Output is correct
48 Correct 730 ms 22340 KB Output is correct
49 Correct 110 ms 24532 KB Output is correct
50 Correct 173 ms 20440 KB Output is correct
51 Correct 778 ms 25120 KB Output is correct
52 Correct 1149 ms 29488 KB Output is correct
53 Correct 1187 ms 31052 KB Output is correct
54 Correct 1301 ms 32964 KB Output is correct
55 Correct 486 ms 21996 KB Output is correct
56 Correct 1171 ms 30704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 5012 KB Output is correct
4 Correct 4 ms 4960 KB Output is correct
5 Correct 5 ms 5068 KB Output is correct
6 Correct 4 ms 5068 KB Output is correct
7 Correct 4 ms 5068 KB Output is correct
8 Correct 4 ms 5068 KB Output is correct
9 Correct 92 ms 16684 KB Output is correct
10 Correct 111 ms 18744 KB Output is correct
11 Correct 106 ms 18564 KB Output is correct
12 Correct 113 ms 19264 KB Output is correct
13 Correct 118 ms 19240 KB Output is correct
14 Correct 93 ms 16808 KB Output is correct
15 Correct 171 ms 20372 KB Output is correct
16 Correct 172 ms 20060 KB Output is correct
17 Correct 179 ms 20796 KB Output is correct
18 Correct 175 ms 20760 KB Output is correct
19 Correct 184 ms 21096 KB Output is correct
20 Correct 154 ms 23824 KB Output is correct
21 Correct 177 ms 23656 KB Output is correct
22 Correct 152 ms 23828 KB Output is correct
23 Correct 162 ms 23772 KB Output is correct
24 Correct 153 ms 23204 KB Output is correct
25 Correct 156 ms 23676 KB Output is correct
26 Correct 162 ms 23132 KB Output is correct
27 Correct 8 ms 8268 KB Output is correct
28 Correct 7 ms 8268 KB Output is correct
29 Correct 8 ms 8220 KB Output is correct
30 Correct 7 ms 8216 KB Output is correct
31 Correct 10 ms 8268 KB Output is correct
32 Correct 8 ms 8236 KB Output is correct
33 Correct 8 ms 8224 KB Output is correct
34 Correct 7 ms 8268 KB Output is correct
35 Correct 8 ms 8268 KB Output is correct
36 Correct 23 ms 10144 KB Output is correct
37 Correct 398 ms 21292 KB Output is correct
38 Correct 406 ms 20812 KB Output is correct
39 Correct 389 ms 20452 KB Output is correct
40 Correct 432 ms 20404 KB Output is correct
41 Correct 458 ms 20352 KB Output is correct
42 Correct 428 ms 19996 KB Output is correct
43 Correct 402 ms 21124 KB Output is correct
44 Correct 366 ms 21136 KB Output is correct
45 Correct 414 ms 21260 KB Output is correct
46 Correct 571 ms 22288 KB Output is correct
47 Execution timed out 2078 ms 10568 KB Time limit exceeded
48 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 8012 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 5012 KB Output is correct
5 Correct 4 ms 4960 KB Output is correct
6 Correct 5 ms 5068 KB Output is correct
7 Correct 4 ms 5068 KB Output is correct
8 Correct 4 ms 5068 KB Output is correct
9 Correct 4 ms 5068 KB Output is correct
10 Correct 92 ms 16684 KB Output is correct
11 Correct 111 ms 18744 KB Output is correct
12 Correct 106 ms 18564 KB Output is correct
13 Correct 113 ms 19264 KB Output is correct
14 Correct 118 ms 19240 KB Output is correct
15 Correct 93 ms 16808 KB Output is correct
16 Correct 171 ms 20372 KB Output is correct
17 Correct 172 ms 20060 KB Output is correct
18 Correct 179 ms 20796 KB Output is correct
19 Correct 175 ms 20760 KB Output is correct
20 Correct 184 ms 21096 KB Output is correct
21 Correct 154 ms 23824 KB Output is correct
22 Correct 177 ms 23656 KB Output is correct
23 Correct 152 ms 23828 KB Output is correct
24 Correct 162 ms 23772 KB Output is correct
25 Correct 153 ms 23204 KB Output is correct
26 Correct 156 ms 23676 KB Output is correct
27 Correct 162 ms 23132 KB Output is correct
28 Correct 8 ms 8268 KB Output is correct
29 Correct 7 ms 8268 KB Output is correct
30 Correct 8 ms 8220 KB Output is correct
31 Correct 7 ms 8216 KB Output is correct
32 Correct 10 ms 8268 KB Output is correct
33 Correct 8 ms 8236 KB Output is correct
34 Correct 8 ms 8224 KB Output is correct
35 Correct 7 ms 8268 KB Output is correct
36 Correct 8 ms 8268 KB Output is correct
37 Correct 23 ms 10144 KB Output is correct
38 Correct 398 ms 21292 KB Output is correct
39 Correct 406 ms 20812 KB Output is correct
40 Correct 389 ms 20452 KB Output is correct
41 Correct 432 ms 20404 KB Output is correct
42 Correct 458 ms 20352 KB Output is correct
43 Correct 428 ms 19996 KB Output is correct
44 Correct 402 ms 21124 KB Output is correct
45 Correct 366 ms 21136 KB Output is correct
46 Correct 414 ms 21260 KB Output is correct
47 Correct 571 ms 22288 KB Output is correct
48 Execution timed out 2078 ms 10568 KB Time limit exceeded
49 Halted 0 ms 0 KB -