답안 #965495

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
965495 2024-04-18T18:21:33 Z socpite 자매 도시 (APIO20_swap) C++14
100 / 100
248 ms 65176 KB
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;

const int maxn = 3e5+5;
const int INF = 1e9+5;
const int lg = 20;

int n, m;
int Wmax;

int sp[lg][maxn];

vector<int> g[maxn];
vector<pair<int, pair<int, int>>> E;

int up[maxn], L[maxn], R[maxn], dep[maxn], dp[maxn];

int Find(int x){
	return up[x] != -1 ? up[x] = Find(up[x]) : x;
}

void Union(int a, int b, int w){
	int ra = Find(a);
	int rb = Find(b);
	if(ra != rb){
		if(L[ra] == -1 || L[rb] == -1){
			L[n] = R[n] = -1;
			dp[n] = w;
		}
		else {
			if(a != L[ra])swap(L[ra], R[ra]);
			if(b != L[rb])swap(L[rb], R[rb]);
			if(a == L[ra] && b == L[rb]){
				L[n] = R[ra];
				R[n] = R[rb];
				dp[n] = INF;
			}
			else {
				L[n] = R[n] = -1;
				dp[n] = w;
			}
		}
		g[n].push_back(ra);
		g[n].push_back(rb);
		up[ra] = n;
		up[rb] = n;
	}
	else {
		g[n].push_back(ra);
		L[n] = R[n] = -1;
		up[ra] = n;
		dp[n] = w;	
	}
	up[n] = -1;
	n++;
}

void dfs(int x){
	for(auto v: g[x]){
		sp[0][v] = x;
		dep[v] = dep[x] + 1;
		dp[v] = min(dp[v], dp[x]);
		dfs(v);
	}
}

void build(){
	for(int i = 1; i < lg; i++){
		for(int j = 0; j < n; j++){
			if(sp[i-1][j] == -1)sp[i][j] = -1;
			else sp[i][j] = sp[i-1][sp[i-1][j]];
		}
	}
}

void tup(int &x, int dist){
	for(int i = 0; i < lg; i++){
		if(dist&(1<<i))x = sp[i][x];
	}
}

void init(int N, int M, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
	n = N;
	m = M;
	for(int i = 0; i < n; i++){
		up[i] = -1;
		dp[i] = INF;
		L[i] = R[i] = i;
	}
	for(int i = 0; i < m; i++){
		E.push_back({W[i], {U[i], V[i]}});
	}
	sort(E.begin(), E.end());
	for(auto v: E)Union(v.second.first, v.second.second, v.first);
	sp[0][n-1] = -1;
	dfs(n-1);
	build();
}

int getMinimumFuelCapacity(int a, int b) {
	if(dep[a] < dep[b])swap(a, b);
	tup(a, dep[a] - dep[b]);
	for(int i = lg-1; i >= 0; i--){
		if(sp[i][a] != sp[i][b]){
			a = sp[i][a];
			b = sp[i][b];
		}
	}
	a = sp[0][a];
	if(dp[a] == INF)return -1;
	return dp[a];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 33116 KB Output is correct
2 Correct 6 ms 33116 KB Output is correct
3 Correct 5 ms 33112 KB Output is correct
4 Correct 5 ms 33116 KB Output is correct
5 Correct 6 ms 33372 KB Output is correct
6 Correct 5 ms 33268 KB Output is correct
7 Correct 5 ms 33372 KB Output is correct
8 Correct 5 ms 33368 KB Output is correct
9 Correct 44 ms 42952 KB Output is correct
10 Correct 56 ms 44484 KB Output is correct
11 Correct 55 ms 44484 KB Output is correct
12 Correct 58 ms 44872 KB Output is correct
13 Correct 56 ms 48072 KB Output is correct
14 Correct 54 ms 43212 KB Output is correct
15 Correct 173 ms 48208 KB Output is correct
16 Correct 150 ms 48084 KB Output is correct
17 Correct 155 ms 48688 KB Output is correct
18 Correct 180 ms 52092 KB Output is correct
19 Correct 68 ms 39972 KB Output is correct
20 Correct 139 ms 49228 KB Output is correct
21 Correct 165 ms 49288 KB Output is correct
22 Correct 150 ms 49712 KB Output is correct
23 Correct 188 ms 53036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 33116 KB Output is correct
2 Correct 6 ms 33116 KB Output is correct
3 Correct 184 ms 54740 KB Output is correct
4 Correct 180 ms 55304 KB Output is correct
5 Correct 165 ms 55152 KB Output is correct
6 Correct 166 ms 54756 KB Output is correct
7 Correct 166 ms 55112 KB Output is correct
8 Correct 212 ms 54388 KB Output is correct
9 Correct 167 ms 55128 KB Output is correct
10 Correct 215 ms 54508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 33116 KB Output is correct
2 Correct 6 ms 33116 KB Output is correct
3 Correct 5 ms 33112 KB Output is correct
4 Correct 5 ms 33116 KB Output is correct
5 Correct 6 ms 33372 KB Output is correct
6 Correct 5 ms 33268 KB Output is correct
7 Correct 5 ms 33372 KB Output is correct
8 Correct 5 ms 33368 KB Output is correct
9 Correct 5 ms 33284 KB Output is correct
10 Correct 5 ms 33204 KB Output is correct
11 Correct 5 ms 33372 KB Output is correct
12 Correct 5 ms 33372 KB Output is correct
13 Correct 5 ms 33116 KB Output is correct
14 Correct 5 ms 33372 KB Output is correct
15 Correct 6 ms 33372 KB Output is correct
16 Correct 6 ms 33368 KB Output is correct
17 Correct 5 ms 33372 KB Output is correct
18 Correct 5 ms 33372 KB Output is correct
19 Correct 5 ms 33372 KB Output is correct
20 Correct 5 ms 33372 KB Output is correct
21 Correct 5 ms 33372 KB Output is correct
22 Correct 5 ms 33372 KB Output is correct
23 Correct 5 ms 33372 KB Output is correct
24 Correct 6 ms 33372 KB Output is correct
25 Correct 5 ms 33372 KB Output is correct
26 Correct 9 ms 33372 KB Output is correct
27 Correct 5 ms 33372 KB Output is correct
28 Correct 5 ms 33372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 33284 KB Output is correct
2 Correct 9 ms 33116 KB Output is correct
3 Correct 6 ms 33116 KB Output is correct
4 Correct 5 ms 33112 KB Output is correct
5 Correct 5 ms 33116 KB Output is correct
6 Correct 6 ms 33372 KB Output is correct
7 Correct 5 ms 33268 KB Output is correct
8 Correct 5 ms 33372 KB Output is correct
9 Correct 5 ms 33368 KB Output is correct
10 Correct 44 ms 42952 KB Output is correct
11 Correct 56 ms 44484 KB Output is correct
12 Correct 55 ms 44484 KB Output is correct
13 Correct 58 ms 44872 KB Output is correct
14 Correct 56 ms 48072 KB Output is correct
15 Correct 5 ms 33204 KB Output is correct
16 Correct 5 ms 33372 KB Output is correct
17 Correct 5 ms 33372 KB Output is correct
18 Correct 5 ms 33116 KB Output is correct
19 Correct 5 ms 33372 KB Output is correct
20 Correct 6 ms 33372 KB Output is correct
21 Correct 6 ms 33368 KB Output is correct
22 Correct 5 ms 33372 KB Output is correct
23 Correct 5 ms 33372 KB Output is correct
24 Correct 5 ms 33372 KB Output is correct
25 Correct 5 ms 33372 KB Output is correct
26 Correct 5 ms 33372 KB Output is correct
27 Correct 5 ms 33372 KB Output is correct
28 Correct 5 ms 33372 KB Output is correct
29 Correct 6 ms 33372 KB Output is correct
30 Correct 5 ms 33372 KB Output is correct
31 Correct 9 ms 33372 KB Output is correct
32 Correct 5 ms 33372 KB Output is correct
33 Correct 5 ms 33372 KB Output is correct
34 Correct 11 ms 34588 KB Output is correct
35 Correct 59 ms 44512 KB Output is correct
36 Correct 58 ms 44756 KB Output is correct
37 Correct 64 ms 44736 KB Output is correct
38 Correct 55 ms 44484 KB Output is correct
39 Correct 54 ms 44488 KB Output is correct
40 Correct 53 ms 43716 KB Output is correct
41 Correct 57 ms 44988 KB Output is correct
42 Correct 55 ms 44624 KB Output is correct
43 Correct 52 ms 48836 KB Output is correct
44 Correct 58 ms 45000 KB Output is correct
45 Correct 87 ms 55436 KB Output is correct
46 Correct 55 ms 44540 KB Output is correct
47 Correct 59 ms 44676 KB Output is correct
48 Correct 67 ms 48096 KB Output is correct
49 Correct 79 ms 59068 KB Output is correct
50 Correct 63 ms 53412 KB Output is correct
51 Correct 74 ms 52768 KB Output is correct
52 Correct 101 ms 54980 KB Output is correct
53 Correct 103 ms 56424 KB Output is correct
54 Correct 113 ms 60140 KB Output is correct
55 Correct 53 ms 48336 KB Output is correct
56 Correct 107 ms 58076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 33116 KB Output is correct
2 Correct 6 ms 33116 KB Output is correct
3 Correct 5 ms 33112 KB Output is correct
4 Correct 5 ms 33116 KB Output is correct
5 Correct 6 ms 33372 KB Output is correct
6 Correct 5 ms 33268 KB Output is correct
7 Correct 5 ms 33372 KB Output is correct
8 Correct 5 ms 33368 KB Output is correct
9 Correct 44 ms 42952 KB Output is correct
10 Correct 56 ms 44484 KB Output is correct
11 Correct 55 ms 44484 KB Output is correct
12 Correct 58 ms 44872 KB Output is correct
13 Correct 56 ms 48072 KB Output is correct
14 Correct 54 ms 43212 KB Output is correct
15 Correct 173 ms 48208 KB Output is correct
16 Correct 150 ms 48084 KB Output is correct
17 Correct 155 ms 48688 KB Output is correct
18 Correct 180 ms 52092 KB Output is correct
19 Correct 184 ms 54740 KB Output is correct
20 Correct 180 ms 55304 KB Output is correct
21 Correct 165 ms 55152 KB Output is correct
22 Correct 166 ms 54756 KB Output is correct
23 Correct 166 ms 55112 KB Output is correct
24 Correct 212 ms 54388 KB Output is correct
25 Correct 167 ms 55128 KB Output is correct
26 Correct 215 ms 54508 KB Output is correct
27 Correct 5 ms 33204 KB Output is correct
28 Correct 5 ms 33372 KB Output is correct
29 Correct 5 ms 33372 KB Output is correct
30 Correct 5 ms 33116 KB Output is correct
31 Correct 5 ms 33372 KB Output is correct
32 Correct 6 ms 33372 KB Output is correct
33 Correct 6 ms 33368 KB Output is correct
34 Correct 5 ms 33372 KB Output is correct
35 Correct 5 ms 33372 KB Output is correct
36 Correct 11 ms 34588 KB Output is correct
37 Correct 59 ms 44512 KB Output is correct
38 Correct 58 ms 44756 KB Output is correct
39 Correct 64 ms 44736 KB Output is correct
40 Correct 55 ms 44484 KB Output is correct
41 Correct 54 ms 44488 KB Output is correct
42 Correct 53 ms 43716 KB Output is correct
43 Correct 57 ms 44988 KB Output is correct
44 Correct 55 ms 44624 KB Output is correct
45 Correct 52 ms 48836 KB Output is correct
46 Correct 58 ms 45000 KB Output is correct
47 Correct 19 ms 35100 KB Output is correct
48 Correct 163 ms 49444 KB Output is correct
49 Correct 142 ms 49440 KB Output is correct
50 Correct 194 ms 49572 KB Output is correct
51 Correct 146 ms 49528 KB Output is correct
52 Correct 142 ms 49012 KB Output is correct
53 Correct 121 ms 47544 KB Output is correct
54 Correct 148 ms 50480 KB Output is correct
55 Correct 201 ms 49592 KB Output is correct
56 Correct 188 ms 52916 KB Output is correct
57 Correct 182 ms 50376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 33284 KB Output is correct
2 Correct 9 ms 33116 KB Output is correct
3 Correct 6 ms 33116 KB Output is correct
4 Correct 5 ms 33112 KB Output is correct
5 Correct 5 ms 33116 KB Output is correct
6 Correct 6 ms 33372 KB Output is correct
7 Correct 5 ms 33268 KB Output is correct
8 Correct 5 ms 33372 KB Output is correct
9 Correct 5 ms 33368 KB Output is correct
10 Correct 44 ms 42952 KB Output is correct
11 Correct 56 ms 44484 KB Output is correct
12 Correct 55 ms 44484 KB Output is correct
13 Correct 58 ms 44872 KB Output is correct
14 Correct 56 ms 48072 KB Output is correct
15 Correct 54 ms 43212 KB Output is correct
16 Correct 173 ms 48208 KB Output is correct
17 Correct 150 ms 48084 KB Output is correct
18 Correct 155 ms 48688 KB Output is correct
19 Correct 180 ms 52092 KB Output is correct
20 Correct 184 ms 54740 KB Output is correct
21 Correct 180 ms 55304 KB Output is correct
22 Correct 165 ms 55152 KB Output is correct
23 Correct 166 ms 54756 KB Output is correct
24 Correct 166 ms 55112 KB Output is correct
25 Correct 212 ms 54388 KB Output is correct
26 Correct 167 ms 55128 KB Output is correct
27 Correct 215 ms 54508 KB Output is correct
28 Correct 5 ms 33204 KB Output is correct
29 Correct 5 ms 33372 KB Output is correct
30 Correct 5 ms 33372 KB Output is correct
31 Correct 5 ms 33116 KB Output is correct
32 Correct 5 ms 33372 KB Output is correct
33 Correct 6 ms 33372 KB Output is correct
34 Correct 6 ms 33368 KB Output is correct
35 Correct 5 ms 33372 KB Output is correct
36 Correct 5 ms 33372 KB Output is correct
37 Correct 11 ms 34588 KB Output is correct
38 Correct 59 ms 44512 KB Output is correct
39 Correct 58 ms 44756 KB Output is correct
40 Correct 64 ms 44736 KB Output is correct
41 Correct 55 ms 44484 KB Output is correct
42 Correct 54 ms 44488 KB Output is correct
43 Correct 53 ms 43716 KB Output is correct
44 Correct 57 ms 44988 KB Output is correct
45 Correct 55 ms 44624 KB Output is correct
46 Correct 52 ms 48836 KB Output is correct
47 Correct 58 ms 45000 KB Output is correct
48 Correct 19 ms 35100 KB Output is correct
49 Correct 163 ms 49444 KB Output is correct
50 Correct 142 ms 49440 KB Output is correct
51 Correct 194 ms 49572 KB Output is correct
52 Correct 146 ms 49528 KB Output is correct
53 Correct 142 ms 49012 KB Output is correct
54 Correct 121 ms 47544 KB Output is correct
55 Correct 148 ms 50480 KB Output is correct
56 Correct 201 ms 49592 KB Output is correct
57 Correct 188 ms 52916 KB Output is correct
58 Correct 182 ms 50376 KB Output is correct
59 Correct 68 ms 39972 KB Output is correct
60 Correct 139 ms 49228 KB Output is correct
61 Correct 165 ms 49288 KB Output is correct
62 Correct 150 ms 49712 KB Output is correct
63 Correct 188 ms 53036 KB Output is correct
64 Correct 5 ms 33372 KB Output is correct
65 Correct 5 ms 33372 KB Output is correct
66 Correct 5 ms 33372 KB Output is correct
67 Correct 5 ms 33372 KB Output is correct
68 Correct 5 ms 33372 KB Output is correct
69 Correct 6 ms 33372 KB Output is correct
70 Correct 5 ms 33372 KB Output is correct
71 Correct 9 ms 33372 KB Output is correct
72 Correct 5 ms 33372 KB Output is correct
73 Correct 5 ms 33372 KB Output is correct
74 Correct 87 ms 55436 KB Output is correct
75 Correct 55 ms 44540 KB Output is correct
76 Correct 59 ms 44676 KB Output is correct
77 Correct 67 ms 48096 KB Output is correct
78 Correct 79 ms 59068 KB Output is correct
79 Correct 63 ms 53412 KB Output is correct
80 Correct 74 ms 52768 KB Output is correct
81 Correct 101 ms 54980 KB Output is correct
82 Correct 103 ms 56424 KB Output is correct
83 Correct 113 ms 60140 KB Output is correct
84 Correct 53 ms 48336 KB Output is correct
85 Correct 107 ms 58076 KB Output is correct
86 Correct 54 ms 42832 KB Output is correct
87 Correct 169 ms 49528 KB Output is correct
88 Correct 139 ms 49492 KB Output is correct
89 Correct 248 ms 52520 KB Output is correct
90 Correct 149 ms 65176 KB Output is correct
91 Correct 152 ms 63204 KB Output is correct
92 Correct 168 ms 56864 KB Output is correct
93 Correct 200 ms 58660 KB Output is correct
94 Correct 228 ms 60580 KB Output is correct
95 Correct 231 ms 64140 KB Output is correct
96 Correct 201 ms 53040 KB Output is correct
97 Correct 232 ms 57212 KB Output is correct