Submission #545159

# Submission time Handle Problem Language Result Execution time Memory
545159 2022-04-03T17:21:07 Z MilosMilutinovic Swapping Cities (APIO20_swap) C++14
100 / 100
373 ms 61664 KB
#include "swap.h"
#include <bits/stdc++.h>

#define fi first
#define se second
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

template<typename T> bool chkmin(T&x,T y){return x>y?x=y,1:0;}
template<typename T> bool chkmax(T&x,T y){return x<y?x=y,1:0;}

int n,m,fa[300005],f[300005][20],val[300005],dep[300005],tl[300005],tr[300005],ok[300005],up[300005];
array<int,3> edg[200005];
vector<int> adj[300005];

int getf(int x){return x==fa[x]?x:fa[x]=getf(fa[x]);}

void dfs(int u,int p){
	up[u]=(ok[u]?u:up[p]);
	dep[u]=dep[p]+1;
	f[u][0]=p;
	for(int i=1;i<20;i++) f[u][i]=f[f[u][i-1]][i-1];
	for(int v:adj[u]) dfs(v,u);
}

int lca(int x,int y){
	if(dep[x]<dep[y]) swap(x,y);
	for(int i=19;i>=0;i--) if(dep[f[x][i]]>=dep[y]) x=f[x][i];
	if(x==y) return x;
	for(int i=19;i>=0;i--) if(f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i];
	return f[x][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++) edg[i+1]={W[i],U[i]+1,V[i]+1};
	sort(edg+1,edg+m+1);
	for(int i=1;i<=n;i++) tl[i]=tr[i]=i;
	for(int i=1;i<=n+m;i++) fa[i]=i;
	for(int i=1;i<=m;i++){
		int u=edg[i][1],v=edg[i][2],w=edg[i][0];
		val[n+i]=w;
		u=getf(u); v=getf(v); fa[u]=fa[v]=n+i;
		ok[n+i]=(ok[u]|ok[v]|(u==v?1:0));
		bool found=false;
		for(int x:{tl[u],tr[u]}){
			for(int y:{tl[v],tr[v]}){
				if((x==edg[i][1]&&y==edg[i][2])||(x==edg[i][2]&&y==edg[i][1])){
					found=true; tl[n+i]=(x^tl[u]^tr[u]); tr[n+i]=(y^tl[v]^tr[v]);
				}
			}
		}
		if(!found) ok[n+i]=1;
		adj[n+i].pb(u); if(u!=v) adj[n+i].pb(v);
	}
	dfs(n+m,0);
}

int getMinimumFuelCapacity(int X,int Y){
	if(!ok[n+m]) return -1;
	X++; Y++;
	assert(lca(X,Y)>=1&&lca(X,Y)<=n+m);
	return val[up[lca(X,Y)]];
}

/*
5 6
0 1 4
0 2 4
1 2 1
1 3 2
1 4 10
2 3 3
3
1 2
2 4
0 1

3 2
0 1 5
0 2 5
1
1 2
*/
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7400 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 5 ms 7628 KB Output is correct
6 Correct 4 ms 7508 KB Output is correct
7 Correct 4 ms 7636 KB Output is correct
8 Correct 4 ms 7636 KB Output is correct
9 Correct 95 ms 28584 KB Output is correct
10 Correct 118 ms 33248 KB Output is correct
11 Correct 117 ms 32836 KB Output is correct
12 Correct 124 ms 34388 KB Output is correct
13 Correct 104 ms 35956 KB Output is correct
14 Correct 100 ms 28620 KB Output is correct
15 Correct 180 ms 35260 KB Output is correct
16 Correct 166 ms 34516 KB Output is correct
17 Correct 171 ms 36052 KB Output is correct
18 Correct 171 ms 37720 KB Output is correct
19 Correct 94 ms 15064 KB Output is correct
20 Correct 250 ms 40776 KB Output is correct
21 Correct 249 ms 40068 KB Output is correct
22 Correct 271 ms 41904 KB Output is correct
23 Correct 337 ms 43484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7400 KB Output is correct
3 Correct 299 ms 38148 KB Output is correct
4 Correct 297 ms 39476 KB Output is correct
5 Correct 307 ms 38740 KB Output is correct
6 Correct 291 ms 39372 KB Output is correct
7 Correct 312 ms 39160 KB Output is correct
8 Correct 294 ms 37920 KB Output is correct
9 Correct 308 ms 38948 KB Output is correct
10 Correct 305 ms 37680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7400 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 5 ms 7628 KB Output is correct
6 Correct 4 ms 7508 KB Output is correct
7 Correct 4 ms 7636 KB Output is correct
8 Correct 4 ms 7636 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 4 ms 7624 KB Output is correct
11 Correct 5 ms 7628 KB Output is correct
12 Correct 5 ms 7636 KB Output is correct
13 Correct 5 ms 7508 KB Output is correct
14 Correct 5 ms 7636 KB Output is correct
15 Correct 5 ms 7648 KB Output is correct
16 Correct 5 ms 7628 KB Output is correct
17 Correct 5 ms 7636 KB Output is correct
18 Correct 5 ms 7636 KB Output is correct
19 Correct 5 ms 7636 KB Output is correct
20 Correct 5 ms 7636 KB Output is correct
21 Correct 7 ms 7640 KB Output is correct
22 Correct 5 ms 7764 KB Output is correct
23 Correct 5 ms 7584 KB Output is correct
24 Correct 5 ms 7756 KB Output is correct
25 Correct 5 ms 7764 KB Output is correct
26 Correct 6 ms 7764 KB Output is correct
27 Correct 5 ms 7636 KB Output is correct
28 Correct 7 ms 7764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7400 KB Output is correct
4 Correct 3 ms 7380 KB Output is correct
5 Correct 4 ms 7380 KB Output is correct
6 Correct 5 ms 7628 KB Output is correct
7 Correct 4 ms 7508 KB Output is correct
8 Correct 4 ms 7636 KB Output is correct
9 Correct 4 ms 7636 KB Output is correct
10 Correct 95 ms 28584 KB Output is correct
11 Correct 118 ms 33248 KB Output is correct
12 Correct 117 ms 32836 KB Output is correct
13 Correct 124 ms 34388 KB Output is correct
14 Correct 104 ms 35956 KB Output is correct
15 Correct 4 ms 7624 KB Output is correct
16 Correct 5 ms 7628 KB Output is correct
17 Correct 5 ms 7636 KB Output is correct
18 Correct 5 ms 7508 KB Output is correct
19 Correct 5 ms 7636 KB Output is correct
20 Correct 5 ms 7648 KB Output is correct
21 Correct 5 ms 7628 KB Output is correct
22 Correct 5 ms 7636 KB Output is correct
23 Correct 5 ms 7636 KB Output is correct
24 Correct 5 ms 7636 KB Output is correct
25 Correct 5 ms 7636 KB Output is correct
26 Correct 7 ms 7640 KB Output is correct
27 Correct 5 ms 7764 KB Output is correct
28 Correct 5 ms 7584 KB Output is correct
29 Correct 5 ms 7756 KB Output is correct
30 Correct 5 ms 7764 KB Output is correct
31 Correct 6 ms 7764 KB Output is correct
32 Correct 5 ms 7636 KB Output is correct
33 Correct 7 ms 7764 KB Output is correct
34 Correct 16 ms 11212 KB Output is correct
35 Correct 125 ms 36172 KB Output is correct
36 Correct 123 ms 36160 KB Output is correct
37 Correct 128 ms 36124 KB Output is correct
38 Correct 123 ms 35680 KB Output is correct
39 Correct 121 ms 35656 KB Output is correct
40 Correct 109 ms 33228 KB Output is correct
41 Correct 130 ms 36600 KB Output is correct
42 Correct 122 ms 36164 KB Output is correct
43 Correct 99 ms 38220 KB Output is correct
44 Correct 122 ms 36536 KB Output is correct
45 Correct 153 ms 46240 KB Output is correct
46 Correct 122 ms 36172 KB Output is correct
47 Correct 126 ms 36260 KB Output is correct
48 Correct 130 ms 39240 KB Output is correct
49 Correct 102 ms 41072 KB Output is correct
50 Correct 84 ms 33868 KB Output is correct
51 Correct 124 ms 40860 KB Output is correct
52 Correct 172 ms 49816 KB Output is correct
53 Correct 194 ms 53348 KB Output is correct
54 Correct 199 ms 57816 KB Output is correct
55 Correct 103 ms 38108 KB Output is correct
56 Correct 181 ms 53468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7400 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 5 ms 7628 KB Output is correct
6 Correct 4 ms 7508 KB Output is correct
7 Correct 4 ms 7636 KB Output is correct
8 Correct 4 ms 7636 KB Output is correct
9 Correct 95 ms 28584 KB Output is correct
10 Correct 118 ms 33248 KB Output is correct
11 Correct 117 ms 32836 KB Output is correct
12 Correct 124 ms 34388 KB Output is correct
13 Correct 104 ms 35956 KB Output is correct
14 Correct 100 ms 28620 KB Output is correct
15 Correct 180 ms 35260 KB Output is correct
16 Correct 166 ms 34516 KB Output is correct
17 Correct 171 ms 36052 KB Output is correct
18 Correct 171 ms 37720 KB Output is correct
19 Correct 299 ms 38148 KB Output is correct
20 Correct 297 ms 39476 KB Output is correct
21 Correct 307 ms 38740 KB Output is correct
22 Correct 291 ms 39372 KB Output is correct
23 Correct 312 ms 39160 KB Output is correct
24 Correct 294 ms 37920 KB Output is correct
25 Correct 308 ms 38948 KB Output is correct
26 Correct 305 ms 37680 KB Output is correct
27 Correct 4 ms 7624 KB Output is correct
28 Correct 5 ms 7628 KB Output is correct
29 Correct 5 ms 7636 KB Output is correct
30 Correct 5 ms 7508 KB Output is correct
31 Correct 5 ms 7636 KB Output is correct
32 Correct 5 ms 7648 KB Output is correct
33 Correct 5 ms 7628 KB Output is correct
34 Correct 5 ms 7636 KB Output is correct
35 Correct 5 ms 7636 KB Output is correct
36 Correct 16 ms 11212 KB Output is correct
37 Correct 125 ms 36172 KB Output is correct
38 Correct 123 ms 36160 KB Output is correct
39 Correct 128 ms 36124 KB Output is correct
40 Correct 123 ms 35680 KB Output is correct
41 Correct 121 ms 35656 KB Output is correct
42 Correct 109 ms 33228 KB Output is correct
43 Correct 130 ms 36600 KB Output is correct
44 Correct 122 ms 36164 KB Output is correct
45 Correct 99 ms 38220 KB Output is correct
46 Correct 122 ms 36536 KB Output is correct
47 Correct 26 ms 11384 KB Output is correct
48 Correct 262 ms 41036 KB Output is correct
49 Correct 259 ms 41108 KB Output is correct
50 Correct 249 ms 40956 KB Output is correct
51 Correct 247 ms 40876 KB Output is correct
52 Correct 242 ms 39256 KB Output is correct
53 Correct 201 ms 32428 KB Output is correct
54 Correct 269 ms 41896 KB Output is correct
55 Correct 264 ms 41008 KB Output is correct
56 Correct 341 ms 42612 KB Output is correct
57 Correct 247 ms 41944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7400 KB Output is correct
4 Correct 3 ms 7380 KB Output is correct
5 Correct 4 ms 7380 KB Output is correct
6 Correct 5 ms 7628 KB Output is correct
7 Correct 4 ms 7508 KB Output is correct
8 Correct 4 ms 7636 KB Output is correct
9 Correct 4 ms 7636 KB Output is correct
10 Correct 95 ms 28584 KB Output is correct
11 Correct 118 ms 33248 KB Output is correct
12 Correct 117 ms 32836 KB Output is correct
13 Correct 124 ms 34388 KB Output is correct
14 Correct 104 ms 35956 KB Output is correct
15 Correct 100 ms 28620 KB Output is correct
16 Correct 180 ms 35260 KB Output is correct
17 Correct 166 ms 34516 KB Output is correct
18 Correct 171 ms 36052 KB Output is correct
19 Correct 171 ms 37720 KB Output is correct
20 Correct 299 ms 38148 KB Output is correct
21 Correct 297 ms 39476 KB Output is correct
22 Correct 307 ms 38740 KB Output is correct
23 Correct 291 ms 39372 KB Output is correct
24 Correct 312 ms 39160 KB Output is correct
25 Correct 294 ms 37920 KB Output is correct
26 Correct 308 ms 38948 KB Output is correct
27 Correct 305 ms 37680 KB Output is correct
28 Correct 4 ms 7624 KB Output is correct
29 Correct 5 ms 7628 KB Output is correct
30 Correct 5 ms 7636 KB Output is correct
31 Correct 5 ms 7508 KB Output is correct
32 Correct 5 ms 7636 KB Output is correct
33 Correct 5 ms 7648 KB Output is correct
34 Correct 5 ms 7628 KB Output is correct
35 Correct 5 ms 7636 KB Output is correct
36 Correct 5 ms 7636 KB Output is correct
37 Correct 16 ms 11212 KB Output is correct
38 Correct 125 ms 36172 KB Output is correct
39 Correct 123 ms 36160 KB Output is correct
40 Correct 128 ms 36124 KB Output is correct
41 Correct 123 ms 35680 KB Output is correct
42 Correct 121 ms 35656 KB Output is correct
43 Correct 109 ms 33228 KB Output is correct
44 Correct 130 ms 36600 KB Output is correct
45 Correct 122 ms 36164 KB Output is correct
46 Correct 99 ms 38220 KB Output is correct
47 Correct 122 ms 36536 KB Output is correct
48 Correct 26 ms 11384 KB Output is correct
49 Correct 262 ms 41036 KB Output is correct
50 Correct 259 ms 41108 KB Output is correct
51 Correct 249 ms 40956 KB Output is correct
52 Correct 247 ms 40876 KB Output is correct
53 Correct 242 ms 39256 KB Output is correct
54 Correct 201 ms 32428 KB Output is correct
55 Correct 269 ms 41896 KB Output is correct
56 Correct 264 ms 41008 KB Output is correct
57 Correct 341 ms 42612 KB Output is correct
58 Correct 247 ms 41944 KB Output is correct
59 Correct 94 ms 15064 KB Output is correct
60 Correct 250 ms 40776 KB Output is correct
61 Correct 249 ms 40068 KB Output is correct
62 Correct 271 ms 41904 KB Output is correct
63 Correct 337 ms 43484 KB Output is correct
64 Correct 5 ms 7636 KB Output is correct
65 Correct 5 ms 7636 KB Output is correct
66 Correct 7 ms 7640 KB Output is correct
67 Correct 5 ms 7764 KB Output is correct
68 Correct 5 ms 7584 KB Output is correct
69 Correct 5 ms 7756 KB Output is correct
70 Correct 5 ms 7764 KB Output is correct
71 Correct 6 ms 7764 KB Output is correct
72 Correct 5 ms 7636 KB Output is correct
73 Correct 7 ms 7764 KB Output is correct
74 Correct 153 ms 46240 KB Output is correct
75 Correct 122 ms 36172 KB Output is correct
76 Correct 126 ms 36260 KB Output is correct
77 Correct 130 ms 39240 KB Output is correct
78 Correct 102 ms 41072 KB Output is correct
79 Correct 84 ms 33868 KB Output is correct
80 Correct 124 ms 40860 KB Output is correct
81 Correct 172 ms 49816 KB Output is correct
82 Correct 194 ms 53348 KB Output is correct
83 Correct 199 ms 57816 KB Output is correct
84 Correct 103 ms 38108 KB Output is correct
85 Correct 181 ms 53468 KB Output is correct
86 Correct 85 ms 21924 KB Output is correct
87 Correct 256 ms 41060 KB Output is correct
88 Correct 246 ms 41004 KB Output is correct
89 Correct 305 ms 42544 KB Output is correct
90 Correct 208 ms 47020 KB Output is correct
91 Correct 236 ms 44756 KB Output is correct
92 Correct 317 ms 44668 KB Output is correct
93 Correct 304 ms 53968 KB Output is correct
94 Correct 373 ms 57512 KB Output is correct
95 Correct 339 ms 61664 KB Output is correct
96 Correct 333 ms 42928 KB Output is correct
97 Correct 353 ms 50316 KB Output is correct