답안 #444598

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
444598 2021-07-14T10:49:13 Z nonsensenonsense1 자매 도시 (APIO20_swap) C++17
100 / 100
496 ms 48368 KB
#include "swap.h"
#include <cstring>
#include <utility>
#include <vector>
#include <algorithm>

const int INF = 0x3f3f3f3f;

void mnm(int &a, int b) 
{
	if (a > b) a = b;
}

void mxm(int &a, int b) 
{
	if (a < b) a = b;
}

struct dsu {
	dsu *p;
	int size;
	bool over;
	std::vector<int> v;
	dsu() {
		over = false;
		p = 0;
		size = 1;
	}
	dsu *find() {
		if (p) return p = p->find();
		return this;
	}
};

const int N = 100000;
const int M = 200000;
const int LG = 17;
int in[N], out[N], f[LG][N], up[LG][N], fi[N], deg[N];
std::vector<std::pair<int, int> > g[N];
std::pair<int, std::pair<int, int> > e[M];
dsu d[N];

void update(std::vector<int> &v, int w) 
{
	while (!v.empty()) {
		fi[v.back()] = w;
		v.pop_back();
	}
}

bool unite(dsu *a, dsu *b, int w) 
{
	a = a->find();
	b = b->find();
	if (a == b) return 0;
	if (a->size < b->size) std::swap(a, b);
	b->p = a;
	a->size += b->size;
	a->v.insert(a->v.end(), b->v.begin(), b->v.end());
	if (a->over || b->over) {
		a->over = true;
		update(a->v, w);
	}
	return 1;
}

void rm(dsu *a, int w) 
{
	a = a->find();
	if (!a->over) {
		a->over = true;
		update(a->v, w);
	}
}

void dfs(int v) 
{
	static int dt = 0;
	in[v] = dt++;
	for (int i = 0; i < (int)g[v].size(); ++i) if (g[v][i].first != f[0][v]) {
		f[0][g[v][i].first] = v;
		up[0][g[v][i].first] = g[v][i].second;
		dfs(g[v][i].first);
	}
	out[v] = dt;
}

bool parent(int u, int v) 
{
	return in[u] <= in[v] && out[u] >= out[v];
}

void init(int n, int m, std::vector<int> u, std::vector<int> v, std::vector<int> w) 
{
	memset(fi, 0x3f, n << 2);
	for (int i = 0; i < n; ++i) d[i].v.push_back(i);
	for (int i = 0; i < m; ++i) e[i] = std::make_pair(w[i], std::make_pair(u[i], v[i]));
	std::sort(e, e + m);
	for (int i = 0; i < m; ++i) {
		bool way = false;
		if (unite(d + e[i].second.first, d + e[i].second.second, e[i].first)) {
			g[e[i].second.first].push_back(std::make_pair(e[i].second.second, e[i].first));
			g[e[i].second.second].push_back(std::make_pair(e[i].second.first, e[i].first));
		}
		else way = true;
		++deg[e[i].second.first];
		++deg[e[i].second.second];
		if (way || deg[e[i].second.first] == 3 || deg[e[i].second.second] == 3) rm(d + e[i].second.first, e[i].first);
	}
	dfs(0);
	for (int j = 1; j < LG; ++j) for (int i = 0; i < n; ++i) {
		f[j][i] = f[j - 1][f[j - 1][i]];
		up[j][i] = std::max(up[j - 1][i], up[j - 1][f[j - 1][i]]);
	}
}

int getMinimumFuelCapacity(int u, int v) 
{
	int edge = std::min(fi[u], fi[v]);
	for (int t = 0; t < 2; ++t) {
		if (!parent(v, u)) {
			for (int i = LG - 1; i >= 0; --i) if (!parent(f[i][v], u)) {
				edge = std::max(edge, up[i][v]);
				v = f[i][v];
			}
			edge = std::max(edge, up[0][v]);
		}
		std::swap(u, v);
	}
	if (edge == INF) return -1;
	return edge;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6732 KB Output is correct
2 Correct 4 ms 6692 KB Output is correct
3 Correct 4 ms 6752 KB Output is correct
4 Correct 4 ms 6840 KB Output is correct
5 Correct 5 ms 6988 KB Output is correct
6 Correct 5 ms 6988 KB Output is correct
7 Correct 5 ms 6988 KB Output is correct
8 Correct 5 ms 6988 KB Output is correct
9 Correct 120 ms 33216 KB Output is correct
10 Correct 154 ms 40128 KB Output is correct
11 Correct 145 ms 39060 KB Output is correct
12 Correct 148 ms 41376 KB Output is correct
13 Correct 113 ms 40036 KB Output is correct
14 Correct 140 ms 33088 KB Output is correct
15 Correct 427 ms 44408 KB Output is correct
16 Correct 418 ms 42192 KB Output is correct
17 Correct 454 ms 46720 KB Output is correct
18 Correct 414 ms 42980 KB Output is correct
19 Correct 121 ms 17500 KB Output is correct
20 Correct 481 ms 44784 KB Output is correct
21 Correct 483 ms 43212 KB Output is correct
22 Correct 450 ms 46376 KB Output is correct
23 Correct 407 ms 44056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6732 KB Output is correct
2 Correct 4 ms 6692 KB Output is correct
3 Correct 238 ms 37564 KB Output is correct
4 Correct 240 ms 38664 KB Output is correct
5 Correct 233 ms 38280 KB Output is correct
6 Correct 240 ms 38540 KB Output is correct
7 Correct 239 ms 38536 KB Output is correct
8 Correct 212 ms 37384 KB Output is correct
9 Correct 225 ms 38328 KB Output is correct
10 Correct 219 ms 37404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6732 KB Output is correct
2 Correct 4 ms 6692 KB Output is correct
3 Correct 4 ms 6752 KB Output is correct
4 Correct 4 ms 6840 KB Output is correct
5 Correct 5 ms 6988 KB Output is correct
6 Correct 5 ms 6988 KB Output is correct
7 Correct 5 ms 6988 KB Output is correct
8 Correct 5 ms 6988 KB Output is correct
9 Correct 4 ms 6732 KB Output is correct
10 Correct 5 ms 6988 KB Output is correct
11 Correct 5 ms 6988 KB Output is correct
12 Correct 4 ms 6988 KB Output is correct
13 Correct 5 ms 6860 KB Output is correct
14 Correct 5 ms 6860 KB Output is correct
15 Correct 5 ms 6988 KB Output is correct
16 Correct 5 ms 6988 KB Output is correct
17 Correct 5 ms 6988 KB Output is correct
18 Correct 5 ms 6988 KB Output is correct
19 Correct 5 ms 6860 KB Output is correct
20 Correct 5 ms 7008 KB Output is correct
21 Correct 5 ms 6956 KB Output is correct
22 Correct 5 ms 6860 KB Output is correct
23 Correct 5 ms 6860 KB Output is correct
24 Correct 6 ms 6952 KB Output is correct
25 Correct 6 ms 6988 KB Output is correct
26 Correct 5 ms 6988 KB Output is correct
27 Correct 5 ms 6988 KB Output is correct
28 Correct 5 ms 6988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6732 KB Output is correct
2 Correct 4 ms 6732 KB Output is correct
3 Correct 4 ms 6692 KB Output is correct
4 Correct 4 ms 6752 KB Output is correct
5 Correct 4 ms 6840 KB Output is correct
6 Correct 5 ms 6988 KB Output is correct
7 Correct 5 ms 6988 KB Output is correct
8 Correct 5 ms 6988 KB Output is correct
9 Correct 5 ms 6988 KB Output is correct
10 Correct 120 ms 33216 KB Output is correct
11 Correct 154 ms 40128 KB Output is correct
12 Correct 145 ms 39060 KB Output is correct
13 Correct 148 ms 41376 KB Output is correct
14 Correct 113 ms 40036 KB Output is correct
15 Correct 5 ms 6988 KB Output is correct
16 Correct 5 ms 6988 KB Output is correct
17 Correct 4 ms 6988 KB Output is correct
18 Correct 5 ms 6860 KB Output is correct
19 Correct 5 ms 6860 KB Output is correct
20 Correct 5 ms 6988 KB Output is correct
21 Correct 5 ms 6988 KB Output is correct
22 Correct 5 ms 6988 KB Output is correct
23 Correct 5 ms 6988 KB Output is correct
24 Correct 5 ms 6860 KB Output is correct
25 Correct 5 ms 7008 KB Output is correct
26 Correct 5 ms 6956 KB Output is correct
27 Correct 5 ms 6860 KB Output is correct
28 Correct 5 ms 6860 KB Output is correct
29 Correct 6 ms 6952 KB Output is correct
30 Correct 6 ms 6988 KB Output is correct
31 Correct 5 ms 6988 KB Output is correct
32 Correct 5 ms 6988 KB Output is correct
33 Correct 5 ms 6988 KB Output is correct
34 Correct 16 ms 10336 KB Output is correct
35 Correct 153 ms 39856 KB Output is correct
36 Correct 145 ms 37552 KB Output is correct
37 Correct 152 ms 35140 KB Output is correct
38 Correct 140 ms 33956 KB Output is correct
39 Correct 132 ms 33092 KB Output is correct
40 Correct 119 ms 30900 KB Output is correct
41 Correct 147 ms 39080 KB Output is correct
42 Correct 150 ms 40132 KB Output is correct
43 Correct 113 ms 37764 KB Output is correct
44 Correct 145 ms 33912 KB Output is correct
45 Correct 130 ms 31952 KB Output is correct
46 Correct 146 ms 36528 KB Output is correct
47 Correct 135 ms 33716 KB Output is correct
48 Correct 144 ms 33476 KB Output is correct
49 Correct 66 ms 15196 KB Output is correct
50 Correct 58 ms 14884 KB Output is correct
51 Correct 108 ms 27716 KB Output is correct
52 Correct 179 ms 40612 KB Output is correct
53 Correct 193 ms 38800 KB Output is correct
54 Correct 214 ms 44532 KB Output is correct
55 Correct 109 ms 38804 KB Output is correct
56 Correct 166 ms 37872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6732 KB Output is correct
2 Correct 4 ms 6692 KB Output is correct
3 Correct 4 ms 6752 KB Output is correct
4 Correct 4 ms 6840 KB Output is correct
5 Correct 5 ms 6988 KB Output is correct
6 Correct 5 ms 6988 KB Output is correct
7 Correct 5 ms 6988 KB Output is correct
8 Correct 5 ms 6988 KB Output is correct
9 Correct 120 ms 33216 KB Output is correct
10 Correct 154 ms 40128 KB Output is correct
11 Correct 145 ms 39060 KB Output is correct
12 Correct 148 ms 41376 KB Output is correct
13 Correct 113 ms 40036 KB Output is correct
14 Correct 140 ms 33088 KB Output is correct
15 Correct 427 ms 44408 KB Output is correct
16 Correct 418 ms 42192 KB Output is correct
17 Correct 454 ms 46720 KB Output is correct
18 Correct 414 ms 42980 KB Output is correct
19 Correct 238 ms 37564 KB Output is correct
20 Correct 240 ms 38664 KB Output is correct
21 Correct 233 ms 38280 KB Output is correct
22 Correct 240 ms 38540 KB Output is correct
23 Correct 239 ms 38536 KB Output is correct
24 Correct 212 ms 37384 KB Output is correct
25 Correct 225 ms 38328 KB Output is correct
26 Correct 219 ms 37404 KB Output is correct
27 Correct 5 ms 6988 KB Output is correct
28 Correct 5 ms 6988 KB Output is correct
29 Correct 4 ms 6988 KB Output is correct
30 Correct 5 ms 6860 KB Output is correct
31 Correct 5 ms 6860 KB Output is correct
32 Correct 5 ms 6988 KB Output is correct
33 Correct 5 ms 6988 KB Output is correct
34 Correct 5 ms 6988 KB Output is correct
35 Correct 5 ms 6988 KB Output is correct
36 Correct 16 ms 10336 KB Output is correct
37 Correct 153 ms 39856 KB Output is correct
38 Correct 145 ms 37552 KB Output is correct
39 Correct 152 ms 35140 KB Output is correct
40 Correct 140 ms 33956 KB Output is correct
41 Correct 132 ms 33092 KB Output is correct
42 Correct 119 ms 30900 KB Output is correct
43 Correct 147 ms 39080 KB Output is correct
44 Correct 150 ms 40132 KB Output is correct
45 Correct 113 ms 37764 KB Output is correct
46 Correct 145 ms 33912 KB Output is correct
47 Correct 27 ms 10584 KB Output is correct
48 Correct 457 ms 43576 KB Output is correct
49 Correct 474 ms 41364 KB Output is correct
50 Correct 432 ms 40020 KB Output is correct
51 Correct 439 ms 39036 KB Output is correct
52 Correct 349 ms 36780 KB Output is correct
53 Correct 268 ms 30640 KB Output is correct
54 Correct 472 ms 43840 KB Output is correct
55 Correct 496 ms 44452 KB Output is correct
56 Correct 424 ms 43176 KB Output is correct
57 Correct 365 ms 39452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6732 KB Output is correct
2 Correct 4 ms 6732 KB Output is correct
3 Correct 4 ms 6692 KB Output is correct
4 Correct 4 ms 6752 KB Output is correct
5 Correct 4 ms 6840 KB Output is correct
6 Correct 5 ms 6988 KB Output is correct
7 Correct 5 ms 6988 KB Output is correct
8 Correct 5 ms 6988 KB Output is correct
9 Correct 5 ms 6988 KB Output is correct
10 Correct 120 ms 33216 KB Output is correct
11 Correct 154 ms 40128 KB Output is correct
12 Correct 145 ms 39060 KB Output is correct
13 Correct 148 ms 41376 KB Output is correct
14 Correct 113 ms 40036 KB Output is correct
15 Correct 140 ms 33088 KB Output is correct
16 Correct 427 ms 44408 KB Output is correct
17 Correct 418 ms 42192 KB Output is correct
18 Correct 454 ms 46720 KB Output is correct
19 Correct 414 ms 42980 KB Output is correct
20 Correct 238 ms 37564 KB Output is correct
21 Correct 240 ms 38664 KB Output is correct
22 Correct 233 ms 38280 KB Output is correct
23 Correct 240 ms 38540 KB Output is correct
24 Correct 239 ms 38536 KB Output is correct
25 Correct 212 ms 37384 KB Output is correct
26 Correct 225 ms 38328 KB Output is correct
27 Correct 219 ms 37404 KB Output is correct
28 Correct 5 ms 6988 KB Output is correct
29 Correct 5 ms 6988 KB Output is correct
30 Correct 4 ms 6988 KB Output is correct
31 Correct 5 ms 6860 KB Output is correct
32 Correct 5 ms 6860 KB Output is correct
33 Correct 5 ms 6988 KB Output is correct
34 Correct 5 ms 6988 KB Output is correct
35 Correct 5 ms 6988 KB Output is correct
36 Correct 5 ms 6988 KB Output is correct
37 Correct 16 ms 10336 KB Output is correct
38 Correct 153 ms 39856 KB Output is correct
39 Correct 145 ms 37552 KB Output is correct
40 Correct 152 ms 35140 KB Output is correct
41 Correct 140 ms 33956 KB Output is correct
42 Correct 132 ms 33092 KB Output is correct
43 Correct 119 ms 30900 KB Output is correct
44 Correct 147 ms 39080 KB Output is correct
45 Correct 150 ms 40132 KB Output is correct
46 Correct 113 ms 37764 KB Output is correct
47 Correct 145 ms 33912 KB Output is correct
48 Correct 27 ms 10584 KB Output is correct
49 Correct 457 ms 43576 KB Output is correct
50 Correct 474 ms 41364 KB Output is correct
51 Correct 432 ms 40020 KB Output is correct
52 Correct 439 ms 39036 KB Output is correct
53 Correct 349 ms 36780 KB Output is correct
54 Correct 268 ms 30640 KB Output is correct
55 Correct 472 ms 43840 KB Output is correct
56 Correct 496 ms 44452 KB Output is correct
57 Correct 424 ms 43176 KB Output is correct
58 Correct 365 ms 39452 KB Output is correct
59 Correct 121 ms 17500 KB Output is correct
60 Correct 481 ms 44784 KB Output is correct
61 Correct 483 ms 43212 KB Output is correct
62 Correct 450 ms 46376 KB Output is correct
63 Correct 407 ms 44056 KB Output is correct
64 Correct 5 ms 6860 KB Output is correct
65 Correct 5 ms 7008 KB Output is correct
66 Correct 5 ms 6956 KB Output is correct
67 Correct 5 ms 6860 KB Output is correct
68 Correct 5 ms 6860 KB Output is correct
69 Correct 6 ms 6952 KB Output is correct
70 Correct 6 ms 6988 KB Output is correct
71 Correct 5 ms 6988 KB Output is correct
72 Correct 5 ms 6988 KB Output is correct
73 Correct 5 ms 6988 KB Output is correct
74 Correct 130 ms 31952 KB Output is correct
75 Correct 146 ms 36528 KB Output is correct
76 Correct 135 ms 33716 KB Output is correct
77 Correct 144 ms 33476 KB Output is correct
78 Correct 66 ms 15196 KB Output is correct
79 Correct 58 ms 14884 KB Output is correct
80 Correct 108 ms 27716 KB Output is correct
81 Correct 179 ms 40612 KB Output is correct
82 Correct 193 ms 38800 KB Output is correct
83 Correct 214 ms 44532 KB Output is correct
84 Correct 109 ms 38804 KB Output is correct
85 Correct 166 ms 37872 KB Output is correct
86 Correct 73 ms 17296 KB Output is correct
87 Correct 469 ms 41008 KB Output is correct
88 Correct 459 ms 40880 KB Output is correct
89 Correct 304 ms 36880 KB Output is correct
90 Correct 167 ms 19840 KB Output is correct
91 Correct 190 ms 21616 KB Output is correct
92 Correct 260 ms 32624 KB Output is correct
93 Correct 471 ms 45100 KB Output is correct
94 Correct 399 ms 43424 KB Output is correct
95 Correct 487 ms 48368 KB Output is correct
96 Correct 416 ms 41620 KB Output is correct
97 Correct 349 ms 40980 KB Output is correct