답안 #576401

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
576401 2022-06-13T05:35:05 Z Arnch 자매 도시 (APIO20_swap) C++17
100 / 100
391 ms 65620 KB
#include "swap.h"
#include<bits/stdc++.h>

using namespace std;

const int N = 2e5 + 10, inf = 2e9, Log = 25;

int n, m, up[N], vp[N], wp[N], h[N], ans[N], par[N][Log], p[N], tim;
vector<int> sor;
vector<int> child[N];
struct node {
	int par, sz, s, e, ans;
	bool isp;
	node() {
		par = sz = isp = s = e = 0;
		ans = inf;
	}
} x[N << 2];

bool cmp(int i, int j) {
	return wp[i] < wp[j];
}

int find(int u) {
	if(p[u] == u) return u;
	return p[u] = find(p[u]);
}
void merge(int u, int v, int t) {
	int X = find(u), Y = find(v);
	if(X == Y) {
		if(x[X].ans == inf) {
			x[X].ans = t;
			x[X].isp = 0; 
		}
		return;
	}
	child[tim].push_back(X), child[tim].push_back(Y);
	x[tim].sz = x[X].sz + x[Y].sz;
	x[X].par = tim, x[Y].par = tim, p[X] = tim, p[Y] = tim;
	if(x[X].isp == 0 || x[Y].isp == 0) {
		x[tim].isp = 0;
		x[tim].ans = t;
	}
	else {
		if((u == x[X].s || u == x[X].e) && (v == x[Y].s || v == x[Y].e)) {
			x[tim].isp = 1;
			x[tim].ans = inf;
			x[tim].s = (u == x[X].s) ? (x[X].e) : (x[X].s);
			x[tim].e = (v == x[Y].s) ? (x[Y].e) : (x[Y].s);
		}
		else {
			x[tim].isp = 0;
			x[tim].ans = t;
		}
	}
	tim++;
}

void dfs(int u, int p) {
	ans[u] = min(ans[u], x[u].ans);
	par[u][0] = p;
	for(int i = 1; i < Log; i++) par[u][i] = par[par[u][i - 1]][i - 1];
	for(auto i : child[u]) {
		h[i] = h[u] + 1;
		ans[i] = ans[u];
		dfs(i, u);
	}
}

int get_par(int u, int y) {
	for(int i = 0; i < Log; i++) {
		if((y >> i) & 1) u = par[u][i];	
	}
	return u;
}

int lca(int u, int v) {
	if(h[u] > h[v]) swap(u, v);
	v = get_par(v, h[v] - h[u]);
	if(u == v) return u;
	for(int i = Log - 1; i >= 0; i--) {
		if(par[u][i] != par[v][i]) {
			u = par[u][i], v = par[v][i];
		}
	}
	return par[u][0];
}

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
	n = N, m = M;
	for(int i = 0; i < m; i++) {
		up[i] = U[i], vp[i] = V[i], wp[i] = W[i];
		sor.push_back(i);
	}
	sort(sor.begin(), sor.end(), cmp);
	tim = n;
	for(int i = 0; i < 2 * n; i++) {
		p[i] = i;
		x[i].par = i, x[i].sz = 1, x[i].e = i, x[i].s = i, x[i].isp = 1;
	}
	for(auto i : sor) {
		merge(up[i], vp[i], wp[i]);
	}
	for(int i = 0; i < 2 * N; i++) ans[i] = inf;
	dfs(tim - 1, tim - 1);
}

int getMinimumFuelCapacity(int X, int Y) {
	int lc = lca(X, Y);
	if(ans[lc] >= inf) return -1;
	return ans[lc];
}

Compilation message

swap.cpp: In constructor 'node::node()':
swap.cpp:15:22: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   15 |   par = sz = isp = s = e = 0;
      |                    ~~^~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23764 KB Output is correct
2 Correct 11 ms 23764 KB Output is correct
3 Correct 12 ms 23764 KB Output is correct
4 Correct 11 ms 23924 KB Output is correct
5 Correct 12 ms 24064 KB Output is correct
6 Correct 13 ms 24140 KB Output is correct
7 Correct 14 ms 24020 KB Output is correct
8 Correct 13 ms 24148 KB Output is correct
9 Correct 123 ms 48108 KB Output is correct
10 Correct 152 ms 53568 KB Output is correct
11 Correct 139 ms 53172 KB Output is correct
12 Correct 152 ms 54816 KB Output is correct
13 Correct 148 ms 57968 KB Output is correct
14 Correct 123 ms 48512 KB Output is correct
15 Correct 267 ms 57888 KB Output is correct
16 Correct 277 ms 57076 KB Output is correct
17 Correct 281 ms 58884 KB Output is correct
18 Correct 391 ms 62076 KB Output is correct
19 Correct 93 ms 33744 KB Output is correct
20 Correct 284 ms 59100 KB Output is correct
21 Correct 251 ms 58240 KB Output is correct
22 Correct 297 ms 60380 KB Output is correct
23 Correct 368 ms 63472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23764 KB Output is correct
2 Correct 11 ms 23764 KB Output is correct
3 Correct 317 ms 63936 KB Output is correct
4 Correct 321 ms 65620 KB Output is correct
5 Correct 301 ms 64832 KB Output is correct
6 Correct 362 ms 65376 KB Output is correct
7 Correct 288 ms 65232 KB Output is correct
8 Correct 320 ms 63828 KB Output is correct
9 Correct 289 ms 64848 KB Output is correct
10 Correct 350 ms 63524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23764 KB Output is correct
2 Correct 11 ms 23764 KB Output is correct
3 Correct 12 ms 23764 KB Output is correct
4 Correct 11 ms 23924 KB Output is correct
5 Correct 12 ms 24064 KB Output is correct
6 Correct 13 ms 24140 KB Output is correct
7 Correct 14 ms 24020 KB Output is correct
8 Correct 13 ms 24148 KB Output is correct
9 Correct 12 ms 23764 KB Output is correct
10 Correct 13 ms 24128 KB Output is correct
11 Correct 13 ms 24064 KB Output is correct
12 Correct 15 ms 24020 KB Output is correct
13 Correct 12 ms 24056 KB Output is correct
14 Correct 13 ms 24060 KB Output is correct
15 Correct 13 ms 24064 KB Output is correct
16 Correct 14 ms 24020 KB Output is correct
17 Correct 13 ms 24148 KB Output is correct
18 Correct 14 ms 24048 KB Output is correct
19 Correct 13 ms 24032 KB Output is correct
20 Correct 18 ms 24076 KB Output is correct
21 Correct 13 ms 24020 KB Output is correct
22 Correct 13 ms 23936 KB Output is correct
23 Correct 12 ms 24020 KB Output is correct
24 Correct 13 ms 24148 KB Output is correct
25 Correct 13 ms 24064 KB Output is correct
26 Correct 13 ms 24064 KB Output is correct
27 Correct 14 ms 24148 KB Output is correct
28 Correct 15 ms 24148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 11 ms 23764 KB Output is correct
3 Correct 11 ms 23764 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 11 ms 23924 KB Output is correct
6 Correct 12 ms 24064 KB Output is correct
7 Correct 13 ms 24140 KB Output is correct
8 Correct 14 ms 24020 KB Output is correct
9 Correct 13 ms 24148 KB Output is correct
10 Correct 123 ms 48108 KB Output is correct
11 Correct 152 ms 53568 KB Output is correct
12 Correct 139 ms 53172 KB Output is correct
13 Correct 152 ms 54816 KB Output is correct
14 Correct 148 ms 57968 KB Output is correct
15 Correct 13 ms 24128 KB Output is correct
16 Correct 13 ms 24064 KB Output is correct
17 Correct 15 ms 24020 KB Output is correct
18 Correct 12 ms 24056 KB Output is correct
19 Correct 13 ms 24060 KB Output is correct
20 Correct 13 ms 24064 KB Output is correct
21 Correct 14 ms 24020 KB Output is correct
22 Correct 13 ms 24148 KB Output is correct
23 Correct 14 ms 24048 KB Output is correct
24 Correct 13 ms 24032 KB Output is correct
25 Correct 18 ms 24076 KB Output is correct
26 Correct 13 ms 24020 KB Output is correct
27 Correct 13 ms 23936 KB Output is correct
28 Correct 12 ms 24020 KB Output is correct
29 Correct 13 ms 24148 KB Output is correct
30 Correct 13 ms 24064 KB Output is correct
31 Correct 13 ms 24064 KB Output is correct
32 Correct 14 ms 24148 KB Output is correct
33 Correct 15 ms 24148 KB Output is correct
34 Correct 32 ms 28024 KB Output is correct
35 Correct 149 ms 54472 KB Output is correct
36 Correct 146 ms 54588 KB Output is correct
37 Correct 152 ms 54468 KB Output is correct
38 Correct 179 ms 54056 KB Output is correct
39 Correct 150 ms 53908 KB Output is correct
40 Correct 131 ms 51436 KB Output is correct
41 Correct 143 ms 54940 KB Output is correct
42 Correct 155 ms 54516 KB Output is correct
43 Correct 143 ms 58728 KB Output is correct
44 Correct 156 ms 54852 KB Output is correct
45 Correct 183 ms 54040 KB Output is correct
46 Correct 150 ms 54660 KB Output is correct
47 Correct 142 ms 54516 KB Output is correct
48 Correct 162 ms 56124 KB Output is correct
49 Correct 82 ms 33056 KB Output is correct
50 Correct 69 ms 32836 KB Output is correct
51 Correct 128 ms 48704 KB Output is correct
52 Correct 204 ms 58556 KB Output is correct
53 Correct 200 ms 60372 KB Output is correct
54 Correct 211 ms 60624 KB Output is correct
55 Correct 152 ms 58348 KB Output is correct
56 Correct 208 ms 61188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23764 KB Output is correct
2 Correct 11 ms 23764 KB Output is correct
3 Correct 12 ms 23764 KB Output is correct
4 Correct 11 ms 23924 KB Output is correct
5 Correct 12 ms 24064 KB Output is correct
6 Correct 13 ms 24140 KB Output is correct
7 Correct 14 ms 24020 KB Output is correct
8 Correct 13 ms 24148 KB Output is correct
9 Correct 123 ms 48108 KB Output is correct
10 Correct 152 ms 53568 KB Output is correct
11 Correct 139 ms 53172 KB Output is correct
12 Correct 152 ms 54816 KB Output is correct
13 Correct 148 ms 57968 KB Output is correct
14 Correct 123 ms 48512 KB Output is correct
15 Correct 267 ms 57888 KB Output is correct
16 Correct 277 ms 57076 KB Output is correct
17 Correct 281 ms 58884 KB Output is correct
18 Correct 391 ms 62076 KB Output is correct
19 Correct 317 ms 63936 KB Output is correct
20 Correct 321 ms 65620 KB Output is correct
21 Correct 301 ms 64832 KB Output is correct
22 Correct 362 ms 65376 KB Output is correct
23 Correct 288 ms 65232 KB Output is correct
24 Correct 320 ms 63828 KB Output is correct
25 Correct 289 ms 64848 KB Output is correct
26 Correct 350 ms 63524 KB Output is correct
27 Correct 13 ms 24128 KB Output is correct
28 Correct 13 ms 24064 KB Output is correct
29 Correct 15 ms 24020 KB Output is correct
30 Correct 12 ms 24056 KB Output is correct
31 Correct 13 ms 24060 KB Output is correct
32 Correct 13 ms 24064 KB Output is correct
33 Correct 14 ms 24020 KB Output is correct
34 Correct 13 ms 24148 KB Output is correct
35 Correct 14 ms 24048 KB Output is correct
36 Correct 32 ms 28024 KB Output is correct
37 Correct 149 ms 54472 KB Output is correct
38 Correct 146 ms 54588 KB Output is correct
39 Correct 152 ms 54468 KB Output is correct
40 Correct 179 ms 54056 KB Output is correct
41 Correct 150 ms 53908 KB Output is correct
42 Correct 131 ms 51436 KB Output is correct
43 Correct 143 ms 54940 KB Output is correct
44 Correct 155 ms 54516 KB Output is correct
45 Correct 143 ms 58728 KB Output is correct
46 Correct 156 ms 54852 KB Output is correct
47 Correct 41 ms 28116 KB Output is correct
48 Correct 276 ms 59476 KB Output is correct
49 Correct 272 ms 59548 KB Output is correct
50 Correct 289 ms 59292 KB Output is correct
51 Correct 277 ms 59240 KB Output is correct
52 Correct 274 ms 57468 KB Output is correct
53 Correct 245 ms 50184 KB Output is correct
54 Correct 264 ms 60344 KB Output is correct
55 Correct 263 ms 59436 KB Output is correct
56 Correct 356 ms 62724 KB Output is correct
57 Correct 322 ms 60504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 11 ms 23764 KB Output is correct
3 Correct 11 ms 23764 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 11 ms 23924 KB Output is correct
6 Correct 12 ms 24064 KB Output is correct
7 Correct 13 ms 24140 KB Output is correct
8 Correct 14 ms 24020 KB Output is correct
9 Correct 13 ms 24148 KB Output is correct
10 Correct 123 ms 48108 KB Output is correct
11 Correct 152 ms 53568 KB Output is correct
12 Correct 139 ms 53172 KB Output is correct
13 Correct 152 ms 54816 KB Output is correct
14 Correct 148 ms 57968 KB Output is correct
15 Correct 123 ms 48512 KB Output is correct
16 Correct 267 ms 57888 KB Output is correct
17 Correct 277 ms 57076 KB Output is correct
18 Correct 281 ms 58884 KB Output is correct
19 Correct 391 ms 62076 KB Output is correct
20 Correct 317 ms 63936 KB Output is correct
21 Correct 321 ms 65620 KB Output is correct
22 Correct 301 ms 64832 KB Output is correct
23 Correct 362 ms 65376 KB Output is correct
24 Correct 288 ms 65232 KB Output is correct
25 Correct 320 ms 63828 KB Output is correct
26 Correct 289 ms 64848 KB Output is correct
27 Correct 350 ms 63524 KB Output is correct
28 Correct 13 ms 24128 KB Output is correct
29 Correct 13 ms 24064 KB Output is correct
30 Correct 15 ms 24020 KB Output is correct
31 Correct 12 ms 24056 KB Output is correct
32 Correct 13 ms 24060 KB Output is correct
33 Correct 13 ms 24064 KB Output is correct
34 Correct 14 ms 24020 KB Output is correct
35 Correct 13 ms 24148 KB Output is correct
36 Correct 14 ms 24048 KB Output is correct
37 Correct 32 ms 28024 KB Output is correct
38 Correct 149 ms 54472 KB Output is correct
39 Correct 146 ms 54588 KB Output is correct
40 Correct 152 ms 54468 KB Output is correct
41 Correct 179 ms 54056 KB Output is correct
42 Correct 150 ms 53908 KB Output is correct
43 Correct 131 ms 51436 KB Output is correct
44 Correct 143 ms 54940 KB Output is correct
45 Correct 155 ms 54516 KB Output is correct
46 Correct 143 ms 58728 KB Output is correct
47 Correct 156 ms 54852 KB Output is correct
48 Correct 41 ms 28116 KB Output is correct
49 Correct 276 ms 59476 KB Output is correct
50 Correct 272 ms 59548 KB Output is correct
51 Correct 289 ms 59292 KB Output is correct
52 Correct 277 ms 59240 KB Output is correct
53 Correct 274 ms 57468 KB Output is correct
54 Correct 245 ms 50184 KB Output is correct
55 Correct 264 ms 60344 KB Output is correct
56 Correct 263 ms 59436 KB Output is correct
57 Correct 356 ms 62724 KB Output is correct
58 Correct 322 ms 60504 KB Output is correct
59 Correct 93 ms 33744 KB Output is correct
60 Correct 284 ms 59100 KB Output is correct
61 Correct 251 ms 58240 KB Output is correct
62 Correct 297 ms 60380 KB Output is correct
63 Correct 368 ms 63472 KB Output is correct
64 Correct 13 ms 24032 KB Output is correct
65 Correct 18 ms 24076 KB Output is correct
66 Correct 13 ms 24020 KB Output is correct
67 Correct 13 ms 23936 KB Output is correct
68 Correct 12 ms 24020 KB Output is correct
69 Correct 13 ms 24148 KB Output is correct
70 Correct 13 ms 24064 KB Output is correct
71 Correct 13 ms 24064 KB Output is correct
72 Correct 14 ms 24148 KB Output is correct
73 Correct 15 ms 24148 KB Output is correct
74 Correct 183 ms 54040 KB Output is correct
75 Correct 150 ms 54660 KB Output is correct
76 Correct 142 ms 54516 KB Output is correct
77 Correct 162 ms 56124 KB Output is correct
78 Correct 82 ms 33056 KB Output is correct
79 Correct 69 ms 32836 KB Output is correct
80 Correct 128 ms 48704 KB Output is correct
81 Correct 204 ms 58556 KB Output is correct
82 Correct 200 ms 60372 KB Output is correct
83 Correct 211 ms 60624 KB Output is correct
84 Correct 152 ms 58348 KB Output is correct
85 Correct 208 ms 61188 KB Output is correct
86 Correct 88 ms 36276 KB Output is correct
87 Correct 268 ms 59432 KB Output is correct
88 Correct 297 ms 59408 KB Output is correct
89 Correct 320 ms 59108 KB Output is correct
90 Correct 171 ms 37716 KB Output is correct
91 Correct 183 ms 39984 KB Output is correct
92 Correct 265 ms 53632 KB Output is correct
93 Correct 316 ms 63192 KB Output is correct
94 Correct 343 ms 64588 KB Output is correct
95 Correct 306 ms 64436 KB Output is correct
96 Correct 358 ms 62960 KB Output is correct
97 Correct 356 ms 63748 KB Output is correct