Submission #293656

# Submission time Handle Problem Language Result Execution time Memory
293656 2020-09-08T09:09:56 Z errorgorn Swapping Cities (APIO20_swap) C++14
100 / 100
474 ms 56988 KB
#include "swap.h"

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ii pair<ll,ll>
#define iii pair<ll,ii>
#define fi first
#define se second

#define rep(x,s,e) for (auto x=s-(s>e);x!=e-(s>e);(s<e?x++:x--))
#define sz(x) (int)(x).size()
#define all(x) (x).begin(),(x).end()

const int INF=1e9+100;

int n,m;
vector<ii> al[100005];
vector<iii> edges,loop;

int p[200005]; //ufds
int find(int i){
	if (i==p[i]) return i;
	else return p[i]=find(p[i]);
}

ll w[200005]; //weight of kruskal tree
ll extra[200005]; //extra stuff (fork, loop)
int pa[200005][20]; //2k decomp on kruskal tree
int d[200005]; //depth for lca query
vector<int> child[200005]; //stupid al

int IDX;

int mov(int i,int k){
	rep(x,0,20) if (k&(1<<x)) {
		i=pa[i][x];
	}
	return i;
}

int lca(int i,int j){
	if (d[i]<d[j]) swap(i,j);
	
	i=mov(i,d[i]-d[j]);
	if (i==j) return i;
	
	rep(x,20,0){
		if (pa[i][x]!=pa[j][x]){
			i=pa[i][x];
			j=pa[j][x];
		}
	}
	
	return pa[i][0];
}

void init(int N, int M,
          std::vector<int> U, std::vector<int> V, std::vector<int> W) {
	
	rep(x,0,200005) p[x]=x;
	memset(pa,-1,sizeof(pa));
	memset(extra,63,sizeof(extra));
	
	n=N,m=M;
	
	rep(x,0,m){
		al[U[x]].push_back(ii(V[x],W[x]));
		al[V[x]].push_back(ii(U[x],W[x]));
		edges.push_back(iii(W[x],ii(V[x],U[x])));
	}
	
	rep(x,0,n){
		vector<int> temp;
		for (auto &it:al[x]) temp.push_back(it.se);
		sort(all(temp));
		
		if (sz(temp)>=3) extra[x]=temp[2];
	}
	
	IDX=n;
	
	sort(all(edges));
	for (auto &it:edges){
		int u=find(it.se.fi),v=find(it.se.se);
		
		if (u==v){
			loop.push_back(it);
			continue;
		}
		w[IDX]=it.fi;
		p[u]=p[v]=IDX;
		child[IDX].push_back(u);
		child[IDX].push_back(v);
		IDX++;
	}
	
	rep(node,IDX,0){
		for (auto &it:child[node]){
			d[it]=d[node]+1;
			int curr=pa[it][0]=node;
			rep(x,0,20){
				if (curr==-1) break;
				curr=pa[it][x+1]=pa[curr][x];
			}
		}
	}
	
	for (auto &it:loop){
		int temp=lca(it.se.se,it.se.se);
		extra[temp]=min(extra[temp],it.fi);
	}
	
	rep(x,0,IDX){
		for (auto &it:child[x]){
			extra[x]=min(extra[x],extra[it]);
		}
		w[x]=max(w[x],extra[x]);
	}
	
	rep(x,IDX,0){
		for (auto &it:child[x]){
			w[it]=min(w[it],w[x]);
		}
	}
}

int getMinimumFuelCapacity(int u, int v) {
	if (w[IDX-1]>=INF) return -1;
	
	u=lca(u,v);
	if (w[u]!=INF) return w[u];
	
	return w[pa[u][0]];
}
# Verdict Execution time Memory Grader output
1 Correct 15 ms 25344 KB Output is correct
2 Correct 15 ms 25344 KB Output is correct
3 Correct 15 ms 25344 KB Output is correct
4 Correct 15 ms 25472 KB Output is correct
5 Correct 16 ms 25472 KB Output is correct
6 Correct 15 ms 25468 KB Output is correct
7 Correct 15 ms 25472 KB Output is correct
8 Correct 15 ms 25472 KB Output is correct
9 Correct 142 ms 37288 KB Output is correct
10 Correct 160 ms 39848 KB Output is correct
11 Correct 197 ms 39804 KB Output is correct
12 Correct 173 ms 40488 KB Output is correct
13 Correct 164 ms 41280 KB Output is correct
14 Correct 142 ms 37284 KB Output is correct
15 Correct 222 ms 41804 KB Output is correct
16 Correct 226 ms 41428 KB Output is correct
17 Correct 240 ms 42380 KB Output is correct
18 Correct 240 ms 43024 KB Output is correct
19 Correct 116 ms 31284 KB Output is correct
20 Correct 297 ms 43136 KB Output is correct
21 Correct 302 ms 42720 KB Output is correct
22 Correct 317 ms 43664 KB Output is correct
23 Correct 439 ms 44428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 25344 KB Output is correct
2 Correct 15 ms 25344 KB Output is correct
3 Correct 367 ms 42568 KB Output is correct
4 Correct 370 ms 43068 KB Output is correct
5 Correct 381 ms 42992 KB Output is correct
6 Correct 366 ms 42940 KB Output is correct
7 Correct 379 ms 43096 KB Output is correct
8 Correct 353 ms 42376 KB Output is correct
9 Correct 362 ms 42968 KB Output is correct
10 Correct 357 ms 42384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 25344 KB Output is correct
2 Correct 15 ms 25344 KB Output is correct
3 Correct 15 ms 25344 KB Output is correct
4 Correct 15 ms 25344 KB Output is correct
5 Correct 15 ms 25472 KB Output is correct
6 Correct 16 ms 25472 KB Output is correct
7 Correct 15 ms 25468 KB Output is correct
8 Correct 15 ms 25472 KB Output is correct
9 Correct 15 ms 25472 KB Output is correct
10 Correct 16 ms 25472 KB Output is correct
11 Correct 16 ms 25472 KB Output is correct
12 Correct 16 ms 25472 KB Output is correct
13 Correct 16 ms 25472 KB Output is correct
14 Correct 15 ms 25472 KB Output is correct
15 Correct 16 ms 25472 KB Output is correct
16 Correct 16 ms 25504 KB Output is correct
17 Correct 16 ms 25472 KB Output is correct
18 Correct 16 ms 25472 KB Output is correct
19 Correct 17 ms 25472 KB Output is correct
20 Correct 15 ms 25472 KB Output is correct
21 Correct 17 ms 25600 KB Output is correct
22 Correct 16 ms 25600 KB Output is correct
23 Correct 15 ms 25472 KB Output is correct
24 Correct 16 ms 25600 KB Output is correct
25 Correct 17 ms 25600 KB Output is correct
26 Correct 16 ms 25600 KB Output is correct
27 Correct 15 ms 25472 KB Output is correct
28 Correct 17 ms 25728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 25344 KB Output is correct
2 Correct 15 ms 25344 KB Output is correct
3 Correct 15 ms 25344 KB Output is correct
4 Correct 15 ms 25344 KB Output is correct
5 Correct 15 ms 25472 KB Output is correct
6 Correct 16 ms 25472 KB Output is correct
7 Correct 15 ms 25468 KB Output is correct
8 Correct 15 ms 25472 KB Output is correct
9 Correct 15 ms 25472 KB Output is correct
10 Correct 142 ms 37288 KB Output is correct
11 Correct 160 ms 39848 KB Output is correct
12 Correct 197 ms 39804 KB Output is correct
13 Correct 173 ms 40488 KB Output is correct
14 Correct 164 ms 41280 KB Output is correct
15 Correct 16 ms 25472 KB Output is correct
16 Correct 16 ms 25472 KB Output is correct
17 Correct 16 ms 25472 KB Output is correct
18 Correct 16 ms 25472 KB Output is correct
19 Correct 15 ms 25472 KB Output is correct
20 Correct 16 ms 25472 KB Output is correct
21 Correct 16 ms 25504 KB Output is correct
22 Correct 16 ms 25472 KB Output is correct
23 Correct 16 ms 25472 KB Output is correct
24 Correct 30 ms 27516 KB Output is correct
25 Correct 157 ms 40488 KB Output is correct
26 Correct 158 ms 40488 KB Output is correct
27 Correct 164 ms 40488 KB Output is correct
28 Correct 170 ms 40360 KB Output is correct
29 Correct 163 ms 40228 KB Output is correct
30 Correct 151 ms 39208 KB Output is correct
31 Correct 174 ms 40488 KB Output is correct
32 Correct 166 ms 40464 KB Output is correct
33 Correct 167 ms 40484 KB Output is correct
34 Correct 177 ms 40612 KB Output is correct
35 Correct 17 ms 25472 KB Output is correct
36 Correct 15 ms 25472 KB Output is correct
37 Correct 17 ms 25600 KB Output is correct
38 Correct 16 ms 25600 KB Output is correct
39 Correct 15 ms 25472 KB Output is correct
40 Correct 16 ms 25600 KB Output is correct
41 Correct 17 ms 25600 KB Output is correct
42 Correct 16 ms 25600 KB Output is correct
43 Correct 15 ms 25472 KB Output is correct
44 Correct 17 ms 25728 KB Output is correct
45 Correct 242 ms 47848 KB Output is correct
46 Correct 160 ms 40484 KB Output is correct
47 Correct 187 ms 40572 KB Output is correct
48 Correct 222 ms 42024 KB Output is correct
49 Correct 133 ms 51740 KB Output is correct
50 Correct 114 ms 42276 KB Output is correct
51 Correct 199 ms 43688 KB Output is correct
52 Correct 251 ms 49308 KB Output is correct
53 Correct 305 ms 51488 KB Output is correct
54 Correct 316 ms 55328 KB Output is correct
55 Correct 160 ms 40616 KB Output is correct
56 Correct 300 ms 51484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 25344 KB Output is correct
2 Correct 15 ms 25344 KB Output is correct
3 Correct 15 ms 25344 KB Output is correct
4 Correct 15 ms 25472 KB Output is correct
5 Correct 16 ms 25472 KB Output is correct
6 Correct 15 ms 25468 KB Output is correct
7 Correct 15 ms 25472 KB Output is correct
8 Correct 15 ms 25472 KB Output is correct
9 Correct 142 ms 37288 KB Output is correct
10 Correct 160 ms 39848 KB Output is correct
11 Correct 197 ms 39804 KB Output is correct
12 Correct 173 ms 40488 KB Output is correct
13 Correct 164 ms 41280 KB Output is correct
14 Correct 142 ms 37284 KB Output is correct
15 Correct 222 ms 41804 KB Output is correct
16 Correct 226 ms 41428 KB Output is correct
17 Correct 240 ms 42380 KB Output is correct
18 Correct 240 ms 43024 KB Output is correct
19 Correct 367 ms 42568 KB Output is correct
20 Correct 370 ms 43068 KB Output is correct
21 Correct 381 ms 42992 KB Output is correct
22 Correct 366 ms 42940 KB Output is correct
23 Correct 379 ms 43096 KB Output is correct
24 Correct 353 ms 42376 KB Output is correct
25 Correct 362 ms 42968 KB Output is correct
26 Correct 357 ms 42384 KB Output is correct
27 Correct 16 ms 25472 KB Output is correct
28 Correct 16 ms 25472 KB Output is correct
29 Correct 16 ms 25472 KB Output is correct
30 Correct 16 ms 25472 KB Output is correct
31 Correct 15 ms 25472 KB Output is correct
32 Correct 16 ms 25472 KB Output is correct
33 Correct 16 ms 25504 KB Output is correct
34 Correct 16 ms 25472 KB Output is correct
35 Correct 16 ms 25472 KB Output is correct
36 Correct 30 ms 27516 KB Output is correct
37 Correct 157 ms 40488 KB Output is correct
38 Correct 158 ms 40488 KB Output is correct
39 Correct 164 ms 40488 KB Output is correct
40 Correct 170 ms 40360 KB Output is correct
41 Correct 163 ms 40228 KB Output is correct
42 Correct 151 ms 39208 KB Output is correct
43 Correct 174 ms 40488 KB Output is correct
44 Correct 166 ms 40464 KB Output is correct
45 Correct 167 ms 40484 KB Output is correct
46 Correct 177 ms 40612 KB Output is correct
47 Correct 41 ms 27572 KB Output is correct
48 Correct 320 ms 43020 KB Output is correct
49 Correct 317 ms 43020 KB Output is correct
50 Correct 321 ms 43148 KB Output is correct
51 Correct 340 ms 43052 KB Output is correct
52 Correct 364 ms 42196 KB Output is correct
53 Correct 300 ms 39068 KB Output is correct
54 Correct 336 ms 43636 KB Output is correct
55 Correct 321 ms 43080 KB Output is correct
56 Correct 474 ms 43092 KB Output is correct
57 Correct 383 ms 43856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 25344 KB Output is correct
2 Correct 15 ms 25344 KB Output is correct
3 Correct 15 ms 25344 KB Output is correct
4 Correct 15 ms 25344 KB Output is correct
5 Correct 15 ms 25472 KB Output is correct
6 Correct 16 ms 25472 KB Output is correct
7 Correct 15 ms 25468 KB Output is correct
8 Correct 15 ms 25472 KB Output is correct
9 Correct 15 ms 25472 KB Output is correct
10 Correct 142 ms 37288 KB Output is correct
11 Correct 160 ms 39848 KB Output is correct
12 Correct 197 ms 39804 KB Output is correct
13 Correct 173 ms 40488 KB Output is correct
14 Correct 164 ms 41280 KB Output is correct
15 Correct 142 ms 37284 KB Output is correct
16 Correct 222 ms 41804 KB Output is correct
17 Correct 226 ms 41428 KB Output is correct
18 Correct 240 ms 42380 KB Output is correct
19 Correct 240 ms 43024 KB Output is correct
20 Correct 367 ms 42568 KB Output is correct
21 Correct 370 ms 43068 KB Output is correct
22 Correct 381 ms 42992 KB Output is correct
23 Correct 366 ms 42940 KB Output is correct
24 Correct 379 ms 43096 KB Output is correct
25 Correct 353 ms 42376 KB Output is correct
26 Correct 362 ms 42968 KB Output is correct
27 Correct 357 ms 42384 KB Output is correct
28 Correct 16 ms 25472 KB Output is correct
29 Correct 16 ms 25472 KB Output is correct
30 Correct 16 ms 25472 KB Output is correct
31 Correct 16 ms 25472 KB Output is correct
32 Correct 15 ms 25472 KB Output is correct
33 Correct 16 ms 25472 KB Output is correct
34 Correct 16 ms 25504 KB Output is correct
35 Correct 16 ms 25472 KB Output is correct
36 Correct 16 ms 25472 KB Output is correct
37 Correct 30 ms 27516 KB Output is correct
38 Correct 157 ms 40488 KB Output is correct
39 Correct 158 ms 40488 KB Output is correct
40 Correct 164 ms 40488 KB Output is correct
41 Correct 170 ms 40360 KB Output is correct
42 Correct 163 ms 40228 KB Output is correct
43 Correct 151 ms 39208 KB Output is correct
44 Correct 174 ms 40488 KB Output is correct
45 Correct 166 ms 40464 KB Output is correct
46 Correct 167 ms 40484 KB Output is correct
47 Correct 177 ms 40612 KB Output is correct
48 Correct 41 ms 27572 KB Output is correct
49 Correct 320 ms 43020 KB Output is correct
50 Correct 317 ms 43020 KB Output is correct
51 Correct 321 ms 43148 KB Output is correct
52 Correct 340 ms 43052 KB Output is correct
53 Correct 364 ms 42196 KB Output is correct
54 Correct 300 ms 39068 KB Output is correct
55 Correct 336 ms 43636 KB Output is correct
56 Correct 321 ms 43080 KB Output is correct
57 Correct 474 ms 43092 KB Output is correct
58 Correct 383 ms 43856 KB Output is correct
59 Correct 116 ms 31284 KB Output is correct
60 Correct 297 ms 43136 KB Output is correct
61 Correct 302 ms 42720 KB Output is correct
62 Correct 317 ms 43664 KB Output is correct
63 Correct 439 ms 44428 KB Output is correct
64 Correct 17 ms 25472 KB Output is correct
65 Correct 15 ms 25472 KB Output is correct
66 Correct 17 ms 25600 KB Output is correct
67 Correct 16 ms 25600 KB Output is correct
68 Correct 15 ms 25472 KB Output is correct
69 Correct 16 ms 25600 KB Output is correct
70 Correct 17 ms 25600 KB Output is correct
71 Correct 16 ms 25600 KB Output is correct
72 Correct 15 ms 25472 KB Output is correct
73 Correct 17 ms 25728 KB Output is correct
74 Correct 242 ms 47848 KB Output is correct
75 Correct 160 ms 40484 KB Output is correct
76 Correct 187 ms 40572 KB Output is correct
77 Correct 222 ms 42024 KB Output is correct
78 Correct 133 ms 51740 KB Output is correct
79 Correct 114 ms 42276 KB Output is correct
80 Correct 199 ms 43688 KB Output is correct
81 Correct 251 ms 49308 KB Output is correct
82 Correct 305 ms 51488 KB Output is correct
83 Correct 316 ms 55328 KB Output is correct
84 Correct 160 ms 40616 KB Output is correct
85 Correct 300 ms 51484 KB Output is correct
86 Correct 122 ms 33324 KB Output is correct
87 Correct 358 ms 43276 KB Output is correct
88 Correct 351 ms 43024 KB Output is correct
89 Correct 398 ms 43460 KB Output is correct
90 Correct 240 ms 51868 KB Output is correct
91 Correct 256 ms 50460 KB Output is correct
92 Correct 359 ms 45064 KB Output is correct
93 Correct 427 ms 50644 KB Output is correct
94 Correct 469 ms 53660 KB Output is correct
95 Correct 446 ms 56988 KB Output is correct
96 Correct 451 ms 43296 KB Output is correct
97 Correct 456 ms 47712 KB Output is correct