답안 #297102

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
297102 2020-09-11T08:49:04 Z tmwilliamlin168 자매 도시 (APIO20_swap) C++14
100 / 100
695 ms 54628 KB
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;

#define ar array

const int mxN=1e5;
int n, a[mxN], r[mxN];

struct tree {
	int d[mxN], anc[mxN][17], anc2[mxN][17], b1[mxN][17], b2[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;
		// return 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();
		// */
		// a[u]=min(w, a[u]);
		// for(auto v : adj[u])
		// 	if(v[1]^p)
		// 		dfs2(v[1], w, u);
	}
	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;
	}
	vector<ar<int, 3>> co(vector<ar<int, 3>> e) {
		iota(r, r+n, 0);
		vector<ar<int, 3>> g;
		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
				// g.push_back(f);
				dfs2(find(f[1]), f[0]);
		}
		memset(b2, 0x3f, sizeof(b2));
		return g;
	}
	int gv(int u, int x, int y) {
		int r=a[u];
		for(auto e : adj[u]) {
			if(e[1]^x&&e[1]^y) {
				r=min(e[0], r);
				break;
			}
		}
		return r;
	}
	void dfs(int u=0, int p=-1) {
		anc[u][0]=p;
		anc2[u][0]=u;
		for(int i=1; i<17; ++i) {
			anc[u][i]=~anc[u][i-1]?anc[anc[u][i-1]][i-1]:-1;
			anc2[u][i]=~anc[u][i-1]?anc2[anc[u][i-1]][i-1]:0;
			b1[u][i]=~anc[u][i-1]?max(b1[u][i-1], b1[anc[u][i-1]][i-1]):0;
		}
		if(u) {
			b2[u][1]=gv(p, u, anc[p][0]);
			for(int i=2; i<17; ++i)
				b2[u][2]=~anc[u][i-1]?min({b2[u][i-1], b2[anc[u][i-1]][i-1], gv(anc[u][i-1], anc2[u][i-1], anc[anc[u][i-1]][0])}):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});
	}
	int qry2(int x, int y) {
		if(d[x]<d[y])
			swap(x, y);
		int r=2e9, x2=x, y2=y;
		for(int i=16; ~i; --i) {
			if(d[x]-(1<<i)>=d[y]) {
				if(x2^x)
					r=min(gv(x, x2, anc[x][0]), r);
				r=min(b2[x][i], r);
				x2=anc2[x][i];
				x=anc[x][i];
			}
		}
		if(x==y)
			return r;
		for(int i=16; ~i; --i) {
			if(anc[x][i]^anc[y][i]) {
				if(x2^x)
					r=min(gv(x, x2, anc[x][0]), r);
				if(y2^y)
					r=min(gv(y, y2, anc[y][0]), r);
				r=min({b2[x][i], b2[y][i], r});
				x2=anc2[x][i];
				y2=anc2[y][i];
				x=anc[x][i];
				y=anc[y][i];
			}
		}
		if(x2^x)
			r=min(gv(x, x2, anc[x][0]), r);
		if(y2^y)
			r=min(gv(y, y2, anc[y][0]), r);
		return min(gv(anc[x][0], x, y), r);
	}
} t0;//, t1;

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);
	e=t0.co(e);
	/*t1.co(e);
	for(auto f : e) {
		a[f[1]]=max(f[0], a[f[1]]);
		a[f[2]]=max(f[0], a[f[2]]);
	}*/
	t0.dfs();
	//t1.dfs();
}

int getMinimumFuelCapacity(int x, int y) {
	// cout << t0.qry(x, y) << " " << t0.qry2(x, y) << " " << t1.qry(x, y) << endl;
	// int a=min(max(t0.qry(x, y), t0.qry2(x, y)), t1.qry(x, y));
	int b=max(t0.qry(x, y), a[x]);
	return b>1e9?-1:b;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 11648 KB Output is correct
2 Correct 8 ms 11808 KB Output is correct
3 Correct 8 ms 11648 KB Output is correct
4 Correct 8 ms 11904 KB Output is correct
5 Correct 9 ms 12032 KB Output is correct
6 Correct 9 ms 12032 KB Output is correct
7 Correct 9 ms 12032 KB Output is correct
8 Correct 9 ms 12032 KB Output is correct
9 Correct 251 ms 38420 KB Output is correct
10 Correct 330 ms 45972 KB Output is correct
11 Correct 315 ms 44708 KB Output is correct
12 Correct 354 ms 47040 KB Output is correct
13 Correct 309 ms 49084 KB Output is correct
14 Correct 260 ms 37688 KB Output is correct
15 Correct 636 ms 47632 KB Output is correct
16 Correct 596 ms 44788 KB Output is correct
17 Correct 582 ms 50856 KB Output is correct
18 Correct 545 ms 49160 KB Output is correct
19 Correct 148 ms 21228 KB Output is correct
20 Correct 603 ms 47988 KB Output is correct
21 Correct 590 ms 45960 KB Output is correct
22 Correct 605 ms 49620 KB Output is correct
23 Correct 575 ms 50084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 11648 KB Output is correct
2 Correct 8 ms 11808 KB Output is correct
3 Correct 280 ms 43612 KB Output is correct
4 Correct 284 ms 45352 KB Output is correct
5 Correct 281 ms 44592 KB Output is correct
6 Correct 444 ms 45232 KB Output is correct
7 Correct 291 ms 45040 KB Output is correct
8 Correct 283 ms 43780 KB Output is correct
9 Correct 289 ms 44792 KB Output is correct
10 Correct 284 ms 43560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 11680 KB Output is correct
2 Correct 8 ms 11648 KB Output is correct
3 Correct 8 ms 11808 KB Output is correct
4 Correct 8 ms 11648 KB Output is correct
5 Correct 8 ms 11904 KB Output is correct
6 Correct 9 ms 12032 KB Output is correct
7 Correct 9 ms 12032 KB Output is correct
8 Correct 9 ms 12032 KB Output is correct
9 Correct 9 ms 12032 KB Output is correct
10 Correct 9 ms 12032 KB Output is correct
11 Correct 10 ms 12032 KB Output is correct
12 Correct 10 ms 12064 KB Output is correct
13 Correct 9 ms 12032 KB Output is correct
14 Correct 9 ms 12032 KB Output is correct
15 Correct 9 ms 12032 KB Output is correct
16 Correct 10 ms 12192 KB Output is correct
17 Correct 10 ms 12160 KB Output is correct
18 Correct 9 ms 12032 KB Output is correct
19 Correct 9 ms 11904 KB Output is correct
20 Correct 13 ms 12032 KB Output is correct
21 Correct 9 ms 12032 KB Output is correct
22 Correct 9 ms 11904 KB Output is correct
23 Correct 9 ms 11952 KB Output is correct
24 Correct 10 ms 12160 KB Output is correct
25 Correct 10 ms 12064 KB Output is correct
26 Correct 10 ms 12160 KB Output is correct
27 Correct 9 ms 12032 KB Output is correct
28 Correct 10 ms 12032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 11680 KB Output is correct
2 Correct 8 ms 11648 KB Output is correct
3 Correct 8 ms 11808 KB Output is correct
4 Correct 8 ms 11648 KB Output is correct
5 Correct 8 ms 11904 KB Output is correct
6 Correct 9 ms 12032 KB Output is correct
7 Correct 9 ms 12032 KB Output is correct
8 Correct 9 ms 12032 KB Output is correct
9 Correct 9 ms 12032 KB Output is correct
10 Correct 251 ms 38420 KB Output is correct
11 Correct 330 ms 45972 KB Output is correct
12 Correct 315 ms 44708 KB Output is correct
13 Correct 354 ms 47040 KB Output is correct
14 Correct 309 ms 49084 KB Output is correct
15 Correct 9 ms 12032 KB Output is correct
16 Correct 10 ms 12032 KB Output is correct
17 Correct 10 ms 12064 KB Output is correct
18 Correct 9 ms 12032 KB Output is correct
19 Correct 9 ms 12032 KB Output is correct
20 Correct 9 ms 12032 KB Output is correct
21 Correct 10 ms 12192 KB Output is correct
22 Correct 10 ms 12160 KB Output is correct
23 Correct 9 ms 12032 KB Output is correct
24 Correct 31 ms 15952 KB Output is correct
25 Correct 345 ms 48244 KB Output is correct
26 Correct 308 ms 45120 KB Output is correct
27 Correct 293 ms 42812 KB Output is correct
28 Correct 344 ms 41956 KB Output is correct
29 Correct 228 ms 41324 KB Output is correct
30 Correct 224 ms 38996 KB Output is correct
31 Correct 349 ms 46272 KB Output is correct
32 Correct 343 ms 47424 KB Output is correct
33 Correct 296 ms 48836 KB Output is correct
34 Correct 235 ms 42560 KB Output is correct
35 Correct 9 ms 11904 KB Output is correct
36 Correct 13 ms 12032 KB Output is correct
37 Correct 9 ms 12032 KB Output is correct
38 Correct 9 ms 11904 KB Output is correct
39 Correct 9 ms 11952 KB Output is correct
40 Correct 10 ms 12160 KB Output is correct
41 Correct 10 ms 12064 KB Output is correct
42 Correct 10 ms 12160 KB Output is correct
43 Correct 9 ms 12032 KB Output is correct
44 Correct 10 ms 12032 KB Output is correct
45 Correct 216 ms 39016 KB Output is correct
46 Correct 321 ms 43868 KB Output is correct
47 Correct 321 ms 41780 KB Output is correct
48 Correct 214 ms 41924 KB Output is correct
49 Correct 101 ms 21988 KB Output is correct
50 Correct 121 ms 20076 KB Output is correct
51 Correct 179 ms 34252 KB Output is correct
52 Correct 365 ms 46844 KB Output is correct
53 Correct 326 ms 46748 KB Output is correct
54 Correct 399 ms 50512 KB Output is correct
55 Correct 300 ms 50488 KB Output is correct
56 Correct 291 ms 45720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 11648 KB Output is correct
2 Correct 8 ms 11808 KB Output is correct
3 Correct 8 ms 11648 KB Output is correct
4 Correct 8 ms 11904 KB Output is correct
5 Correct 9 ms 12032 KB Output is correct
6 Correct 9 ms 12032 KB Output is correct
7 Correct 9 ms 12032 KB Output is correct
8 Correct 9 ms 12032 KB Output is correct
9 Correct 251 ms 38420 KB Output is correct
10 Correct 330 ms 45972 KB Output is correct
11 Correct 315 ms 44708 KB Output is correct
12 Correct 354 ms 47040 KB Output is correct
13 Correct 309 ms 49084 KB Output is correct
14 Correct 260 ms 37688 KB Output is correct
15 Correct 636 ms 47632 KB Output is correct
16 Correct 596 ms 44788 KB Output is correct
17 Correct 582 ms 50856 KB Output is correct
18 Correct 545 ms 49160 KB Output is correct
19 Correct 280 ms 43612 KB Output is correct
20 Correct 284 ms 45352 KB Output is correct
21 Correct 281 ms 44592 KB Output is correct
22 Correct 444 ms 45232 KB Output is correct
23 Correct 291 ms 45040 KB Output is correct
24 Correct 283 ms 43780 KB Output is correct
25 Correct 289 ms 44792 KB Output is correct
26 Correct 284 ms 43560 KB Output is correct
27 Correct 9 ms 12032 KB Output is correct
28 Correct 10 ms 12032 KB Output is correct
29 Correct 10 ms 12064 KB Output is correct
30 Correct 9 ms 12032 KB Output is correct
31 Correct 9 ms 12032 KB Output is correct
32 Correct 9 ms 12032 KB Output is correct
33 Correct 10 ms 12192 KB Output is correct
34 Correct 10 ms 12160 KB Output is correct
35 Correct 9 ms 12032 KB Output is correct
36 Correct 31 ms 15952 KB Output is correct
37 Correct 345 ms 48244 KB Output is correct
38 Correct 308 ms 45120 KB Output is correct
39 Correct 293 ms 42812 KB Output is correct
40 Correct 344 ms 41956 KB Output is correct
41 Correct 228 ms 41324 KB Output is correct
42 Correct 224 ms 38996 KB Output is correct
43 Correct 349 ms 46272 KB Output is correct
44 Correct 343 ms 47424 KB Output is correct
45 Correct 296 ms 48836 KB Output is correct
46 Correct 235 ms 42560 KB Output is correct
47 Correct 42 ms 16016 KB Output is correct
48 Correct 652 ms 50976 KB Output is correct
49 Correct 636 ms 49188 KB Output is correct
50 Correct 530 ms 47852 KB Output is correct
51 Correct 562 ms 47068 KB Output is correct
52 Correct 478 ms 44992 KB Output is correct
53 Correct 361 ms 37992 KB Output is correct
54 Correct 624 ms 50928 KB Output is correct
55 Correct 600 ms 51496 KB Output is correct
56 Correct 536 ms 54628 KB Output is correct
57 Correct 469 ms 47968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 11680 KB Output is correct
2 Correct 8 ms 11648 KB Output is correct
3 Correct 8 ms 11808 KB Output is correct
4 Correct 8 ms 11648 KB Output is correct
5 Correct 8 ms 11904 KB Output is correct
6 Correct 9 ms 12032 KB Output is correct
7 Correct 9 ms 12032 KB Output is correct
8 Correct 9 ms 12032 KB Output is correct
9 Correct 9 ms 12032 KB Output is correct
10 Correct 251 ms 38420 KB Output is correct
11 Correct 330 ms 45972 KB Output is correct
12 Correct 315 ms 44708 KB Output is correct
13 Correct 354 ms 47040 KB Output is correct
14 Correct 309 ms 49084 KB Output is correct
15 Correct 260 ms 37688 KB Output is correct
16 Correct 636 ms 47632 KB Output is correct
17 Correct 596 ms 44788 KB Output is correct
18 Correct 582 ms 50856 KB Output is correct
19 Correct 545 ms 49160 KB Output is correct
20 Correct 280 ms 43612 KB Output is correct
21 Correct 284 ms 45352 KB Output is correct
22 Correct 281 ms 44592 KB Output is correct
23 Correct 444 ms 45232 KB Output is correct
24 Correct 291 ms 45040 KB Output is correct
25 Correct 283 ms 43780 KB Output is correct
26 Correct 289 ms 44792 KB Output is correct
27 Correct 284 ms 43560 KB Output is correct
28 Correct 9 ms 12032 KB Output is correct
29 Correct 10 ms 12032 KB Output is correct
30 Correct 10 ms 12064 KB Output is correct
31 Correct 9 ms 12032 KB Output is correct
32 Correct 9 ms 12032 KB Output is correct
33 Correct 9 ms 12032 KB Output is correct
34 Correct 10 ms 12192 KB Output is correct
35 Correct 10 ms 12160 KB Output is correct
36 Correct 9 ms 12032 KB Output is correct
37 Correct 31 ms 15952 KB Output is correct
38 Correct 345 ms 48244 KB Output is correct
39 Correct 308 ms 45120 KB Output is correct
40 Correct 293 ms 42812 KB Output is correct
41 Correct 344 ms 41956 KB Output is correct
42 Correct 228 ms 41324 KB Output is correct
43 Correct 224 ms 38996 KB Output is correct
44 Correct 349 ms 46272 KB Output is correct
45 Correct 343 ms 47424 KB Output is correct
46 Correct 296 ms 48836 KB Output is correct
47 Correct 235 ms 42560 KB Output is correct
48 Correct 42 ms 16016 KB Output is correct
49 Correct 652 ms 50976 KB Output is correct
50 Correct 636 ms 49188 KB Output is correct
51 Correct 530 ms 47852 KB Output is correct
52 Correct 562 ms 47068 KB Output is correct
53 Correct 478 ms 44992 KB Output is correct
54 Correct 361 ms 37992 KB Output is correct
55 Correct 624 ms 50928 KB Output is correct
56 Correct 600 ms 51496 KB Output is correct
57 Correct 536 ms 54628 KB Output is correct
58 Correct 469 ms 47968 KB Output is correct
59 Correct 148 ms 21228 KB Output is correct
60 Correct 603 ms 47988 KB Output is correct
61 Correct 590 ms 45960 KB Output is correct
62 Correct 605 ms 49620 KB Output is correct
63 Correct 575 ms 50084 KB Output is correct
64 Correct 9 ms 11904 KB Output is correct
65 Correct 13 ms 12032 KB Output is correct
66 Correct 9 ms 12032 KB Output is correct
67 Correct 9 ms 11904 KB Output is correct
68 Correct 9 ms 11952 KB Output is correct
69 Correct 10 ms 12160 KB Output is correct
70 Correct 10 ms 12064 KB Output is correct
71 Correct 10 ms 12160 KB Output is correct
72 Correct 9 ms 12032 KB Output is correct
73 Correct 10 ms 12032 KB Output is correct
74 Correct 216 ms 39016 KB Output is correct
75 Correct 321 ms 43868 KB Output is correct
76 Correct 321 ms 41780 KB Output is correct
77 Correct 214 ms 41924 KB Output is correct
78 Correct 101 ms 21988 KB Output is correct
79 Correct 121 ms 20076 KB Output is correct
80 Correct 179 ms 34252 KB Output is correct
81 Correct 365 ms 46844 KB Output is correct
82 Correct 326 ms 46748 KB Output is correct
83 Correct 399 ms 50512 KB Output is correct
84 Correct 300 ms 50488 KB Output is correct
85 Correct 291 ms 45720 KB Output is correct
86 Correct 104 ms 23176 KB Output is correct
87 Correct 568 ms 48588 KB Output is correct
88 Correct 573 ms 48828 KB Output is correct
89 Correct 381 ms 44748 KB Output is correct
90 Correct 208 ms 25956 KB Output is correct
91 Correct 265 ms 25832 KB Output is correct
92 Correct 341 ms 39308 KB Output is correct
93 Correct 637 ms 52168 KB Output is correct
94 Correct 466 ms 51064 KB Output is correct
95 Correct 695 ms 54180 KB Output is correct
96 Correct 523 ms 52124 KB Output is correct
97 Correct 447 ms 49208 KB Output is correct