답안 #406108

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
406108 2021-05-17T08:31:03 Z amunduzbaev 자매 도시 (APIO20_swap) C++14
43 / 100
1299 ms 36708 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++) 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(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);
}

# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 5004 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 4 ms 5068 KB Output is correct
6 Correct 4 ms 5016 KB Output is correct
7 Correct 4 ms 5024 KB Output is correct
8 Correct 4 ms 5068 KB Output is correct
9 Correct 75 ms 14848 KB Output is correct
10 Correct 104 ms 16676 KB Output is correct
11 Correct 106 ms 16584 KB Output is correct
12 Correct 109 ms 17184 KB Output is correct
13 Correct 112 ms 17092 KB Output is correct
14 Correct 80 ms 14848 KB Output is correct
15 Correct 179 ms 18720 KB Output is correct
16 Correct 180 ms 18336 KB Output is correct
17 Correct 195 ms 18924 KB Output is correct
18 Correct 168 ms 18976 KB Output is correct
19 Correct 68 ms 11476 KB Output is correct
20 Correct 170 ms 20052 KB Output is correct
21 Correct 167 ms 19892 KB Output is correct
22 Correct 236 ms 20504 KB Output is correct
23 Correct 179 ms 20528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 5004 KB Output is correct
3 Runtime error 123 ms 36708 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 5004 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 4 ms 5068 KB Output is correct
6 Correct 4 ms 5016 KB Output is correct
7 Correct 4 ms 5024 KB Output is correct
8 Correct 4 ms 5068 KB Output is correct
9 Correct 5 ms 8012 KB Output is correct
10 Correct 7 ms 8140 KB Output is correct
11 Correct 6 ms 8216 KB Output is correct
12 Correct 8 ms 8160 KB Output is correct
13 Correct 8 ms 8208 KB Output is correct
14 Correct 7 ms 8220 KB Output is correct
15 Correct 10 ms 8220 KB Output is correct
16 Correct 7 ms 8204 KB Output is correct
17 Correct 6 ms 8268 KB Output is correct
18 Correct 7 ms 8268 KB Output is correct
19 Correct 8 ms 8140 KB Output is correct
20 Correct 7 ms 8172 KB Output is correct
21 Correct 13 ms 8220 KB Output is correct
22 Correct 9 ms 8268 KB Output is correct
23 Correct 9 ms 8244 KB Output is correct
24 Correct 11 ms 8364 KB Output is correct
25 Correct 11 ms 8268 KB Output is correct
26 Correct 11 ms 8348 KB Output is correct
27 Correct 8 ms 8268 KB Output is correct
28 Correct 13 ms 8380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8012 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 5004 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 4 ms 4940 KB Output is correct
6 Correct 4 ms 5068 KB Output is correct
7 Correct 4 ms 5016 KB Output is correct
8 Correct 4 ms 5024 KB Output is correct
9 Correct 4 ms 5068 KB Output is correct
10 Correct 75 ms 14848 KB Output is correct
11 Correct 104 ms 16676 KB Output is correct
12 Correct 106 ms 16584 KB Output is correct
13 Correct 109 ms 17184 KB Output is correct
14 Correct 112 ms 17092 KB Output is correct
15 Correct 7 ms 8140 KB Output is correct
16 Correct 6 ms 8216 KB Output is correct
17 Correct 8 ms 8160 KB Output is correct
18 Correct 8 ms 8208 KB Output is correct
19 Correct 7 ms 8220 KB Output is correct
20 Correct 10 ms 8220 KB Output is correct
21 Correct 7 ms 8204 KB Output is correct
22 Correct 6 ms 8268 KB Output is correct
23 Correct 7 ms 8268 KB Output is correct
24 Correct 8 ms 8140 KB Output is correct
25 Correct 7 ms 8172 KB Output is correct
26 Correct 13 ms 8220 KB Output is correct
27 Correct 9 ms 8268 KB Output is correct
28 Correct 9 ms 8244 KB Output is correct
29 Correct 11 ms 8364 KB Output is correct
30 Correct 11 ms 8268 KB Output is correct
31 Correct 11 ms 8348 KB Output is correct
32 Correct 8 ms 8268 KB Output is correct
33 Correct 13 ms 8380 KB Output is correct
34 Correct 22 ms 9924 KB Output is correct
35 Correct 408 ms 20616 KB Output is correct
36 Correct 461 ms 20156 KB Output is correct
37 Correct 390 ms 19884 KB Output is correct
38 Correct 449 ms 19664 KB Output is correct
39 Correct 432 ms 19784 KB Output is correct
40 Correct 468 ms 19616 KB Output is correct
41 Correct 445 ms 20724 KB Output is correct
42 Correct 340 ms 20524 KB Output is correct
43 Correct 358 ms 20572 KB Output is correct
44 Correct 602 ms 21964 KB Output is correct
45 Correct 875 ms 27428 KB Output is correct
46 Correct 598 ms 20144 KB Output is correct
47 Correct 631 ms 19744 KB Output is correct
48 Correct 751 ms 21908 KB Output is correct
49 Correct 131 ms 26356 KB Output is correct
50 Correct 188 ms 22184 KB Output is correct
51 Correct 811 ms 25400 KB Output is correct
52 Correct 1094 ms 28904 KB Output is correct
53 Correct 1072 ms 30376 KB Output is correct
54 Correct 1299 ms 32408 KB Output is correct
55 Correct 482 ms 21256 KB Output is correct
56 Correct 1144 ms 30148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 5004 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 4 ms 5068 KB Output is correct
6 Correct 4 ms 5016 KB Output is correct
7 Correct 4 ms 5024 KB Output is correct
8 Correct 4 ms 5068 KB Output is correct
9 Correct 75 ms 14848 KB Output is correct
10 Correct 104 ms 16676 KB Output is correct
11 Correct 106 ms 16584 KB Output is correct
12 Correct 109 ms 17184 KB Output is correct
13 Correct 112 ms 17092 KB Output is correct
14 Correct 80 ms 14848 KB Output is correct
15 Correct 179 ms 18720 KB Output is correct
16 Correct 180 ms 18336 KB Output is correct
17 Correct 195 ms 18924 KB Output is correct
18 Correct 168 ms 18976 KB Output is correct
19 Runtime error 123 ms 36708 KB Execution killed with signal 11
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8012 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 5004 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 4 ms 4940 KB Output is correct
6 Correct 4 ms 5068 KB Output is correct
7 Correct 4 ms 5016 KB Output is correct
8 Correct 4 ms 5024 KB Output is correct
9 Correct 4 ms 5068 KB Output is correct
10 Correct 75 ms 14848 KB Output is correct
11 Correct 104 ms 16676 KB Output is correct
12 Correct 106 ms 16584 KB Output is correct
13 Correct 109 ms 17184 KB Output is correct
14 Correct 112 ms 17092 KB Output is correct
15 Correct 80 ms 14848 KB Output is correct
16 Correct 179 ms 18720 KB Output is correct
17 Correct 180 ms 18336 KB Output is correct
18 Correct 195 ms 18924 KB Output is correct
19 Correct 168 ms 18976 KB Output is correct
20 Runtime error 123 ms 36708 KB Execution killed with signal 11
21 Halted 0 ms 0 KB -