답안 #538044

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
538044 2022-03-16T03:54:03 Z pavement 자매 도시 (APIO20_swap) C++17
100 / 100
429 ms 69872 KB
#include "swap.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
//#define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef double db;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

int N, M, dep[300005], link[100005], rep[100005], sz[100005], ep[100005][2], anc[300005][25];
bool nc[300005];
iii T[200005];
vector<int> U, V, W, adj[300005];

int find(int x) {
	if (x == link[x]) return x;
	return link[x] = find(link[x]);
}

void unite(int a, int b) {
	int u = a, v = b;
	a = find(a);
	b = find(b);
	if (a == b) return;
	if (sz[b] > sz[a]) swap(a, b);
	sz[a] += sz[b];
	link[b] = a;
	nc[a] |= nc[b];
	if (!nc[a]) {
		if (u == ep[a][0] && v == ep[b][0]) {
			ep[a][0] = ep[a][1];
			ep[a][1] = ep[b][1];
		} else if (u == ep[a][0] && v == ep[b][1]) {
			ep[a][0] = ep[a][1];
			ep[a][1] = ep[b][0];
		} else if (u == ep[a][1] && v == ep[b][0]) {
			ep[a][1] = ep[b][1];
		} else if (u == ep[a][1] && v == ep[b][1]) {
			ep[a][1] = ep[b][0];
		} else {
			swap(u, v);
			if (u == ep[a][0] && v == ep[b][0]) {
				ep[a][0] = ep[a][1];
				ep[a][1] = ep[b][1];
			} else if (u == ep[a][0] && v == ep[b][1]) {
				ep[a][0] = ep[a][1];
				ep[a][1] = ep[b][0];
			} else if (u == ep[a][1] && v == ep[b][0]) {
				ep[a][1] = ep[b][1];
			} else if (u == ep[a][1] && v == ep[b][1]) {
				ep[a][1] = ep[b][0];
			} else {
				nc[a] = 1;
			}
		}
	}
}

void dfs(int n, int e = -1) {
	anc[n][0] = e;
	for (int i = 1; i <= 20; i++)
		if (anc[n][i - 1] != -1)
			anc[n][i] = anc[anc[n][i - 1]][i - 1];
	for (auto u : adj[n])
		if (u != e) {
			dep[u] = dep[n] + 1;
			dfs(u, n);
		}
}

int lca(int u, int v) {
	if (dep[u] < dep[v]) swap(u, v);
	for (int i = 20; i >= 0; i--)
		if (dep[u] - (1 << i) >= dep[v]) u = anc[u][i];
	if (u == v) return u;
	for (int i = 20; i >= 0; i--)
		if (anc[u][i] != anc[v][i]) {
			u = anc[u][i];
			v = anc[v][i];
		}
	return anc[u][0];
}

void init(int _N, int _M, vector<int> _U, vector<int> _V, vector<int> _W) {
	N = _N;
	M = _M;
	U = _U;
	V = _V;
	W = _W;
	memset(anc, -1, sizeof anc);
	for (int i = 0; i < M; i++)
		T[i] = mt(W[i], U[i], V[i]);
	for (int i = 0; i < N; i++) {
		link[i] = rep[i] = ep[i][0] = ep[i][1] = i;
		sz[i] = 1;
	}
	sort(T, T + M);
	for (int i = 0; i < M; i++) {
		tie(W[i], U[i], V[i]) = T[i];
		int a = rep[find(U[i])], b = rep[find(V[i])];
		if (a != b) {
			adj[N + i].pb(a);
			adj[N + i].pb(b);
		} else {
			nc[find(U[i])] = 1;
			adj[N + i].pb(a);
		}
		unite(U[i], V[i]);
		nc[N + i] = nc[find(U[i])];
		rep[find(U[i])] = N + i;
	}
	dfs(N + M - 1);
}

int getMinimumFuelCapacity(int X, int Y) {
	int w = lca(X, Y);
	if (nc[w]) return W[w - N];
	for (int i = 20; i >= 0; i--)
		if (anc[w][i] != -1 && !nc[anc[w][i]])
			w = anc[w][i];
	if (anc[w][0] == -1) return -1;
	w = anc[w][0];
	return W[w - N];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 36660 KB Output is correct
2 Correct 18 ms 36668 KB Output is correct
3 Correct 17 ms 36692 KB Output is correct
4 Correct 17 ms 36676 KB Output is correct
5 Correct 18 ms 36820 KB Output is correct
6 Correct 17 ms 36820 KB Output is correct
7 Correct 18 ms 36820 KB Output is correct
8 Correct 17 ms 36808 KB Output is correct
9 Correct 115 ms 46844 KB Output is correct
10 Correct 131 ms 49052 KB Output is correct
11 Correct 130 ms 48784 KB Output is correct
12 Correct 135 ms 49604 KB Output is correct
13 Correct 126 ms 52648 KB Output is correct
14 Correct 116 ms 47052 KB Output is correct
15 Correct 289 ms 53204 KB Output is correct
16 Correct 269 ms 52872 KB Output is correct
17 Correct 267 ms 53580 KB Output is correct
18 Correct 429 ms 56624 KB Output is correct
19 Correct 144 ms 43820 KB Output is correct
20 Correct 239 ms 54288 KB Output is correct
21 Correct 282 ms 54280 KB Output is correct
22 Correct 267 ms 54996 KB Output is correct
23 Correct 360 ms 58124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 36660 KB Output is correct
2 Correct 18 ms 36668 KB Output is correct
3 Correct 337 ms 59588 KB Output is correct
4 Correct 293 ms 60256 KB Output is correct
5 Correct 297 ms 60144 KB Output is correct
6 Correct 329 ms 60148 KB Output is correct
7 Correct 291 ms 60340 KB Output is correct
8 Correct 308 ms 59516 KB Output is correct
9 Correct 330 ms 60000 KB Output is correct
10 Correct 303 ms 59300 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 36660 KB Output is correct
2 Correct 18 ms 36668 KB Output is correct
3 Correct 17 ms 36692 KB Output is correct
4 Correct 17 ms 36676 KB Output is correct
5 Correct 18 ms 36820 KB Output is correct
6 Correct 17 ms 36820 KB Output is correct
7 Correct 18 ms 36820 KB Output is correct
8 Correct 17 ms 36808 KB Output is correct
9 Correct 16 ms 36708 KB Output is correct
10 Correct 21 ms 36832 KB Output is correct
11 Correct 17 ms 36812 KB Output is correct
12 Correct 19 ms 36760 KB Output is correct
13 Correct 16 ms 36804 KB Output is correct
14 Correct 16 ms 36768 KB Output is correct
15 Correct 17 ms 36820 KB Output is correct
16 Correct 19 ms 36832 KB Output is correct
17 Correct 17 ms 36784 KB Output is correct
18 Correct 17 ms 36808 KB Output is correct
19 Correct 19 ms 36860 KB Output is correct
20 Correct 17 ms 36812 KB Output is correct
21 Correct 16 ms 36820 KB Output is correct
22 Correct 16 ms 37016 KB Output is correct
23 Correct 16 ms 36724 KB Output is correct
24 Correct 17 ms 36940 KB Output is correct
25 Correct 17 ms 36976 KB Output is correct
26 Correct 17 ms 36948 KB Output is correct
27 Correct 17 ms 36812 KB Output is correct
28 Correct 19 ms 36956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 36708 KB Output is correct
2 Correct 17 ms 36660 KB Output is correct
3 Correct 18 ms 36668 KB Output is correct
4 Correct 17 ms 36692 KB Output is correct
5 Correct 17 ms 36676 KB Output is correct
6 Correct 18 ms 36820 KB Output is correct
7 Correct 17 ms 36820 KB Output is correct
8 Correct 18 ms 36820 KB Output is correct
9 Correct 17 ms 36808 KB Output is correct
10 Correct 115 ms 46844 KB Output is correct
11 Correct 131 ms 49052 KB Output is correct
12 Correct 130 ms 48784 KB Output is correct
13 Correct 135 ms 49604 KB Output is correct
14 Correct 126 ms 52648 KB Output is correct
15 Correct 21 ms 36832 KB Output is correct
16 Correct 17 ms 36812 KB Output is correct
17 Correct 19 ms 36760 KB Output is correct
18 Correct 16 ms 36804 KB Output is correct
19 Correct 16 ms 36768 KB Output is correct
20 Correct 17 ms 36820 KB Output is correct
21 Correct 19 ms 36832 KB Output is correct
22 Correct 17 ms 36784 KB Output is correct
23 Correct 17 ms 36808 KB Output is correct
24 Correct 19 ms 36860 KB Output is correct
25 Correct 17 ms 36812 KB Output is correct
26 Correct 16 ms 36820 KB Output is correct
27 Correct 16 ms 37016 KB Output is correct
28 Correct 16 ms 36724 KB Output is correct
29 Correct 17 ms 36940 KB Output is correct
30 Correct 17 ms 36976 KB Output is correct
31 Correct 17 ms 36948 KB Output is correct
32 Correct 17 ms 36812 KB Output is correct
33 Correct 19 ms 36956 KB Output is correct
34 Correct 35 ms 38488 KB Output is correct
35 Correct 129 ms 49396 KB Output is correct
36 Correct 134 ms 49228 KB Output is correct
37 Correct 136 ms 49248 KB Output is correct
38 Correct 116 ms 49152 KB Output is correct
39 Correct 118 ms 49056 KB Output is correct
40 Correct 122 ms 48052 KB Output is correct
41 Correct 132 ms 49472 KB Output is correct
42 Correct 127 ms 49312 KB Output is correct
43 Correct 125 ms 53388 KB Output is correct
44 Correct 128 ms 49728 KB Output is correct
45 Correct 146 ms 59712 KB Output is correct
46 Correct 138 ms 49220 KB Output is correct
47 Correct 138 ms 49340 KB Output is correct
48 Correct 130 ms 52704 KB Output is correct
49 Correct 100 ms 62448 KB Output is correct
50 Correct 104 ms 56472 KB Output is correct
51 Correct 138 ms 56976 KB Output is correct
52 Correct 175 ms 59812 KB Output is correct
53 Correct 214 ms 61388 KB Output is correct
54 Correct 194 ms 65896 KB Output is correct
55 Correct 130 ms 53052 KB Output is correct
56 Correct 209 ms 63476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 36660 KB Output is correct
2 Correct 18 ms 36668 KB Output is correct
3 Correct 17 ms 36692 KB Output is correct
4 Correct 17 ms 36676 KB Output is correct
5 Correct 18 ms 36820 KB Output is correct
6 Correct 17 ms 36820 KB Output is correct
7 Correct 18 ms 36820 KB Output is correct
8 Correct 17 ms 36808 KB Output is correct
9 Correct 115 ms 46844 KB Output is correct
10 Correct 131 ms 49052 KB Output is correct
11 Correct 130 ms 48784 KB Output is correct
12 Correct 135 ms 49604 KB Output is correct
13 Correct 126 ms 52648 KB Output is correct
14 Correct 116 ms 47052 KB Output is correct
15 Correct 289 ms 53204 KB Output is correct
16 Correct 269 ms 52872 KB Output is correct
17 Correct 267 ms 53580 KB Output is correct
18 Correct 429 ms 56624 KB Output is correct
19 Correct 337 ms 59588 KB Output is correct
20 Correct 293 ms 60256 KB Output is correct
21 Correct 297 ms 60144 KB Output is correct
22 Correct 329 ms 60148 KB Output is correct
23 Correct 291 ms 60340 KB Output is correct
24 Correct 308 ms 59516 KB Output is correct
25 Correct 330 ms 60000 KB Output is correct
26 Correct 303 ms 59300 KB Output is correct
27 Correct 21 ms 36832 KB Output is correct
28 Correct 17 ms 36812 KB Output is correct
29 Correct 19 ms 36760 KB Output is correct
30 Correct 16 ms 36804 KB Output is correct
31 Correct 16 ms 36768 KB Output is correct
32 Correct 17 ms 36820 KB Output is correct
33 Correct 19 ms 36832 KB Output is correct
34 Correct 17 ms 36784 KB Output is correct
35 Correct 17 ms 36808 KB Output is correct
36 Correct 35 ms 38488 KB Output is correct
37 Correct 129 ms 49396 KB Output is correct
38 Correct 134 ms 49228 KB Output is correct
39 Correct 136 ms 49248 KB Output is correct
40 Correct 116 ms 49152 KB Output is correct
41 Correct 118 ms 49056 KB Output is correct
42 Correct 122 ms 48052 KB Output is correct
43 Correct 132 ms 49472 KB Output is correct
44 Correct 127 ms 49312 KB Output is correct
45 Correct 125 ms 53388 KB Output is correct
46 Correct 128 ms 49728 KB Output is correct
47 Correct 32 ms 38812 KB Output is correct
48 Correct 257 ms 54068 KB Output is correct
49 Correct 255 ms 54092 KB Output is correct
50 Correct 236 ms 54132 KB Output is correct
51 Correct 266 ms 54088 KB Output is correct
52 Correct 230 ms 53448 KB Output is correct
53 Correct 265 ms 51156 KB Output is correct
54 Correct 265 ms 55000 KB Output is correct
55 Correct 282 ms 54180 KB Output is correct
56 Correct 386 ms 57420 KB Output is correct
57 Correct 258 ms 55076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 36708 KB Output is correct
2 Correct 17 ms 36660 KB Output is correct
3 Correct 18 ms 36668 KB Output is correct
4 Correct 17 ms 36692 KB Output is correct
5 Correct 17 ms 36676 KB Output is correct
6 Correct 18 ms 36820 KB Output is correct
7 Correct 17 ms 36820 KB Output is correct
8 Correct 18 ms 36820 KB Output is correct
9 Correct 17 ms 36808 KB Output is correct
10 Correct 115 ms 46844 KB Output is correct
11 Correct 131 ms 49052 KB Output is correct
12 Correct 130 ms 48784 KB Output is correct
13 Correct 135 ms 49604 KB Output is correct
14 Correct 126 ms 52648 KB Output is correct
15 Correct 116 ms 47052 KB Output is correct
16 Correct 289 ms 53204 KB Output is correct
17 Correct 269 ms 52872 KB Output is correct
18 Correct 267 ms 53580 KB Output is correct
19 Correct 429 ms 56624 KB Output is correct
20 Correct 337 ms 59588 KB Output is correct
21 Correct 293 ms 60256 KB Output is correct
22 Correct 297 ms 60144 KB Output is correct
23 Correct 329 ms 60148 KB Output is correct
24 Correct 291 ms 60340 KB Output is correct
25 Correct 308 ms 59516 KB Output is correct
26 Correct 330 ms 60000 KB Output is correct
27 Correct 303 ms 59300 KB Output is correct
28 Correct 21 ms 36832 KB Output is correct
29 Correct 17 ms 36812 KB Output is correct
30 Correct 19 ms 36760 KB Output is correct
31 Correct 16 ms 36804 KB Output is correct
32 Correct 16 ms 36768 KB Output is correct
33 Correct 17 ms 36820 KB Output is correct
34 Correct 19 ms 36832 KB Output is correct
35 Correct 17 ms 36784 KB Output is correct
36 Correct 17 ms 36808 KB Output is correct
37 Correct 35 ms 38488 KB Output is correct
38 Correct 129 ms 49396 KB Output is correct
39 Correct 134 ms 49228 KB Output is correct
40 Correct 136 ms 49248 KB Output is correct
41 Correct 116 ms 49152 KB Output is correct
42 Correct 118 ms 49056 KB Output is correct
43 Correct 122 ms 48052 KB Output is correct
44 Correct 132 ms 49472 KB Output is correct
45 Correct 127 ms 49312 KB Output is correct
46 Correct 125 ms 53388 KB Output is correct
47 Correct 128 ms 49728 KB Output is correct
48 Correct 32 ms 38812 KB Output is correct
49 Correct 257 ms 54068 KB Output is correct
50 Correct 255 ms 54092 KB Output is correct
51 Correct 236 ms 54132 KB Output is correct
52 Correct 266 ms 54088 KB Output is correct
53 Correct 230 ms 53448 KB Output is correct
54 Correct 265 ms 51156 KB Output is correct
55 Correct 265 ms 55000 KB Output is correct
56 Correct 282 ms 54180 KB Output is correct
57 Correct 386 ms 57420 KB Output is correct
58 Correct 258 ms 55076 KB Output is correct
59 Correct 144 ms 43820 KB Output is correct
60 Correct 239 ms 54288 KB Output is correct
61 Correct 282 ms 54280 KB Output is correct
62 Correct 267 ms 54996 KB Output is correct
63 Correct 360 ms 58124 KB Output is correct
64 Correct 19 ms 36860 KB Output is correct
65 Correct 17 ms 36812 KB Output is correct
66 Correct 16 ms 36820 KB Output is correct
67 Correct 16 ms 37016 KB Output is correct
68 Correct 16 ms 36724 KB Output is correct
69 Correct 17 ms 36940 KB Output is correct
70 Correct 17 ms 36976 KB Output is correct
71 Correct 17 ms 36948 KB Output is correct
72 Correct 17 ms 36812 KB Output is correct
73 Correct 19 ms 36956 KB Output is correct
74 Correct 146 ms 59712 KB Output is correct
75 Correct 138 ms 49220 KB Output is correct
76 Correct 138 ms 49340 KB Output is correct
77 Correct 130 ms 52704 KB Output is correct
78 Correct 100 ms 62448 KB Output is correct
79 Correct 104 ms 56472 KB Output is correct
80 Correct 138 ms 56976 KB Output is correct
81 Correct 175 ms 59812 KB Output is correct
82 Correct 214 ms 61388 KB Output is correct
83 Correct 194 ms 65896 KB Output is correct
84 Correct 130 ms 53052 KB Output is correct
85 Correct 209 ms 63476 KB Output is correct
86 Correct 94 ms 45436 KB Output is correct
87 Correct 261 ms 54180 KB Output is correct
88 Correct 238 ms 54132 KB Output is correct
89 Correct 342 ms 57004 KB Output is correct
90 Correct 219 ms 67836 KB Output is correct
91 Correct 246 ms 65476 KB Output is correct
92 Correct 311 ms 61128 KB Output is correct
93 Correct 325 ms 64152 KB Output is correct
94 Correct 362 ms 66172 KB Output is correct
95 Correct 353 ms 69872 KB Output is correct
96 Correct 419 ms 57636 KB Output is correct
97 Correct 348 ms 62176 KB Output is correct