답안 #406300

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
406300 2021-05-17T10:40:24 Z amunduzbaev 자매 도시 (APIO20_swap) C++14
100 / 100
591 ms 115496 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 N = 3e5+5;
const int mod = 1e9+7;

int n, m, par[N], d[N], f[N], w[N];
vector<int> edges[N];
int lca[N][30], tin[N], tout[N], t;

int fx(int x) { return (x == par[x] ? x : par[x] = fx(par[x])); }
void merge(int a, int b, int c){
	d[a]++, d[b]++;
	int cnd = (max(d[a], d[b]) >= 3);
	a = fx(a), b = fx(b);
	edges[n].pb(a);
	if(b != a) edges[n].pb(b);
	if(a == b){
		par[a] = par[b] = n;
		par[n] = n, f[n] = 1, w[n] = c;
	} else {
		par[a] = par[b] = n, w[n] = c;
		par[n] = n, f[n] = f[a] | f[b] | cnd;
	} n++;
}

void dfs(int u, int p){
	tin[u] = t++;
	lca[u][0] = p;
	for(int j=1;j<30;j++) lca[u][j] = lca[lca[u][j-1]][j-1];
	for(auto x : edges[u]) dfs(x, u);
	tout[u] = t - 1;
}

void init(int32_t N, int32_t M,
	vector<int32_t> a, vector<int32_t> b, vector<int32_t> c) {
	n = N, m = M;
	vector<array<int, 3>> tt;
	for(int i=0;i<m;i++) tt.pb({c[i], a[i], b[i]});
	sort(tt.begin(), tt.end());
	for(int i=0;i<n;i++) par[i] = i;
	for(auto x : tt) merge(x[1], x[2], x[0]);
	//for(int i=n-1;i>=0;i--){
		//for(auto x : edges[i]) cout<<x<<" "<<i<<"\n";
	//}
	memset(lca, -1, sizeof lca);
	dfs(n-1, n-1);
}

bool up(int a, int b) { return (tin[a] <= tin[b] && tout[b] <= tout[a]); }

int f_lca(int a, int b){
	if(up(b, a)) return b;
	if(up(a, b)) return a;
	for(int i=29;i>=0;i--){
		if(up(lca[a][i], b)) continue;
		a = lca[a][i];
	} return lca[a][0];
}

/*

5 4
0 1 0
1 2 1
2 3 2
3 4 3
5
0 1
1 2
2 3
3 4
0 4

*/

int32_t getMinimumFuelCapacity(int32_t x, int32_t y) {
	int lc = f_lca(x, y);
	if(f[lc]) return w[lc];
	for(int i=29;i>=0;i--){
		if(f[lca[lc][i]]) continue;
		lc = lca[lc][i];
	} 
	if(f[lca[lc][0]]) return w[lca[lc][0]];
	else return -1;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 77764 KB Output is correct
2 Correct 39 ms 77772 KB Output is correct
3 Correct 37 ms 77740 KB Output is correct
4 Correct 37 ms 77772 KB Output is correct
5 Correct 38 ms 77880 KB Output is correct
6 Correct 42 ms 77892 KB Output is correct
7 Correct 37 ms 77920 KB Output is correct
8 Correct 35 ms 77968 KB Output is correct
9 Correct 175 ms 91152 KB Output is correct
10 Correct 203 ms 94132 KB Output is correct
11 Correct 185 ms 93796 KB Output is correct
12 Correct 205 ms 94768 KB Output is correct
13 Correct 227 ms 96304 KB Output is correct
14 Correct 184 ms 91436 KB Output is correct
15 Correct 369 ms 98064 KB Output is correct
16 Correct 434 ms 97632 KB Output is correct
17 Correct 372 ms 98740 KB Output is correct
18 Correct 518 ms 100212 KB Output is correct
19 Correct 188 ms 85196 KB Output is correct
20 Correct 357 ms 97944 KB Output is correct
21 Correct 372 ms 97488 KB Output is correct
22 Correct 376 ms 98600 KB Output is correct
23 Correct 586 ms 100340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 77764 KB Output is correct
2 Correct 39 ms 77772 KB Output is correct
3 Correct 456 ms 100148 KB Output is correct
4 Correct 437 ms 101048 KB Output is correct
5 Correct 462 ms 100632 KB Output is correct
6 Correct 471 ms 100916 KB Output is correct
7 Correct 458 ms 100916 KB Output is correct
8 Correct 437 ms 99984 KB Output is correct
9 Correct 438 ms 100692 KB Output is correct
10 Correct 438 ms 99776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 77764 KB Output is correct
2 Correct 39 ms 77772 KB Output is correct
3 Correct 37 ms 77740 KB Output is correct
4 Correct 37 ms 77772 KB Output is correct
5 Correct 38 ms 77880 KB Output is correct
6 Correct 42 ms 77892 KB Output is correct
7 Correct 37 ms 77920 KB Output is correct
8 Correct 35 ms 77968 KB Output is correct
9 Correct 36 ms 77772 KB Output is correct
10 Correct 41 ms 77976 KB Output is correct
11 Correct 41 ms 77860 KB Output is correct
12 Correct 37 ms 77892 KB Output is correct
13 Correct 40 ms 77860 KB Output is correct
14 Correct 45 ms 77944 KB Output is correct
15 Correct 44 ms 77980 KB Output is correct
16 Correct 38 ms 77876 KB Output is correct
17 Correct 41 ms 77892 KB Output is correct
18 Correct 47 ms 77912 KB Output is correct
19 Correct 44 ms 77908 KB Output is correct
20 Correct 36 ms 77892 KB Output is correct
21 Correct 50 ms 78000 KB Output is correct
22 Correct 39 ms 78128 KB Output is correct
23 Correct 42 ms 77896 KB Output is correct
24 Correct 38 ms 78076 KB Output is correct
25 Correct 39 ms 78072 KB Output is correct
26 Correct 37 ms 78156 KB Output is correct
27 Correct 43 ms 77916 KB Output is correct
28 Correct 42 ms 78136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 77772 KB Output is correct
2 Correct 36 ms 77764 KB Output is correct
3 Correct 39 ms 77772 KB Output is correct
4 Correct 37 ms 77740 KB Output is correct
5 Correct 37 ms 77772 KB Output is correct
6 Correct 38 ms 77880 KB Output is correct
7 Correct 42 ms 77892 KB Output is correct
8 Correct 37 ms 77920 KB Output is correct
9 Correct 35 ms 77968 KB Output is correct
10 Correct 175 ms 91152 KB Output is correct
11 Correct 203 ms 94132 KB Output is correct
12 Correct 185 ms 93796 KB Output is correct
13 Correct 205 ms 94768 KB Output is correct
14 Correct 227 ms 96304 KB Output is correct
15 Correct 41 ms 77976 KB Output is correct
16 Correct 41 ms 77860 KB Output is correct
17 Correct 37 ms 77892 KB Output is correct
18 Correct 40 ms 77860 KB Output is correct
19 Correct 45 ms 77944 KB Output is correct
20 Correct 44 ms 77980 KB Output is correct
21 Correct 38 ms 77876 KB Output is correct
22 Correct 41 ms 77892 KB Output is correct
23 Correct 47 ms 77912 KB Output is correct
24 Correct 44 ms 77908 KB Output is correct
25 Correct 36 ms 77892 KB Output is correct
26 Correct 50 ms 78000 KB Output is correct
27 Correct 39 ms 78128 KB Output is correct
28 Correct 42 ms 77896 KB Output is correct
29 Correct 38 ms 78076 KB Output is correct
30 Correct 39 ms 78072 KB Output is correct
31 Correct 37 ms 78156 KB Output is correct
32 Correct 43 ms 77916 KB Output is correct
33 Correct 42 ms 78136 KB Output is correct
34 Correct 61 ms 80076 KB Output is correct
35 Correct 201 ms 94516 KB Output is correct
36 Correct 200 ms 94460 KB Output is correct
37 Correct 203 ms 94408 KB Output is correct
38 Correct 189 ms 94260 KB Output is correct
39 Correct 189 ms 94108 KB Output is correct
40 Correct 177 ms 92844 KB Output is correct
41 Correct 222 ms 94764 KB Output is correct
42 Correct 196 ms 94512 KB Output is correct
43 Correct 246 ms 96476 KB Output is correct
44 Correct 232 ms 94828 KB Output is correct
45 Correct 309 ms 103436 KB Output is correct
46 Correct 207 ms 94504 KB Output is correct
47 Correct 204 ms 94496 KB Output is correct
48 Correct 226 ms 97068 KB Output is correct
49 Correct 198 ms 104044 KB Output is correct
50 Correct 162 ms 98080 KB Output is correct
51 Correct 228 ms 100212 KB Output is correct
52 Correct 270 ms 105160 KB Output is correct
53 Correct 299 ms 107416 KB Output is correct
54 Correct 314 ms 111492 KB Output is correct
55 Correct 219 ms 96408 KB Output is correct
56 Correct 327 ms 107940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 77764 KB Output is correct
2 Correct 39 ms 77772 KB Output is correct
3 Correct 37 ms 77740 KB Output is correct
4 Correct 37 ms 77772 KB Output is correct
5 Correct 38 ms 77880 KB Output is correct
6 Correct 42 ms 77892 KB Output is correct
7 Correct 37 ms 77920 KB Output is correct
8 Correct 35 ms 77968 KB Output is correct
9 Correct 175 ms 91152 KB Output is correct
10 Correct 203 ms 94132 KB Output is correct
11 Correct 185 ms 93796 KB Output is correct
12 Correct 205 ms 94768 KB Output is correct
13 Correct 227 ms 96304 KB Output is correct
14 Correct 184 ms 91436 KB Output is correct
15 Correct 369 ms 98064 KB Output is correct
16 Correct 434 ms 97632 KB Output is correct
17 Correct 372 ms 98740 KB Output is correct
18 Correct 518 ms 100212 KB Output is correct
19 Correct 456 ms 100148 KB Output is correct
20 Correct 437 ms 101048 KB Output is correct
21 Correct 462 ms 100632 KB Output is correct
22 Correct 471 ms 100916 KB Output is correct
23 Correct 458 ms 100916 KB Output is correct
24 Correct 437 ms 99984 KB Output is correct
25 Correct 438 ms 100692 KB Output is correct
26 Correct 438 ms 99776 KB Output is correct
27 Correct 41 ms 77976 KB Output is correct
28 Correct 41 ms 77860 KB Output is correct
29 Correct 37 ms 77892 KB Output is correct
30 Correct 40 ms 77860 KB Output is correct
31 Correct 45 ms 77944 KB Output is correct
32 Correct 44 ms 77980 KB Output is correct
33 Correct 38 ms 77876 KB Output is correct
34 Correct 41 ms 77892 KB Output is correct
35 Correct 47 ms 77912 KB Output is correct
36 Correct 61 ms 80076 KB Output is correct
37 Correct 201 ms 94516 KB Output is correct
38 Correct 200 ms 94460 KB Output is correct
39 Correct 203 ms 94408 KB Output is correct
40 Correct 189 ms 94260 KB Output is correct
41 Correct 189 ms 94108 KB Output is correct
42 Correct 177 ms 92844 KB Output is correct
43 Correct 222 ms 94764 KB Output is correct
44 Correct 196 ms 94512 KB Output is correct
45 Correct 246 ms 96476 KB Output is correct
46 Correct 232 ms 94828 KB Output is correct
47 Correct 84 ms 80204 KB Output is correct
48 Correct 360 ms 98392 KB Output is correct
49 Correct 456 ms 98340 KB Output is correct
50 Correct 390 ms 98348 KB Output is correct
51 Correct 328 ms 98244 KB Output is correct
52 Correct 356 ms 97296 KB Output is correct
53 Correct 293 ms 93308 KB Output is correct
54 Correct 404 ms 98636 KB Output is correct
55 Correct 349 ms 98476 KB Output is correct
56 Correct 451 ms 100008 KB Output is correct
57 Correct 371 ms 98716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 77772 KB Output is correct
2 Correct 36 ms 77764 KB Output is correct
3 Correct 39 ms 77772 KB Output is correct
4 Correct 37 ms 77740 KB Output is correct
5 Correct 37 ms 77772 KB Output is correct
6 Correct 38 ms 77880 KB Output is correct
7 Correct 42 ms 77892 KB Output is correct
8 Correct 37 ms 77920 KB Output is correct
9 Correct 35 ms 77968 KB Output is correct
10 Correct 175 ms 91152 KB Output is correct
11 Correct 203 ms 94132 KB Output is correct
12 Correct 185 ms 93796 KB Output is correct
13 Correct 205 ms 94768 KB Output is correct
14 Correct 227 ms 96304 KB Output is correct
15 Correct 184 ms 91436 KB Output is correct
16 Correct 369 ms 98064 KB Output is correct
17 Correct 434 ms 97632 KB Output is correct
18 Correct 372 ms 98740 KB Output is correct
19 Correct 518 ms 100212 KB Output is correct
20 Correct 456 ms 100148 KB Output is correct
21 Correct 437 ms 101048 KB Output is correct
22 Correct 462 ms 100632 KB Output is correct
23 Correct 471 ms 100916 KB Output is correct
24 Correct 458 ms 100916 KB Output is correct
25 Correct 437 ms 99984 KB Output is correct
26 Correct 438 ms 100692 KB Output is correct
27 Correct 438 ms 99776 KB Output is correct
28 Correct 41 ms 77976 KB Output is correct
29 Correct 41 ms 77860 KB Output is correct
30 Correct 37 ms 77892 KB Output is correct
31 Correct 40 ms 77860 KB Output is correct
32 Correct 45 ms 77944 KB Output is correct
33 Correct 44 ms 77980 KB Output is correct
34 Correct 38 ms 77876 KB Output is correct
35 Correct 41 ms 77892 KB Output is correct
36 Correct 47 ms 77912 KB Output is correct
37 Correct 61 ms 80076 KB Output is correct
38 Correct 201 ms 94516 KB Output is correct
39 Correct 200 ms 94460 KB Output is correct
40 Correct 203 ms 94408 KB Output is correct
41 Correct 189 ms 94260 KB Output is correct
42 Correct 189 ms 94108 KB Output is correct
43 Correct 177 ms 92844 KB Output is correct
44 Correct 222 ms 94764 KB Output is correct
45 Correct 196 ms 94512 KB Output is correct
46 Correct 246 ms 96476 KB Output is correct
47 Correct 232 ms 94828 KB Output is correct
48 Correct 84 ms 80204 KB Output is correct
49 Correct 360 ms 98392 KB Output is correct
50 Correct 456 ms 98340 KB Output is correct
51 Correct 390 ms 98348 KB Output is correct
52 Correct 328 ms 98244 KB Output is correct
53 Correct 356 ms 97296 KB Output is correct
54 Correct 293 ms 93308 KB Output is correct
55 Correct 404 ms 98636 KB Output is correct
56 Correct 349 ms 98476 KB Output is correct
57 Correct 451 ms 100008 KB Output is correct
58 Correct 371 ms 98716 KB Output is correct
59 Correct 188 ms 85196 KB Output is correct
60 Correct 357 ms 97944 KB Output is correct
61 Correct 372 ms 97488 KB Output is correct
62 Correct 376 ms 98600 KB Output is correct
63 Correct 586 ms 100340 KB Output is correct
64 Correct 44 ms 77908 KB Output is correct
65 Correct 36 ms 77892 KB Output is correct
66 Correct 50 ms 78000 KB Output is correct
67 Correct 39 ms 78128 KB Output is correct
68 Correct 42 ms 77896 KB Output is correct
69 Correct 38 ms 78076 KB Output is correct
70 Correct 39 ms 78072 KB Output is correct
71 Correct 37 ms 78156 KB Output is correct
72 Correct 43 ms 77916 KB Output is correct
73 Correct 42 ms 78136 KB Output is correct
74 Correct 309 ms 103436 KB Output is correct
75 Correct 207 ms 94504 KB Output is correct
76 Correct 204 ms 94496 KB Output is correct
77 Correct 226 ms 97068 KB Output is correct
78 Correct 198 ms 104044 KB Output is correct
79 Correct 162 ms 98080 KB Output is correct
80 Correct 228 ms 100212 KB Output is correct
81 Correct 270 ms 105160 KB Output is correct
82 Correct 299 ms 107416 KB Output is correct
83 Correct 314 ms 111492 KB Output is correct
84 Correct 219 ms 96408 KB Output is correct
85 Correct 327 ms 107940 KB Output is correct
86 Correct 162 ms 87536 KB Output is correct
87 Correct 346 ms 98328 KB Output is correct
88 Correct 339 ms 98356 KB Output is correct
89 Correct 487 ms 100148 KB Output is correct
90 Correct 333 ms 108984 KB Output is correct
91 Correct 402 ms 106408 KB Output is correct
92 Correct 474 ms 102956 KB Output is correct
93 Correct 429 ms 108584 KB Output is correct
94 Correct 591 ms 111252 KB Output is correct
95 Correct 481 ms 115496 KB Output is correct
96 Correct 459 ms 100144 KB Output is correct
97 Correct 552 ms 105532 KB Output is correct