답안 #297864

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
297864 2020-09-12T05:35:07 Z tmwilliamlin168 자매 도시 (APIO20_swap) C++14
100 / 100
559 ms 36712 KB
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;

#define ar array

const int mxN=1e5;
int n, a[mxN], r[mxN], d[mxN], anc[mxN][17], b1[mxN][17];
vector<ar<int, 2>> adj[mxN];
vector<int> adj2[mxN];

int find(int x) {
	return x^r[x]?r[x]=find(r[x]):x;
}

void dfs2(int u, int w) {
	a[u]=min(w, a[u]);
	for(int v : adj2[u])
		dfs2(v, w);
	adj2[u].clear();
}

bool join(int x, int y, int w) {
	if((x=find(x))==(y=find(y)))
		return 0;
	r[x]=y;
	if(a[y]<=1e9)
		dfs2(x, w);
	if(a[x]<=1e9)
		dfs2(y, w);
	adj2[y].push_back(x);
	return 1;
}

void dfs(int u=0, int p=-1) {
	anc[u][0]=p;
	for(int i=1; i<17; ++i) {
		anc[u][i]=~anc[u][i-1]?anc[anc[u][i-1]][i-1]:-1;
		b1[u][i]=~anc[u][i-1]?max(b1[u][i-1], b1[anc[u][i-1]][i-1]):0;
	}
	for(auto v : adj[u]) {
		if(v[1]^p) {
			b1[v[1]][0]=v[0];
			d[v[1]]=d[u]+1;
			dfs(v[1], u);
		}
	}
}

int qry(int x, int y) {
	if(d[x]<d[y])
		swap(x, y);
	int r=0;
	for(int i=16; ~i; --i) {
		if(d[x]-(1<<i)>=d[y]) {
			r=max(b1[x][i], r);
			x=anc[x][i];
		}
	}
	if(x==y)
		return r;
	for(int i=16; ~i; --i) {
		if(anc[x][i]^anc[y][i]) {
			r=max({b1[x][i], b1[y][i], r});
			x=anc[x][i];
			y=anc[y][i];
		}
	}
	return max({b1[x][0], b1[y][0], r});
}

void init(int n, int m, vector<int> u, vector<int> v, vector<int> w) {
	::n=n;
	vector<ar<int, 3>> e;
	for(int i=0; i<m; ++i)
		e.push_back({w[i], u[i], v[i]});
	sort(e.begin(), e.end());
	memset(a, 0x3f, 4*n);
	iota(r, r+n, 0);
	for(auto f : e) {
		if(join(f[1], f[2], f[0])) {
			adj[f[1]].push_back({f[0], f[2]});
			adj[f[2]].push_back({f[0], f[1]});
			if(adj[f[1]].size()>2||adj[f[2]].size()>2)
				dfs2(find(f[1]), f[0]);
		} else
			dfs2(find(f[1]), f[0]);
	}
	dfs();
}

int getMinimumFuelCapacity(int x, int y) {
	int b=max(qry(x, y), a[x]);
	return b>1e9?-1:b;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 4 ms 4992 KB Output is correct
3 Correct 4 ms 4992 KB Output is correct
4 Correct 4 ms 5120 KB Output is correct
5 Correct 4 ms 5248 KB Output is correct
6 Correct 4 ms 5248 KB Output is correct
7 Correct 5 ms 5248 KB Output is correct
8 Correct 4 ms 5376 KB Output is correct
9 Correct 154 ms 26860 KB Output is correct
10 Correct 190 ms 32236 KB Output is correct
11 Correct 196 ms 31596 KB Output is correct
12 Correct 214 ms 33132 KB Output is correct
13 Correct 176 ms 34156 KB Output is correct
14 Correct 180 ms 26732 KB Output is correct
15 Correct 468 ms 33772 KB Output is correct
16 Correct 477 ms 32184 KB Output is correct
17 Correct 435 ms 35692 KB Output is correct
18 Correct 429 ms 34924 KB Output is correct
19 Correct 118 ms 14460 KB Output is correct
20 Correct 470 ms 33772 KB Output is correct
21 Correct 490 ms 32676 KB Output is correct
22 Correct 497 ms 34868 KB Output is correct
23 Correct 449 ms 35000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 4 ms 4992 KB Output is correct
3 Correct 260 ms 32452 KB Output is correct
4 Correct 259 ms 33468 KB Output is correct
5 Correct 270 ms 33048 KB Output is correct
6 Correct 257 ms 33344 KB Output is correct
7 Correct 270 ms 33388 KB Output is correct
8 Correct 256 ms 32336 KB Output is correct
9 Correct 265 ms 33012 KB Output is correct
10 Correct 254 ms 32100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 4 ms 4992 KB Output is correct
3 Correct 4 ms 4992 KB Output is correct
4 Correct 4 ms 4992 KB Output is correct
5 Correct 4 ms 5120 KB Output is correct
6 Correct 4 ms 5248 KB Output is correct
7 Correct 4 ms 5248 KB Output is correct
8 Correct 5 ms 5248 KB Output is correct
9 Correct 4 ms 5376 KB Output is correct
10 Correct 5 ms 5248 KB Output is correct
11 Correct 4 ms 5248 KB Output is correct
12 Correct 4 ms 5248 KB Output is correct
13 Correct 4 ms 5248 KB Output is correct
14 Correct 5 ms 5248 KB Output is correct
15 Correct 5 ms 5248 KB Output is correct
16 Correct 5 ms 5248 KB Output is correct
17 Correct 5 ms 5376 KB Output is correct
18 Correct 5 ms 5248 KB Output is correct
19 Correct 4 ms 5248 KB Output is correct
20 Correct 5 ms 5248 KB Output is correct
21 Correct 5 ms 5248 KB Output is correct
22 Correct 5 ms 5248 KB Output is correct
23 Correct 4 ms 5248 KB Output is correct
24 Correct 5 ms 5376 KB Output is correct
25 Correct 5 ms 5376 KB Output is correct
26 Correct 5 ms 5376 KB Output is correct
27 Correct 5 ms 5376 KB Output is correct
28 Correct 6 ms 5376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 4 ms 4992 KB Output is correct
3 Correct 4 ms 4992 KB Output is correct
4 Correct 4 ms 4992 KB Output is correct
5 Correct 4 ms 5120 KB Output is correct
6 Correct 4 ms 5248 KB Output is correct
7 Correct 4 ms 5248 KB Output is correct
8 Correct 5 ms 5248 KB Output is correct
9 Correct 4 ms 5376 KB Output is correct
10 Correct 154 ms 26860 KB Output is correct
11 Correct 190 ms 32236 KB Output is correct
12 Correct 196 ms 31596 KB Output is correct
13 Correct 214 ms 33132 KB Output is correct
14 Correct 176 ms 34156 KB Output is correct
15 Correct 5 ms 5248 KB Output is correct
16 Correct 4 ms 5248 KB Output is correct
17 Correct 4 ms 5248 KB Output is correct
18 Correct 4 ms 5248 KB Output is correct
19 Correct 5 ms 5248 KB Output is correct
20 Correct 5 ms 5248 KB Output is correct
21 Correct 5 ms 5248 KB Output is correct
22 Correct 5 ms 5376 KB Output is correct
23 Correct 5 ms 5248 KB Output is correct
24 Correct 19 ms 8512 KB Output is correct
25 Correct 213 ms 32876 KB Output is correct
26 Correct 207 ms 31340 KB Output is correct
27 Correct 201 ms 30188 KB Output is correct
28 Correct 197 ms 29548 KB Output is correct
29 Correct 187 ms 29264 KB Output is correct
30 Correct 171 ms 27500 KB Output is correct
31 Correct 213 ms 31724 KB Output is correct
32 Correct 209 ms 32432 KB Output is correct
33 Correct 168 ms 33132 KB Output is correct
34 Correct 189 ms 30060 KB Output is correct
35 Correct 4 ms 5248 KB Output is correct
36 Correct 5 ms 5248 KB Output is correct
37 Correct 5 ms 5248 KB Output is correct
38 Correct 5 ms 5248 KB Output is correct
39 Correct 4 ms 5248 KB Output is correct
40 Correct 5 ms 5376 KB Output is correct
41 Correct 5 ms 5376 KB Output is correct
42 Correct 5 ms 5376 KB Output is correct
43 Correct 5 ms 5376 KB Output is correct
44 Correct 6 ms 5376 KB Output is correct
45 Correct 181 ms 27496 KB Output is correct
46 Correct 208 ms 30672 KB Output is correct
47 Correct 191 ms 29548 KB Output is correct
48 Correct 185 ms 29676 KB Output is correct
49 Correct 97 ms 13804 KB Output is correct
50 Correct 79 ms 12524 KB Output is correct
51 Correct 147 ms 23976 KB Output is correct
52 Correct 240 ms 32868 KB Output is correct
53 Correct 260 ms 33492 KB Output is correct
54 Correct 272 ms 35300 KB Output is correct
55 Correct 181 ms 34028 KB Output is correct
56 Correct 233 ms 32488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 4 ms 4992 KB Output is correct
3 Correct 4 ms 4992 KB Output is correct
4 Correct 4 ms 5120 KB Output is correct
5 Correct 4 ms 5248 KB Output is correct
6 Correct 4 ms 5248 KB Output is correct
7 Correct 5 ms 5248 KB Output is correct
8 Correct 4 ms 5376 KB Output is correct
9 Correct 154 ms 26860 KB Output is correct
10 Correct 190 ms 32236 KB Output is correct
11 Correct 196 ms 31596 KB Output is correct
12 Correct 214 ms 33132 KB Output is correct
13 Correct 176 ms 34156 KB Output is correct
14 Correct 180 ms 26732 KB Output is correct
15 Correct 468 ms 33772 KB Output is correct
16 Correct 477 ms 32184 KB Output is correct
17 Correct 435 ms 35692 KB Output is correct
18 Correct 429 ms 34924 KB Output is correct
19 Correct 260 ms 32452 KB Output is correct
20 Correct 259 ms 33468 KB Output is correct
21 Correct 270 ms 33048 KB Output is correct
22 Correct 257 ms 33344 KB Output is correct
23 Correct 270 ms 33388 KB Output is correct
24 Correct 256 ms 32336 KB Output is correct
25 Correct 265 ms 33012 KB Output is correct
26 Correct 254 ms 32100 KB Output is correct
27 Correct 5 ms 5248 KB Output is correct
28 Correct 4 ms 5248 KB Output is correct
29 Correct 4 ms 5248 KB Output is correct
30 Correct 4 ms 5248 KB Output is correct
31 Correct 5 ms 5248 KB Output is correct
32 Correct 5 ms 5248 KB Output is correct
33 Correct 5 ms 5248 KB Output is correct
34 Correct 5 ms 5376 KB Output is correct
35 Correct 5 ms 5248 KB Output is correct
36 Correct 19 ms 8512 KB Output is correct
37 Correct 213 ms 32876 KB Output is correct
38 Correct 207 ms 31340 KB Output is correct
39 Correct 201 ms 30188 KB Output is correct
40 Correct 197 ms 29548 KB Output is correct
41 Correct 187 ms 29264 KB Output is correct
42 Correct 171 ms 27500 KB Output is correct
43 Correct 213 ms 31724 KB Output is correct
44 Correct 209 ms 32432 KB Output is correct
45 Correct 168 ms 33132 KB Output is correct
46 Correct 189 ms 30060 KB Output is correct
47 Correct 30 ms 8620 KB Output is correct
48 Correct 508 ms 33516 KB Output is correct
49 Correct 496 ms 32364 KB Output is correct
50 Correct 462 ms 31724 KB Output is correct
51 Correct 442 ms 31560 KB Output is correct
52 Correct 401 ms 29804 KB Output is correct
53 Correct 283 ms 25044 KB Output is correct
54 Correct 517 ms 33336 KB Output is correct
55 Correct 488 ms 33644 KB Output is correct
56 Correct 432 ms 35048 KB Output is correct
57 Correct 387 ms 32184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 4 ms 4992 KB Output is correct
3 Correct 4 ms 4992 KB Output is correct
4 Correct 4 ms 4992 KB Output is correct
5 Correct 4 ms 5120 KB Output is correct
6 Correct 4 ms 5248 KB Output is correct
7 Correct 4 ms 5248 KB Output is correct
8 Correct 5 ms 5248 KB Output is correct
9 Correct 4 ms 5376 KB Output is correct
10 Correct 154 ms 26860 KB Output is correct
11 Correct 190 ms 32236 KB Output is correct
12 Correct 196 ms 31596 KB Output is correct
13 Correct 214 ms 33132 KB Output is correct
14 Correct 176 ms 34156 KB Output is correct
15 Correct 180 ms 26732 KB Output is correct
16 Correct 468 ms 33772 KB Output is correct
17 Correct 477 ms 32184 KB Output is correct
18 Correct 435 ms 35692 KB Output is correct
19 Correct 429 ms 34924 KB Output is correct
20 Correct 260 ms 32452 KB Output is correct
21 Correct 259 ms 33468 KB Output is correct
22 Correct 270 ms 33048 KB Output is correct
23 Correct 257 ms 33344 KB Output is correct
24 Correct 270 ms 33388 KB Output is correct
25 Correct 256 ms 32336 KB Output is correct
26 Correct 265 ms 33012 KB Output is correct
27 Correct 254 ms 32100 KB Output is correct
28 Correct 5 ms 5248 KB Output is correct
29 Correct 4 ms 5248 KB Output is correct
30 Correct 4 ms 5248 KB Output is correct
31 Correct 4 ms 5248 KB Output is correct
32 Correct 5 ms 5248 KB Output is correct
33 Correct 5 ms 5248 KB Output is correct
34 Correct 5 ms 5248 KB Output is correct
35 Correct 5 ms 5376 KB Output is correct
36 Correct 5 ms 5248 KB Output is correct
37 Correct 19 ms 8512 KB Output is correct
38 Correct 213 ms 32876 KB Output is correct
39 Correct 207 ms 31340 KB Output is correct
40 Correct 201 ms 30188 KB Output is correct
41 Correct 197 ms 29548 KB Output is correct
42 Correct 187 ms 29264 KB Output is correct
43 Correct 171 ms 27500 KB Output is correct
44 Correct 213 ms 31724 KB Output is correct
45 Correct 209 ms 32432 KB Output is correct
46 Correct 168 ms 33132 KB Output is correct
47 Correct 189 ms 30060 KB Output is correct
48 Correct 30 ms 8620 KB Output is correct
49 Correct 508 ms 33516 KB Output is correct
50 Correct 496 ms 32364 KB Output is correct
51 Correct 462 ms 31724 KB Output is correct
52 Correct 442 ms 31560 KB Output is correct
53 Correct 401 ms 29804 KB Output is correct
54 Correct 283 ms 25044 KB Output is correct
55 Correct 517 ms 33336 KB Output is correct
56 Correct 488 ms 33644 KB Output is correct
57 Correct 432 ms 35048 KB Output is correct
58 Correct 387 ms 32184 KB Output is correct
59 Correct 118 ms 14460 KB Output is correct
60 Correct 470 ms 33772 KB Output is correct
61 Correct 490 ms 32676 KB Output is correct
62 Correct 497 ms 34868 KB Output is correct
63 Correct 449 ms 35000 KB Output is correct
64 Correct 4 ms 5248 KB Output is correct
65 Correct 5 ms 5248 KB Output is correct
66 Correct 5 ms 5248 KB Output is correct
67 Correct 5 ms 5248 KB Output is correct
68 Correct 4 ms 5248 KB Output is correct
69 Correct 5 ms 5376 KB Output is correct
70 Correct 5 ms 5376 KB Output is correct
71 Correct 5 ms 5376 KB Output is correct
72 Correct 5 ms 5376 KB Output is correct
73 Correct 6 ms 5376 KB Output is correct
74 Correct 181 ms 27496 KB Output is correct
75 Correct 208 ms 30672 KB Output is correct
76 Correct 191 ms 29548 KB Output is correct
77 Correct 185 ms 29676 KB Output is correct
78 Correct 97 ms 13804 KB Output is correct
79 Correct 79 ms 12524 KB Output is correct
80 Correct 147 ms 23976 KB Output is correct
81 Correct 240 ms 32868 KB Output is correct
82 Correct 260 ms 33492 KB Output is correct
83 Correct 272 ms 35300 KB Output is correct
84 Correct 181 ms 34028 KB Output is correct
85 Correct 233 ms 32488 KB Output is correct
86 Correct 91 ms 14964 KB Output is correct
87 Correct 485 ms 32236 KB Output is correct
88 Correct 489 ms 32236 KB Output is correct
89 Correct 335 ms 29932 KB Output is correct
90 Correct 203 ms 15520 KB Output is correct
91 Correct 218 ms 15848 KB Output is correct
92 Correct 306 ms 25360 KB Output is correct
93 Correct 507 ms 34536 KB Output is correct
94 Correct 422 ms 34660 KB Output is correct
95 Correct 559 ms 36712 KB Output is correct
96 Correct 458 ms 33800 KB Output is correct
97 Correct 404 ms 33000 KB Output is correct