답안 #562272

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
562272 2022-05-14T08:42:30 Z hollwo_pelw 자매 도시 (APIO20_swap) C++17
100 / 100
374 ms 46120 KB
#include "swap.h"

#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 5, M = 2e5 + 5;

int n, m, fa[N + M], val[N + M], col[N + M], deg[N];
int par[19][N + M], nxt[N + M], d[N + M];
vector<int> adj[N + M];

inline int find(int u) { return fa[u] == u ? u : fa[u] = find(fa[u]); }
inline void merge(int u, int v, int w) {
	deg[u] ++, deg[v] ++;
	bool ok = deg[u] >= 3 || deg[v] >= 3;
	if ((u = find(u)) == (v = find(v))) {
		if (!col[u]) {
			fa[u] = ++ n;
			adj[n].push_back(u);
			col[n] = 1, val[n] = w;
		}
		return ;
	}
	fa[u] = fa[v] = ++ n;
	adj[n].push_back(u);
	adj[n].push_back(v);
	col[n] = ok | col[u] | col[v], val[n] = w;
}

void pre_dfs(int u, int f = 0) {
	if (col[u]) f = u;
	nxt[u] = f;

	for (int v : adj[u]) {
		d[v] = d[par[0][v] = u] + 1;
		for (int i = 1; i < 19; i++)
			par[i][v] = par[i - 1][par[i - 1][v]];

		pre_dfs(v, f);
	}
}

inline int lca(int u, int v) {
	if (d[u] > d[v]) swap(u, v);
	
	for (int i = 19; i --; )
		if ((d[v] - d[u]) >> i & 1)
			v = par[i][v];
	if (u == v) return u;
	
	for (int i = 19; i --; )
		if (par[i][u] != par[i][v]) {
			u = par[i][u];
			v = par[i][v];
		}
	return par[0][u];
}

void init(int _n, int _m, vector<int> U, vector<int> V, vector<int> W) {
	n = _n, m = _m;

	iota(fa, fa + N + M, 0);

	vector<int> ids(m);
	iota(ids.begin(), ids.end(), 0);
	sort(ids.begin(), ids.end(), [&](const int &i, const int &j){
		return W[i] < W[j];
	});

	for (int i : ids)
		merge(U[i] + 1, V[i] + 1, W[i]);

	val[0] = -1, pre_dfs(n);
}

int getMinimumFuelCapacity(int X, int Y) {
	return val[nxt[lca(X + 1, Y + 1)]];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8660 KB Output is correct
2 Correct 6 ms 8660 KB Output is correct
3 Correct 5 ms 8660 KB Output is correct
4 Correct 6 ms 8660 KB Output is correct
5 Correct 5 ms 8788 KB Output is correct
6 Correct 5 ms 8772 KB Output is correct
7 Correct 5 ms 8916 KB Output is correct
8 Correct 5 ms 8988 KB Output is correct
9 Correct 92 ms 27352 KB Output is correct
10 Correct 130 ms 31536 KB Output is correct
11 Correct 114 ms 31116 KB Output is correct
12 Correct 124 ms 32424 KB Output is correct
13 Correct 119 ms 36228 KB Output is correct
14 Correct 117 ms 27380 KB Output is correct
15 Correct 255 ms 33240 KB Output is correct
16 Correct 251 ms 32584 KB Output is correct
17 Correct 256 ms 33932 KB Output is correct
18 Correct 333 ms 37876 KB Output is correct
19 Correct 81 ms 16068 KB Output is correct
20 Correct 298 ms 34340 KB Output is correct
21 Correct 246 ms 33684 KB Output is correct
22 Correct 263 ms 35100 KB Output is correct
23 Correct 330 ms 39004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8660 KB Output is correct
2 Correct 6 ms 8660 KB Output is correct
3 Correct 317 ms 40828 KB Output is correct
4 Correct 312 ms 46120 KB Output is correct
5 Correct 333 ms 45424 KB Output is correct
6 Correct 314 ms 45976 KB Output is correct
7 Correct 350 ms 45984 KB Output is correct
8 Correct 307 ms 44484 KB Output is correct
9 Correct 308 ms 45536 KB Output is correct
10 Correct 311 ms 44248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8660 KB Output is correct
2 Correct 6 ms 8660 KB Output is correct
3 Correct 5 ms 8660 KB Output is correct
4 Correct 6 ms 8660 KB Output is correct
5 Correct 5 ms 8788 KB Output is correct
6 Correct 5 ms 8772 KB Output is correct
7 Correct 5 ms 8916 KB Output is correct
8 Correct 5 ms 8988 KB Output is correct
9 Correct 5 ms 8660 KB Output is correct
10 Correct 6 ms 8900 KB Output is correct
11 Correct 5 ms 8904 KB Output is correct
12 Correct 5 ms 8788 KB Output is correct
13 Correct 6 ms 8772 KB Output is correct
14 Correct 6 ms 8768 KB Output is correct
15 Correct 5 ms 8916 KB Output is correct
16 Correct 6 ms 8916 KB Output is correct
17 Correct 7 ms 8900 KB Output is correct
18 Correct 7 ms 8916 KB Output is correct
19 Correct 6 ms 8788 KB Output is correct
20 Correct 6 ms 8904 KB Output is correct
21 Correct 6 ms 8772 KB Output is correct
22 Correct 6 ms 8684 KB Output is correct
23 Correct 5 ms 8788 KB Output is correct
24 Correct 6 ms 8916 KB Output is correct
25 Correct 6 ms 8940 KB Output is correct
26 Correct 6 ms 8916 KB Output is correct
27 Correct 6 ms 8916 KB Output is correct
28 Correct 6 ms 8916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8660 KB Output is correct
2 Correct 5 ms 8660 KB Output is correct
3 Correct 6 ms 8660 KB Output is correct
4 Correct 5 ms 8660 KB Output is correct
5 Correct 6 ms 8660 KB Output is correct
6 Correct 5 ms 8788 KB Output is correct
7 Correct 5 ms 8772 KB Output is correct
8 Correct 5 ms 8916 KB Output is correct
9 Correct 5 ms 8988 KB Output is correct
10 Correct 92 ms 27352 KB Output is correct
11 Correct 130 ms 31536 KB Output is correct
12 Correct 114 ms 31116 KB Output is correct
13 Correct 124 ms 32424 KB Output is correct
14 Correct 119 ms 36228 KB Output is correct
15 Correct 6 ms 8900 KB Output is correct
16 Correct 5 ms 8904 KB Output is correct
17 Correct 5 ms 8788 KB Output is correct
18 Correct 6 ms 8772 KB Output is correct
19 Correct 6 ms 8768 KB Output is correct
20 Correct 5 ms 8916 KB Output is correct
21 Correct 6 ms 8916 KB Output is correct
22 Correct 7 ms 8900 KB Output is correct
23 Correct 7 ms 8916 KB Output is correct
24 Correct 6 ms 8788 KB Output is correct
25 Correct 6 ms 8904 KB Output is correct
26 Correct 6 ms 8772 KB Output is correct
27 Correct 6 ms 8684 KB Output is correct
28 Correct 5 ms 8788 KB Output is correct
29 Correct 6 ms 8916 KB Output is correct
30 Correct 6 ms 8940 KB Output is correct
31 Correct 6 ms 8916 KB Output is correct
32 Correct 6 ms 8916 KB Output is correct
33 Correct 6 ms 8916 KB Output is correct
34 Correct 16 ms 12116 KB Output is correct
35 Correct 131 ms 33952 KB Output is correct
36 Correct 129 ms 33948 KB Output is correct
37 Correct 116 ms 33852 KB Output is correct
38 Correct 130 ms 33484 KB Output is correct
39 Correct 125 ms 33440 KB Output is correct
40 Correct 105 ms 31304 KB Output is correct
41 Correct 119 ms 34124 KB Output is correct
42 Correct 119 ms 33848 KB Output is correct
43 Correct 104 ms 38988 KB Output is correct
44 Correct 125 ms 34308 KB Output is correct
45 Correct 122 ms 33912 KB Output is correct
46 Correct 117 ms 33852 KB Output is correct
47 Correct 130 ms 33964 KB Output is correct
48 Correct 121 ms 35776 KB Output is correct
49 Correct 65 ms 15976 KB Output is correct
50 Correct 54 ms 15924 KB Output is correct
51 Correct 113 ms 29708 KB Output is correct
52 Correct 179 ms 37236 KB Output is correct
53 Correct 189 ms 42936 KB Output is correct
54 Correct 185 ms 38812 KB Output is correct
55 Correct 127 ms 38568 KB Output is correct
56 Correct 163 ms 40108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8660 KB Output is correct
2 Correct 6 ms 8660 KB Output is correct
3 Correct 5 ms 8660 KB Output is correct
4 Correct 6 ms 8660 KB Output is correct
5 Correct 5 ms 8788 KB Output is correct
6 Correct 5 ms 8772 KB Output is correct
7 Correct 5 ms 8916 KB Output is correct
8 Correct 5 ms 8988 KB Output is correct
9 Correct 92 ms 27352 KB Output is correct
10 Correct 130 ms 31536 KB Output is correct
11 Correct 114 ms 31116 KB Output is correct
12 Correct 124 ms 32424 KB Output is correct
13 Correct 119 ms 36228 KB Output is correct
14 Correct 117 ms 27380 KB Output is correct
15 Correct 255 ms 33240 KB Output is correct
16 Correct 251 ms 32584 KB Output is correct
17 Correct 256 ms 33932 KB Output is correct
18 Correct 333 ms 37876 KB Output is correct
19 Correct 317 ms 40828 KB Output is correct
20 Correct 312 ms 46120 KB Output is correct
21 Correct 333 ms 45424 KB Output is correct
22 Correct 314 ms 45976 KB Output is correct
23 Correct 350 ms 45984 KB Output is correct
24 Correct 307 ms 44484 KB Output is correct
25 Correct 308 ms 45536 KB Output is correct
26 Correct 311 ms 44248 KB Output is correct
27 Correct 6 ms 8900 KB Output is correct
28 Correct 5 ms 8904 KB Output is correct
29 Correct 5 ms 8788 KB Output is correct
30 Correct 6 ms 8772 KB Output is correct
31 Correct 6 ms 8768 KB Output is correct
32 Correct 5 ms 8916 KB Output is correct
33 Correct 6 ms 8916 KB Output is correct
34 Correct 7 ms 8900 KB Output is correct
35 Correct 7 ms 8916 KB Output is correct
36 Correct 16 ms 12116 KB Output is correct
37 Correct 131 ms 33952 KB Output is correct
38 Correct 129 ms 33948 KB Output is correct
39 Correct 116 ms 33852 KB Output is correct
40 Correct 130 ms 33484 KB Output is correct
41 Correct 125 ms 33440 KB Output is correct
42 Correct 105 ms 31304 KB Output is correct
43 Correct 119 ms 34124 KB Output is correct
44 Correct 119 ms 33848 KB Output is correct
45 Correct 104 ms 38988 KB Output is correct
46 Correct 125 ms 34308 KB Output is correct
47 Correct 24 ms 12320 KB Output is correct
48 Correct 291 ms 38348 KB Output is correct
49 Correct 318 ms 38428 KB Output is correct
50 Correct 302 ms 38372 KB Output is correct
51 Correct 252 ms 38280 KB Output is correct
52 Correct 256 ms 36776 KB Output is correct
53 Correct 274 ms 31160 KB Output is correct
54 Correct 278 ms 39244 KB Output is correct
55 Correct 311 ms 38336 KB Output is correct
56 Correct 348 ms 42400 KB Output is correct
57 Correct 287 ms 39360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8660 KB Output is correct
2 Correct 5 ms 8660 KB Output is correct
3 Correct 6 ms 8660 KB Output is correct
4 Correct 5 ms 8660 KB Output is correct
5 Correct 6 ms 8660 KB Output is correct
6 Correct 5 ms 8788 KB Output is correct
7 Correct 5 ms 8772 KB Output is correct
8 Correct 5 ms 8916 KB Output is correct
9 Correct 5 ms 8988 KB Output is correct
10 Correct 92 ms 27352 KB Output is correct
11 Correct 130 ms 31536 KB Output is correct
12 Correct 114 ms 31116 KB Output is correct
13 Correct 124 ms 32424 KB Output is correct
14 Correct 119 ms 36228 KB Output is correct
15 Correct 117 ms 27380 KB Output is correct
16 Correct 255 ms 33240 KB Output is correct
17 Correct 251 ms 32584 KB Output is correct
18 Correct 256 ms 33932 KB Output is correct
19 Correct 333 ms 37876 KB Output is correct
20 Correct 317 ms 40828 KB Output is correct
21 Correct 312 ms 46120 KB Output is correct
22 Correct 333 ms 45424 KB Output is correct
23 Correct 314 ms 45976 KB Output is correct
24 Correct 350 ms 45984 KB Output is correct
25 Correct 307 ms 44484 KB Output is correct
26 Correct 308 ms 45536 KB Output is correct
27 Correct 311 ms 44248 KB Output is correct
28 Correct 6 ms 8900 KB Output is correct
29 Correct 5 ms 8904 KB Output is correct
30 Correct 5 ms 8788 KB Output is correct
31 Correct 6 ms 8772 KB Output is correct
32 Correct 6 ms 8768 KB Output is correct
33 Correct 5 ms 8916 KB Output is correct
34 Correct 6 ms 8916 KB Output is correct
35 Correct 7 ms 8900 KB Output is correct
36 Correct 7 ms 8916 KB Output is correct
37 Correct 16 ms 12116 KB Output is correct
38 Correct 131 ms 33952 KB Output is correct
39 Correct 129 ms 33948 KB Output is correct
40 Correct 116 ms 33852 KB Output is correct
41 Correct 130 ms 33484 KB Output is correct
42 Correct 125 ms 33440 KB Output is correct
43 Correct 105 ms 31304 KB Output is correct
44 Correct 119 ms 34124 KB Output is correct
45 Correct 119 ms 33848 KB Output is correct
46 Correct 104 ms 38988 KB Output is correct
47 Correct 125 ms 34308 KB Output is correct
48 Correct 24 ms 12320 KB Output is correct
49 Correct 291 ms 38348 KB Output is correct
50 Correct 318 ms 38428 KB Output is correct
51 Correct 302 ms 38372 KB Output is correct
52 Correct 252 ms 38280 KB Output is correct
53 Correct 256 ms 36776 KB Output is correct
54 Correct 274 ms 31160 KB Output is correct
55 Correct 278 ms 39244 KB Output is correct
56 Correct 311 ms 38336 KB Output is correct
57 Correct 348 ms 42400 KB Output is correct
58 Correct 287 ms 39360 KB Output is correct
59 Correct 81 ms 16068 KB Output is correct
60 Correct 298 ms 34340 KB Output is correct
61 Correct 246 ms 33684 KB Output is correct
62 Correct 263 ms 35100 KB Output is correct
63 Correct 330 ms 39004 KB Output is correct
64 Correct 6 ms 8788 KB Output is correct
65 Correct 6 ms 8904 KB Output is correct
66 Correct 6 ms 8772 KB Output is correct
67 Correct 6 ms 8684 KB Output is correct
68 Correct 5 ms 8788 KB Output is correct
69 Correct 6 ms 8916 KB Output is correct
70 Correct 6 ms 8940 KB Output is correct
71 Correct 6 ms 8916 KB Output is correct
72 Correct 6 ms 8916 KB Output is correct
73 Correct 6 ms 8916 KB Output is correct
74 Correct 122 ms 33912 KB Output is correct
75 Correct 117 ms 33852 KB Output is correct
76 Correct 130 ms 33964 KB Output is correct
77 Correct 121 ms 35776 KB Output is correct
78 Correct 65 ms 15976 KB Output is correct
79 Correct 54 ms 15924 KB Output is correct
80 Correct 113 ms 29708 KB Output is correct
81 Correct 179 ms 37236 KB Output is correct
82 Correct 189 ms 42936 KB Output is correct
83 Correct 185 ms 38812 KB Output is correct
84 Correct 127 ms 38568 KB Output is correct
85 Correct 163 ms 40108 KB Output is correct
86 Correct 72 ms 19132 KB Output is correct
87 Correct 245 ms 38436 KB Output is correct
88 Correct 274 ms 38420 KB Output is correct
89 Correct 268 ms 38708 KB Output is correct
90 Correct 163 ms 19912 KB Output is correct
91 Correct 168 ms 22100 KB Output is correct
92 Correct 238 ms 34116 KB Output is correct
93 Correct 307 ms 41224 KB Output is correct
94 Correct 353 ms 45536 KB Output is correct
95 Correct 319 ms 42580 KB Output is correct
96 Correct 374 ms 42832 KB Output is correct
97 Correct 303 ms 42488 KB Output is correct