답안 #401679

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
401679 2021-05-10T16:56:36 Z hoaphat1 자매 도시 (APIO20_swap) C++17
100 / 100
717 ms 53124 KB
#include<bits/stdc++.h>
 
using namespace std;

struct graph {
	int n, nlog;
	vector<vector<pair<int,int>>> g;
	vector<vector<int>> pr;
	vector<vector<int>> val;
	vector<int> pos;
	vector<int> end;
	graph() {
		
	}
	void resize(int n) {
		g.resize(n);
		pos.resize(n);
		end.resize(n);
		nlog = 0;
		while (1 << nlog <= n) nlog++;
		pr.resize(n, vector<int> (nlog));
		val.resize(n, vector<int> (nlog));
	}
	void add(int u, int v, int w) {
		g[u].emplace_back(v, w);
		g[v].emplace_back(u, w);
	}
	void dfs(int v, int p = 0) {
		static int count = -1;
		pos[v] = ++count;
		pr[v][0] = p;
		for (int i = 1; i < nlog; i++) {
			pr[v][i] = pr[pr[v][i-1]][i-1];
			val[v][i] = max(val[v][i-1], val[pr[v][i-1]][i-1]);
		}
		for (auto&x : g[v]) {
			int u = x.first;
			if (u != p) {
				val[u][0] = x.second;
				dfs(u, v);
			}
		}
		end[v] = count;
	}
	bool anc(int x, int y) {
		return pos[x] <= pos[y] && end[y] <= end[x];
	}
	int lca(int x, int y) {
		if (anc(x, y)) return x;
		if (anc(y, x)) return y;
		for (int i = nlog - 1; i >= 0; i--) {
			if (pr[x][i] && !anc(pr[x][i], y)) x = pr[x][i];
		}
		return pr[x][0];
	}
	int get(int x, int y) {
		int p = lca(x, y);
		int ans = 0;
		for (int i = nlog - 1; i >= 0; i--) {
			if (pr[x][i] && anc(p, pr[x][i])) {
				ans = max(ans, val[x][i]);
				x = pr[x][i];
			}
			if (pr[y][i] && anc(p, pr[y][i])) {
				ans = max(ans, val[y][i]);
				y = pr[y][i];
			}
		}
		return ans;
	}
};

struct dsu {
	vector<int> p;
	int n;
	dsu(int n) {
		resize(n);
	}
	dsu() {
	}
	void resize(int _n) {
		n = _n;
		p = vector<int> (n, -1);
	}
	int get(int x) {
		return p[x] < 0 ? x : p[x] = get(p[x]);
	}
	bool unite(int x, int y) {
		x = get(x); y = get(y);
		if (x != y) {
			if (p[x] < p[y]) swap(x, y);
			p[y] += p[x];
			p[x] = y;
			return true;
		}
		return false;
	}
} d;

const int N = 1e5 + 3;
int n, m;
vector<tuple<int,int,int>> edges;
int deg[N];
int event[N];
vector<int> belong[N];
graph g;

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++) edges.emplace_back(w[i], u[i], v[i]);
	sort(edges.begin(), edges.end());
	d.resize(n);
	g.resize(n);
	for (int i = 0; i < n; i++) belong[i] = {i};
	for (auto&x : edges) {
		deg[get<1>(x)]++;
		deg[get<2>(x)]++;
		int u = d.get(get<1>(x)), v = d.get(get<2>(x));
		bool to = (deg[get<1>(x)] >= 3 || deg[get<2>(x)] >= 3);
		if (u == v) {
			if (!event[u]) {
				for (auto&y : belong[u]) event[y] = get<0> (x);
				belong[u].clear();
			}
		}
		else {
			d.unite(u, v);
			g.add(get<1>(x), get<2>(x), get<0>(x));
			if (!event[u] && !event[v]) {
				if (belong[u].size() < belong[v].size()) swap(u, v);
				for (auto&x : belong[v]) belong[u].push_back(x);
				belong[v].clear();
				if (to) {
					for (auto&y : belong[u]) event[y] = get<0>(x);
					belong[u].clear();
				}
				else {
					if (u != d.get(u)) swap(belong[u], belong[v]);
				}
			}
			else {
				for (auto&y : belong[u]) event[y] = get<0>(x);
				belong[u].clear();
				for (auto&y : belong[v]) event[y] = get<0>(x);
				belong[v].clear();
			}
		}
	}
	g.dfs(0, 0);
}

int getMinimumFuelCapacity(int x, int y) {
	if (!event[x] || !event[y]) return -1;
	return max({event[x], event[y], g.get(x, y)});
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2764 KB Output is correct
5 Correct 3 ms 2892 KB Output is correct
6 Correct 3 ms 2892 KB Output is correct
7 Correct 3 ms 2892 KB Output is correct
8 Correct 3 ms 2884 KB Output is correct
9 Correct 200 ms 34112 KB Output is correct
10 Correct 269 ms 41260 KB Output is correct
11 Correct 260 ms 40356 KB Output is correct
12 Correct 276 ms 42824 KB Output is correct
13 Correct 237 ms 40748 KB Output is correct
14 Correct 202 ms 33740 KB Output is correct
15 Correct 327 ms 47796 KB Output is correct
16 Correct 319 ms 45764 KB Output is correct
17 Correct 343 ms 49756 KB Output is correct
18 Correct 301 ms 46392 KB Output is correct
19 Correct 140 ms 13988 KB Output is correct
20 Correct 633 ms 48916 KB Output is correct
21 Correct 644 ms 47272 KB Output is correct
22 Correct 690 ms 50880 KB Output is correct
23 Correct 643 ms 48044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 281 ms 38908 KB Output is correct
4 Correct 286 ms 44488 KB Output is correct
5 Correct 283 ms 43660 KB Output is correct
6 Correct 280 ms 44296 KB Output is correct
7 Correct 285 ms 44128 KB Output is correct
8 Correct 281 ms 42600 KB Output is correct
9 Correct 286 ms 43704 KB Output is correct
10 Correct 278 ms 42280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2764 KB Output is correct
5 Correct 3 ms 2892 KB Output is correct
6 Correct 3 ms 2892 KB Output is correct
7 Correct 3 ms 2892 KB Output is correct
8 Correct 3 ms 2884 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 3 ms 2892 KB Output is correct
11 Correct 3 ms 2924 KB Output is correct
12 Correct 3 ms 2892 KB Output is correct
13 Correct 3 ms 2764 KB Output is correct
14 Correct 4 ms 2892 KB Output is correct
15 Correct 3 ms 2892 KB Output is correct
16 Correct 4 ms 2892 KB Output is correct
17 Correct 3 ms 2892 KB Output is correct
18 Correct 3 ms 2892 KB Output is correct
19 Correct 3 ms 2764 KB Output is correct
20 Correct 3 ms 2892 KB Output is correct
21 Correct 3 ms 2892 KB Output is correct
22 Correct 3 ms 2764 KB Output is correct
23 Correct 3 ms 2764 KB Output is correct
24 Correct 3 ms 2892 KB Output is correct
25 Correct 3 ms 2892 KB Output is correct
26 Correct 3 ms 2892 KB Output is correct
27 Correct 3 ms 2892 KB Output is correct
28 Correct 3 ms 2892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2764 KB Output is correct
6 Correct 3 ms 2892 KB Output is correct
7 Correct 3 ms 2892 KB Output is correct
8 Correct 3 ms 2892 KB Output is correct
9 Correct 3 ms 2884 KB Output is correct
10 Correct 200 ms 34112 KB Output is correct
11 Correct 269 ms 41260 KB Output is correct
12 Correct 260 ms 40356 KB Output is correct
13 Correct 276 ms 42824 KB Output is correct
14 Correct 237 ms 40748 KB Output is correct
15 Correct 3 ms 2892 KB Output is correct
16 Correct 3 ms 2924 KB Output is correct
17 Correct 3 ms 2892 KB Output is correct
18 Correct 3 ms 2764 KB Output is correct
19 Correct 4 ms 2892 KB Output is correct
20 Correct 3 ms 2892 KB Output is correct
21 Correct 4 ms 2892 KB Output is correct
22 Correct 3 ms 2892 KB Output is correct
23 Correct 3 ms 2892 KB Output is correct
24 Correct 3 ms 2764 KB Output is correct
25 Correct 3 ms 2892 KB Output is correct
26 Correct 3 ms 2892 KB Output is correct
27 Correct 3 ms 2764 KB Output is correct
28 Correct 3 ms 2764 KB Output is correct
29 Correct 3 ms 2892 KB Output is correct
30 Correct 3 ms 2892 KB Output is correct
31 Correct 3 ms 2892 KB Output is correct
32 Correct 3 ms 2892 KB Output is correct
33 Correct 3 ms 2892 KB Output is correct
34 Correct 19 ms 6964 KB Output is correct
35 Correct 276 ms 41944 KB Output is correct
36 Correct 265 ms 40372 KB Output is correct
37 Correct 259 ms 38760 KB Output is correct
38 Correct 250 ms 37728 KB Output is correct
39 Correct 220 ms 36812 KB Output is correct
40 Correct 196 ms 34024 KB Output is correct
41 Correct 270 ms 41768 KB Output is correct
42 Correct 270 ms 42808 KB Output is correct
43 Correct 227 ms 39608 KB Output is correct
44 Correct 212 ms 37724 KB Output is correct
45 Correct 184 ms 32512 KB Output is correct
46 Correct 267 ms 39992 KB Output is correct
47 Correct 231 ms 37744 KB Output is correct
48 Correct 201 ms 36920 KB Output is correct
49 Correct 76 ms 9484 KB Output is correct
50 Correct 60 ms 8908 KB Output is correct
51 Correct 164 ms 27244 KB Output is correct
52 Correct 302 ms 42544 KB Output is correct
53 Correct 255 ms 40960 KB Output is correct
54 Correct 323 ms 45344 KB Output is correct
55 Correct 242 ms 40276 KB Output is correct
56 Correct 243 ms 40108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2764 KB Output is correct
5 Correct 3 ms 2892 KB Output is correct
6 Correct 3 ms 2892 KB Output is correct
7 Correct 3 ms 2892 KB Output is correct
8 Correct 3 ms 2884 KB Output is correct
9 Correct 200 ms 34112 KB Output is correct
10 Correct 269 ms 41260 KB Output is correct
11 Correct 260 ms 40356 KB Output is correct
12 Correct 276 ms 42824 KB Output is correct
13 Correct 237 ms 40748 KB Output is correct
14 Correct 202 ms 33740 KB Output is correct
15 Correct 327 ms 47796 KB Output is correct
16 Correct 319 ms 45764 KB Output is correct
17 Correct 343 ms 49756 KB Output is correct
18 Correct 301 ms 46392 KB Output is correct
19 Correct 281 ms 38908 KB Output is correct
20 Correct 286 ms 44488 KB Output is correct
21 Correct 283 ms 43660 KB Output is correct
22 Correct 280 ms 44296 KB Output is correct
23 Correct 285 ms 44128 KB Output is correct
24 Correct 281 ms 42600 KB Output is correct
25 Correct 286 ms 43704 KB Output is correct
26 Correct 278 ms 42280 KB Output is correct
27 Correct 3 ms 2892 KB Output is correct
28 Correct 3 ms 2924 KB Output is correct
29 Correct 3 ms 2892 KB Output is correct
30 Correct 3 ms 2764 KB Output is correct
31 Correct 4 ms 2892 KB Output is correct
32 Correct 3 ms 2892 KB Output is correct
33 Correct 4 ms 2892 KB Output is correct
34 Correct 3 ms 2892 KB Output is correct
35 Correct 3 ms 2892 KB Output is correct
36 Correct 19 ms 6964 KB Output is correct
37 Correct 276 ms 41944 KB Output is correct
38 Correct 265 ms 40372 KB Output is correct
39 Correct 259 ms 38760 KB Output is correct
40 Correct 250 ms 37728 KB Output is correct
41 Correct 220 ms 36812 KB Output is correct
42 Correct 196 ms 34024 KB Output is correct
43 Correct 270 ms 41768 KB Output is correct
44 Correct 270 ms 42808 KB Output is correct
45 Correct 227 ms 39608 KB Output is correct
46 Correct 212 ms 37724 KB Output is correct
47 Correct 32 ms 7412 KB Output is correct
48 Correct 674 ms 48100 KB Output is correct
49 Correct 708 ms 46428 KB Output is correct
50 Correct 660 ms 45492 KB Output is correct
51 Correct 630 ms 44744 KB Output is correct
52 Correct 520 ms 41832 KB Output is correct
53 Correct 364 ms 33068 KB Output is correct
54 Correct 684 ms 48996 KB Output is correct
55 Correct 674 ms 49528 KB Output is correct
56 Correct 624 ms 46748 KB Output is correct
57 Correct 490 ms 45344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2764 KB Output is correct
6 Correct 3 ms 2892 KB Output is correct
7 Correct 3 ms 2892 KB Output is correct
8 Correct 3 ms 2892 KB Output is correct
9 Correct 3 ms 2884 KB Output is correct
10 Correct 200 ms 34112 KB Output is correct
11 Correct 269 ms 41260 KB Output is correct
12 Correct 260 ms 40356 KB Output is correct
13 Correct 276 ms 42824 KB Output is correct
14 Correct 237 ms 40748 KB Output is correct
15 Correct 202 ms 33740 KB Output is correct
16 Correct 327 ms 47796 KB Output is correct
17 Correct 319 ms 45764 KB Output is correct
18 Correct 343 ms 49756 KB Output is correct
19 Correct 301 ms 46392 KB Output is correct
20 Correct 281 ms 38908 KB Output is correct
21 Correct 286 ms 44488 KB Output is correct
22 Correct 283 ms 43660 KB Output is correct
23 Correct 280 ms 44296 KB Output is correct
24 Correct 285 ms 44128 KB Output is correct
25 Correct 281 ms 42600 KB Output is correct
26 Correct 286 ms 43704 KB Output is correct
27 Correct 278 ms 42280 KB Output is correct
28 Correct 3 ms 2892 KB Output is correct
29 Correct 3 ms 2924 KB Output is correct
30 Correct 3 ms 2892 KB Output is correct
31 Correct 3 ms 2764 KB Output is correct
32 Correct 4 ms 2892 KB Output is correct
33 Correct 3 ms 2892 KB Output is correct
34 Correct 4 ms 2892 KB Output is correct
35 Correct 3 ms 2892 KB Output is correct
36 Correct 3 ms 2892 KB Output is correct
37 Correct 19 ms 6964 KB Output is correct
38 Correct 276 ms 41944 KB Output is correct
39 Correct 265 ms 40372 KB Output is correct
40 Correct 259 ms 38760 KB Output is correct
41 Correct 250 ms 37728 KB Output is correct
42 Correct 220 ms 36812 KB Output is correct
43 Correct 196 ms 34024 KB Output is correct
44 Correct 270 ms 41768 KB Output is correct
45 Correct 270 ms 42808 KB Output is correct
46 Correct 227 ms 39608 KB Output is correct
47 Correct 212 ms 37724 KB Output is correct
48 Correct 32 ms 7412 KB Output is correct
49 Correct 674 ms 48100 KB Output is correct
50 Correct 708 ms 46428 KB Output is correct
51 Correct 660 ms 45492 KB Output is correct
52 Correct 630 ms 44744 KB Output is correct
53 Correct 520 ms 41832 KB Output is correct
54 Correct 364 ms 33068 KB Output is correct
55 Correct 684 ms 48996 KB Output is correct
56 Correct 674 ms 49528 KB Output is correct
57 Correct 624 ms 46748 KB Output is correct
58 Correct 490 ms 45344 KB Output is correct
59 Correct 140 ms 13988 KB Output is correct
60 Correct 633 ms 48916 KB Output is correct
61 Correct 644 ms 47272 KB Output is correct
62 Correct 690 ms 50880 KB Output is correct
63 Correct 643 ms 48044 KB Output is correct
64 Correct 3 ms 2764 KB Output is correct
65 Correct 3 ms 2892 KB Output is correct
66 Correct 3 ms 2892 KB Output is correct
67 Correct 3 ms 2764 KB Output is correct
68 Correct 3 ms 2764 KB Output is correct
69 Correct 3 ms 2892 KB Output is correct
70 Correct 3 ms 2892 KB Output is correct
71 Correct 3 ms 2892 KB Output is correct
72 Correct 3 ms 2892 KB Output is correct
73 Correct 3 ms 2892 KB Output is correct
74 Correct 184 ms 32512 KB Output is correct
75 Correct 267 ms 39992 KB Output is correct
76 Correct 231 ms 37744 KB Output is correct
77 Correct 201 ms 36920 KB Output is correct
78 Correct 76 ms 9484 KB Output is correct
79 Correct 60 ms 8908 KB Output is correct
80 Correct 164 ms 27244 KB Output is correct
81 Correct 302 ms 42544 KB Output is correct
82 Correct 255 ms 40960 KB Output is correct
83 Correct 323 ms 45344 KB Output is correct
84 Correct 242 ms 40276 KB Output is correct
85 Correct 243 ms 40108 KB Output is correct
86 Correct 99 ms 16240 KB Output is correct
87 Correct 660 ms 46212 KB Output is correct
88 Correct 674 ms 46180 KB Output is correct
89 Correct 427 ms 41660 KB Output is correct
90 Correct 179 ms 16016 KB Output is correct
91 Correct 202 ms 18472 KB Output is correct
92 Correct 360 ms 34956 KB Output is correct
93 Correct 686 ms 50124 KB Output is correct
94 Correct 500 ms 49172 KB Output is correct
95 Correct 717 ms 53124 KB Output is correct
96 Correct 621 ms 45916 KB Output is correct
97 Correct 474 ms 46824 KB Output is correct