Submission #897131

# Submission time Handle Problem Language Result Execution time Memory
897131 2024-01-02T15:16:56 Z LCJLY Swapping Cities (APIO20_swap) C++14
100 / 100
286 ms 94108 KB
#include "swap.h"
 
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int>pii;
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << "  " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << "  " << j << " " << #i << "  " << q << " " << #p << endl; 
#define show4(x,y) for(auto it:x) cout << it << " "; cout << #y << endl;
 
vector<int>adj[500005];
long long cost[500005];
 
struct DSU{
	vector<int>e;
	void init(int n){
		e=vector<int>(n,-1);
	}
	
	int get(int x){return e[x]<0?x:e[x]=get(e[x]);}
	
	bool unite(int x, int y){ //x is par
		x=get(x),y=get(y);
		if(x==y) return 0;
		adj[x].push_back(y);
		adj[y].push_back(x);
		e[x]+=e[y];
		e[y]=x;
		return 1;
	}
};
 
int two[22][500005];
int d[500005];
void dfs(int index, int par){
	for(int x=0;x<20;x++){
		if(two[x][index]==-1) break;
		two[x+1][index]=two[x][two[x][index]];
	}
	
	for(auto it:adj[index]){
		if(it==par) continue;
		two[0][it]=index;
		d[it]=d[index]+1;
		dfs(it,index);
	}
}
 
int lca(int a, int b){
	if(d[a]>d[b]) swap(a,b);
	int diff=d[b]-d[a];
	for(int x=0;x<20;x++){
		if(diff&(1<<x)) b=two[x][b];
	}
	
	if(a==b) return a;
	for(int x=19;x>=0;x--){
		if(two[x][b]!=two[x][a]){
			a=two[x][a];
			b=two[x][b];
		}
	}
	return two[0][a];
}
 
bool done[300005];
 
 
int root=0;
int hayden=0;
void init(int n, int m, vector<int> u, vector<int> v, vector<int> w){
	hayden=m;
	pair<int,pii>edge[m];
	for(int x=0;x<m;x++){
		edge[x].first=w[x];
		edge[x].second={u[x],v[x]};
	}
	
	sort(edge,edge+m);
	
	DSU dsu;
	dsu.init(n+m+20);
	
	int in[n+5];
	memset(in,0,sizeof(in));
	
	for(int x=0;x<m;x++){
		int l=edge[x].second.first;
		int r=edge[x].second.second;
		int hold=edge[x].first;
		in[l]++;
		in[r]++;
		
		if(in[l]>2||in[r]>2) done[n+x+1]=true;
		l=dsu.get(l);
		r=dsu.get(r);
		if(l==r||done[l]||done[r]) done[n+x+1]=true;
		dsu.unite(n+x+1,l);
		dsu.unite(n+x+1,r);
		cost[n+x+1]=hold;
	}
	
	memset(two,-1,sizeof(two));
	
	dfs(n+m,-1);
	root=n+m;
}
 
int getMinimumFuelCapacity(int x, int y){
	int hold=lca(x,y);
	
	if(done[hold]){
		return cost[hold];
	}
	
	int cur=hold;
	for(int bit=19;bit>=0;bit--){
		if(d[cur]>=(1<<bit)){
			if(!done[two[bit][cur]]){
				cur=two[bit][cur];
			}
		}
	}
	if(hayden<=2) assert(cur==root);
	if(cur==root) return -1;
	return cost[two[0][cur]];
}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 56920 KB Output is correct
2 Correct 9 ms 56924 KB Output is correct
3 Correct 8 ms 57084 KB Output is correct
4 Correct 11 ms 56920 KB Output is correct
5 Correct 9 ms 57180 KB Output is correct
6 Correct 10 ms 57180 KB Output is correct
7 Correct 9 ms 59232 KB Output is correct
8 Correct 9 ms 59224 KB Output is correct
9 Correct 63 ms 68168 KB Output is correct
10 Correct 81 ms 70228 KB Output is correct
11 Correct 83 ms 70232 KB Output is correct
12 Correct 87 ms 70740 KB Output is correct
13 Correct 88 ms 74644 KB Output is correct
14 Correct 71 ms 68300 KB Output is correct
15 Correct 192 ms 72108 KB Output is correct
16 Correct 192 ms 71760 KB Output is correct
17 Correct 163 ms 72272 KB Output is correct
18 Correct 217 ms 76192 KB Output is correct
19 Correct 75 ms 64452 KB Output is correct
20 Correct 157 ms 72712 KB Output is correct
21 Correct 158 ms 72456 KB Output is correct
22 Correct 165 ms 73124 KB Output is correct
23 Correct 211 ms 77220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 56920 KB Output is correct
2 Correct 9 ms 56924 KB Output is correct
3 Correct 166 ms 81240 KB Output is correct
4 Correct 170 ms 82084 KB Output is correct
5 Correct 246 ms 81604 KB Output is correct
6 Correct 174 ms 81980 KB Output is correct
7 Correct 175 ms 81876 KB Output is correct
8 Correct 165 ms 81096 KB Output is correct
9 Correct 177 ms 81616 KB Output is correct
10 Correct 166 ms 80948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 56920 KB Output is correct
2 Correct 9 ms 56924 KB Output is correct
3 Correct 8 ms 57084 KB Output is correct
4 Correct 11 ms 56920 KB Output is correct
5 Correct 9 ms 57180 KB Output is correct
6 Correct 10 ms 57180 KB Output is correct
7 Correct 9 ms 59232 KB Output is correct
8 Correct 9 ms 59224 KB Output is correct
9 Correct 8 ms 56924 KB Output is correct
10 Correct 9 ms 59064 KB Output is correct
11 Correct 9 ms 59228 KB Output is correct
12 Correct 9 ms 59232 KB Output is correct
13 Correct 9 ms 57180 KB Output is correct
14 Correct 9 ms 57180 KB Output is correct
15 Correct 10 ms 59068 KB Output is correct
16 Correct 9 ms 59196 KB Output is correct
17 Correct 9 ms 59224 KB Output is correct
18 Correct 9 ms 59224 KB Output is correct
19 Correct 9 ms 57176 KB Output is correct
20 Correct 9 ms 59228 KB Output is correct
21 Correct 10 ms 59224 KB Output is correct
22 Correct 11 ms 59352 KB Output is correct
23 Correct 9 ms 57176 KB Output is correct
24 Correct 11 ms 59228 KB Output is correct
25 Correct 10 ms 59388 KB Output is correct
26 Correct 10 ms 59352 KB Output is correct
27 Correct 9 ms 59480 KB Output is correct
28 Correct 10 ms 59328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 56924 KB Output is correct
2 Correct 9 ms 56920 KB Output is correct
3 Correct 9 ms 56924 KB Output is correct
4 Correct 8 ms 57084 KB Output is correct
5 Correct 11 ms 56920 KB Output is correct
6 Correct 9 ms 57180 KB Output is correct
7 Correct 10 ms 57180 KB Output is correct
8 Correct 9 ms 59232 KB Output is correct
9 Correct 9 ms 59224 KB Output is correct
10 Correct 63 ms 68168 KB Output is correct
11 Correct 81 ms 70228 KB Output is correct
12 Correct 83 ms 70232 KB Output is correct
13 Correct 87 ms 70740 KB Output is correct
14 Correct 88 ms 74644 KB Output is correct
15 Correct 9 ms 59064 KB Output is correct
16 Correct 9 ms 59228 KB Output is correct
17 Correct 9 ms 59232 KB Output is correct
18 Correct 9 ms 57180 KB Output is correct
19 Correct 9 ms 57180 KB Output is correct
20 Correct 10 ms 59068 KB Output is correct
21 Correct 9 ms 59196 KB Output is correct
22 Correct 9 ms 59224 KB Output is correct
23 Correct 9 ms 59224 KB Output is correct
24 Correct 9 ms 57176 KB Output is correct
25 Correct 9 ms 59228 KB Output is correct
26 Correct 10 ms 59224 KB Output is correct
27 Correct 11 ms 59352 KB Output is correct
28 Correct 9 ms 57176 KB Output is correct
29 Correct 11 ms 59228 KB Output is correct
30 Correct 10 ms 59388 KB Output is correct
31 Correct 10 ms 59352 KB Output is correct
32 Correct 9 ms 59480 KB Output is correct
33 Correct 10 ms 59328 KB Output is correct
34 Correct 17 ms 60760 KB Output is correct
35 Correct 82 ms 70740 KB Output is correct
36 Correct 97 ms 70996 KB Output is correct
37 Correct 83 ms 72232 KB Output is correct
38 Correct 95 ms 71920 KB Output is correct
39 Correct 84 ms 72016 KB Output is correct
40 Correct 95 ms 71420 KB Output is correct
41 Correct 89 ms 70920 KB Output is correct
42 Correct 89 ms 70680 KB Output is correct
43 Correct 85 ms 77368 KB Output is correct
44 Correct 91 ms 72292 KB Output is correct
45 Correct 127 ms 85164 KB Output is correct
46 Correct 83 ms 72788 KB Output is correct
47 Correct 105 ms 74068 KB Output is correct
48 Correct 123 ms 77784 KB Output is correct
49 Correct 90 ms 86288 KB Output is correct
50 Correct 74 ms 79956 KB Output is correct
51 Correct 103 ms 82176 KB Output is correct
52 Correct 151 ms 83732 KB Output is correct
53 Correct 168 ms 86060 KB Output is correct
54 Correct 177 ms 90104 KB Output is correct
55 Correct 87 ms 78672 KB Output is correct
56 Correct 160 ms 89000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 56920 KB Output is correct
2 Correct 9 ms 56924 KB Output is correct
3 Correct 8 ms 57084 KB Output is correct
4 Correct 11 ms 56920 KB Output is correct
5 Correct 9 ms 57180 KB Output is correct
6 Correct 10 ms 57180 KB Output is correct
7 Correct 9 ms 59232 KB Output is correct
8 Correct 9 ms 59224 KB Output is correct
9 Correct 63 ms 68168 KB Output is correct
10 Correct 81 ms 70228 KB Output is correct
11 Correct 83 ms 70232 KB Output is correct
12 Correct 87 ms 70740 KB Output is correct
13 Correct 88 ms 74644 KB Output is correct
14 Correct 71 ms 68300 KB Output is correct
15 Correct 192 ms 72108 KB Output is correct
16 Correct 192 ms 71760 KB Output is correct
17 Correct 163 ms 72272 KB Output is correct
18 Correct 217 ms 76192 KB Output is correct
19 Correct 166 ms 81240 KB Output is correct
20 Correct 170 ms 82084 KB Output is correct
21 Correct 246 ms 81604 KB Output is correct
22 Correct 174 ms 81980 KB Output is correct
23 Correct 175 ms 81876 KB Output is correct
24 Correct 165 ms 81096 KB Output is correct
25 Correct 177 ms 81616 KB Output is correct
26 Correct 166 ms 80948 KB Output is correct
27 Correct 9 ms 59064 KB Output is correct
28 Correct 9 ms 59228 KB Output is correct
29 Correct 9 ms 59232 KB Output is correct
30 Correct 9 ms 57180 KB Output is correct
31 Correct 9 ms 57180 KB Output is correct
32 Correct 10 ms 59068 KB Output is correct
33 Correct 9 ms 59196 KB Output is correct
34 Correct 9 ms 59224 KB Output is correct
35 Correct 9 ms 59224 KB Output is correct
36 Correct 17 ms 60760 KB Output is correct
37 Correct 82 ms 70740 KB Output is correct
38 Correct 97 ms 70996 KB Output is correct
39 Correct 83 ms 72232 KB Output is correct
40 Correct 95 ms 71920 KB Output is correct
41 Correct 84 ms 72016 KB Output is correct
42 Correct 95 ms 71420 KB Output is correct
43 Correct 89 ms 70920 KB Output is correct
44 Correct 89 ms 70680 KB Output is correct
45 Correct 85 ms 77368 KB Output is correct
46 Correct 91 ms 72292 KB Output is correct
47 Correct 24 ms 60764 KB Output is correct
48 Correct 163 ms 72492 KB Output is correct
49 Correct 159 ms 73980 KB Output is correct
50 Correct 184 ms 74108 KB Output is correct
51 Correct 155 ms 73960 KB Output is correct
52 Correct 161 ms 73452 KB Output is correct
53 Correct 143 ms 69900 KB Output is correct
54 Correct 180 ms 73124 KB Output is correct
55 Correct 163 ms 72580 KB Output is correct
56 Correct 201 ms 78072 KB Output is correct
57 Correct 170 ms 74660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 56924 KB Output is correct
2 Correct 9 ms 56920 KB Output is correct
3 Correct 9 ms 56924 KB Output is correct
4 Correct 8 ms 57084 KB Output is correct
5 Correct 11 ms 56920 KB Output is correct
6 Correct 9 ms 57180 KB Output is correct
7 Correct 10 ms 57180 KB Output is correct
8 Correct 9 ms 59232 KB Output is correct
9 Correct 9 ms 59224 KB Output is correct
10 Correct 63 ms 68168 KB Output is correct
11 Correct 81 ms 70228 KB Output is correct
12 Correct 83 ms 70232 KB Output is correct
13 Correct 87 ms 70740 KB Output is correct
14 Correct 88 ms 74644 KB Output is correct
15 Correct 71 ms 68300 KB Output is correct
16 Correct 192 ms 72108 KB Output is correct
17 Correct 192 ms 71760 KB Output is correct
18 Correct 163 ms 72272 KB Output is correct
19 Correct 217 ms 76192 KB Output is correct
20 Correct 166 ms 81240 KB Output is correct
21 Correct 170 ms 82084 KB Output is correct
22 Correct 246 ms 81604 KB Output is correct
23 Correct 174 ms 81980 KB Output is correct
24 Correct 175 ms 81876 KB Output is correct
25 Correct 165 ms 81096 KB Output is correct
26 Correct 177 ms 81616 KB Output is correct
27 Correct 166 ms 80948 KB Output is correct
28 Correct 9 ms 59064 KB Output is correct
29 Correct 9 ms 59228 KB Output is correct
30 Correct 9 ms 59232 KB Output is correct
31 Correct 9 ms 57180 KB Output is correct
32 Correct 9 ms 57180 KB Output is correct
33 Correct 10 ms 59068 KB Output is correct
34 Correct 9 ms 59196 KB Output is correct
35 Correct 9 ms 59224 KB Output is correct
36 Correct 9 ms 59224 KB Output is correct
37 Correct 17 ms 60760 KB Output is correct
38 Correct 82 ms 70740 KB Output is correct
39 Correct 97 ms 70996 KB Output is correct
40 Correct 83 ms 72232 KB Output is correct
41 Correct 95 ms 71920 KB Output is correct
42 Correct 84 ms 72016 KB Output is correct
43 Correct 95 ms 71420 KB Output is correct
44 Correct 89 ms 70920 KB Output is correct
45 Correct 89 ms 70680 KB Output is correct
46 Correct 85 ms 77368 KB Output is correct
47 Correct 91 ms 72292 KB Output is correct
48 Correct 24 ms 60764 KB Output is correct
49 Correct 163 ms 72492 KB Output is correct
50 Correct 159 ms 73980 KB Output is correct
51 Correct 184 ms 74108 KB Output is correct
52 Correct 155 ms 73960 KB Output is correct
53 Correct 161 ms 73452 KB Output is correct
54 Correct 143 ms 69900 KB Output is correct
55 Correct 180 ms 73124 KB Output is correct
56 Correct 163 ms 72580 KB Output is correct
57 Correct 201 ms 78072 KB Output is correct
58 Correct 170 ms 74660 KB Output is correct
59 Correct 75 ms 64452 KB Output is correct
60 Correct 157 ms 72712 KB Output is correct
61 Correct 158 ms 72456 KB Output is correct
62 Correct 165 ms 73124 KB Output is correct
63 Correct 211 ms 77220 KB Output is correct
64 Correct 9 ms 57176 KB Output is correct
65 Correct 9 ms 59228 KB Output is correct
66 Correct 10 ms 59224 KB Output is correct
67 Correct 11 ms 59352 KB Output is correct
68 Correct 9 ms 57176 KB Output is correct
69 Correct 11 ms 59228 KB Output is correct
70 Correct 10 ms 59388 KB Output is correct
71 Correct 10 ms 59352 KB Output is correct
72 Correct 9 ms 59480 KB Output is correct
73 Correct 10 ms 59328 KB Output is correct
74 Correct 127 ms 85164 KB Output is correct
75 Correct 83 ms 72788 KB Output is correct
76 Correct 105 ms 74068 KB Output is correct
77 Correct 123 ms 77784 KB Output is correct
78 Correct 90 ms 86288 KB Output is correct
79 Correct 74 ms 79956 KB Output is correct
80 Correct 103 ms 82176 KB Output is correct
81 Correct 151 ms 83732 KB Output is correct
82 Correct 168 ms 86060 KB Output is correct
83 Correct 177 ms 90104 KB Output is correct
84 Correct 87 ms 78672 KB Output is correct
85 Correct 160 ms 89000 KB Output is correct
86 Correct 61 ms 68436 KB Output is correct
87 Correct 179 ms 76804 KB Output is correct
88 Correct 158 ms 76712 KB Output is correct
89 Correct 216 ms 81256 KB Output is correct
90 Correct 164 ms 91236 KB Output is correct
91 Correct 156 ms 88576 KB Output is correct
92 Correct 189 ms 85460 KB Output is correct
93 Correct 227 ms 87120 KB Output is correct
94 Correct 286 ms 89972 KB Output is correct
95 Correct 264 ms 94108 KB Output is correct
96 Correct 213 ms 82544 KB Output is correct
97 Correct 278 ms 86468 KB Output is correct