Submission #897129

# Submission time Handle Problem Language Result Execution time Memory
897129 2024-01-02T15:14:54 Z LCJLY Swapping Cities (APIO20_swap) C++14
36 / 100
657 ms 131660 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;
#define int long long

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;
void init(int32_t n, int32_t m, vector<int32_t> u, vector<int32_t> v, vector<int32_t> w){
	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));
	
	root=n+1;
	
	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]++;
	
		l=dsu.get(l);
		r=dsu.get(r);
		if(l==r){
			done[l]=true;
			cost[l]=hold;
		}
		else{				
			if(done[l]||done[r]) done[root]=true;
			if(in[edge[x].second.first]>2||in[edge[x].second.second]>2) done[root]=true;
			dsu.unite(root,l);
			dsu.unite(root,r);
			cost[root]=hold;
			root++;
		}
	}
	root--;
	memset(two,-1,sizeof(two));
	
	dfs(root,-1);
}

int32_t getMinimumFuelCapacity(int32_t x, int32_t 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(cur==root) return -1;
	return cost[two[0][cur]];
}
# Verdict Execution time Memory Grader output
1 Correct 26 ms 101980 KB Output is correct
2 Correct 16 ms 102080 KB Output is correct
3 Correct 14 ms 101980 KB Output is correct
4 Correct 14 ms 101980 KB Output is correct
5 Correct 14 ms 102236 KB Output is correct
6 Correct 15 ms 102228 KB Output is correct
7 Correct 14 ms 102236 KB Output is correct
8 Correct 16 ms 102132 KB Output is correct
9 Correct 78 ms 116720 KB Output is correct
10 Correct 106 ms 119488 KB Output is correct
11 Correct 99 ms 119340 KB Output is correct
12 Correct 112 ms 120036 KB Output is correct
13 Correct 106 ms 122848 KB Output is correct
14 Correct 140 ms 116708 KB Output is correct
15 Correct 555 ms 121264 KB Output is correct
16 Correct 449 ms 120736 KB Output is correct
17 Correct 543 ms 121588 KB Output is correct
18 Correct 657 ms 124496 KB Output is correct
19 Correct 116 ms 109908 KB Output is correct
20 Correct 510 ms 125404 KB Output is correct
21 Correct 534 ms 125132 KB Output is correct
22 Correct 498 ms 126064 KB Output is correct
23 Correct 580 ms 128880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 101980 KB Output is correct
2 Correct 16 ms 102080 KB Output is correct
3 Correct 594 ms 127000 KB Output is correct
4 Correct 607 ms 128084 KB Output is correct
5 Correct 601 ms 127496 KB Output is correct
6 Correct 548 ms 131660 KB Output is correct
7 Correct 577 ms 131648 KB Output is correct
8 Correct 589 ms 130740 KB Output is correct
9 Correct 603 ms 131480 KB Output is correct
10 Correct 586 ms 130656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 101980 KB Output is correct
2 Correct 16 ms 102080 KB Output is correct
3 Correct 14 ms 101980 KB Output is correct
4 Correct 14 ms 101980 KB Output is correct
5 Correct 14 ms 102236 KB Output is correct
6 Correct 15 ms 102228 KB Output is correct
7 Correct 14 ms 102236 KB Output is correct
8 Correct 16 ms 102132 KB Output is correct
9 Correct 13 ms 101976 KB Output is correct
10 Correct 15 ms 102236 KB Output is correct
11 Correct 15 ms 102240 KB Output is correct
12 Correct 14 ms 102232 KB Output is correct
13 Correct 14 ms 102236 KB Output is correct
14 Correct 14 ms 102236 KB Output is correct
15 Correct 14 ms 102212 KB Output is correct
16 Correct 15 ms 102232 KB Output is correct
17 Correct 14 ms 102252 KB Output is correct
18 Correct 15 ms 102240 KB Output is correct
19 Incorrect 14 ms 102140 KB Output isn't correct
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 13 ms 101976 KB Output is correct
2 Correct 26 ms 101980 KB Output is correct
3 Correct 16 ms 102080 KB Output is correct
4 Correct 14 ms 101980 KB Output is correct
5 Correct 14 ms 101980 KB Output is correct
6 Correct 14 ms 102236 KB Output is correct
7 Correct 15 ms 102228 KB Output is correct
8 Correct 14 ms 102236 KB Output is correct
9 Correct 16 ms 102132 KB Output is correct
10 Correct 78 ms 116720 KB Output is correct
11 Correct 106 ms 119488 KB Output is correct
12 Correct 99 ms 119340 KB Output is correct
13 Correct 112 ms 120036 KB Output is correct
14 Correct 106 ms 122848 KB Output is correct
15 Correct 15 ms 102236 KB Output is correct
16 Correct 15 ms 102240 KB Output is correct
17 Correct 14 ms 102232 KB Output is correct
18 Correct 14 ms 102236 KB Output is correct
19 Correct 14 ms 102236 KB Output is correct
20 Correct 14 ms 102212 KB Output is correct
21 Correct 15 ms 102232 KB Output is correct
22 Correct 14 ms 102252 KB Output is correct
23 Correct 15 ms 102240 KB Output is correct
24 Incorrect 14 ms 102140 KB Output isn't correct
25 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 26 ms 101980 KB Output is correct
2 Correct 16 ms 102080 KB Output is correct
3 Correct 14 ms 101980 KB Output is correct
4 Correct 14 ms 101980 KB Output is correct
5 Correct 14 ms 102236 KB Output is correct
6 Correct 15 ms 102228 KB Output is correct
7 Correct 14 ms 102236 KB Output is correct
8 Correct 16 ms 102132 KB Output is correct
9 Correct 78 ms 116720 KB Output is correct
10 Correct 106 ms 119488 KB Output is correct
11 Correct 99 ms 119340 KB Output is correct
12 Correct 112 ms 120036 KB Output is correct
13 Correct 106 ms 122848 KB Output is correct
14 Correct 140 ms 116708 KB Output is correct
15 Correct 555 ms 121264 KB Output is correct
16 Correct 449 ms 120736 KB Output is correct
17 Correct 543 ms 121588 KB Output is correct
18 Correct 657 ms 124496 KB Output is correct
19 Correct 594 ms 127000 KB Output is correct
20 Correct 607 ms 128084 KB Output is correct
21 Correct 601 ms 127496 KB Output is correct
22 Correct 548 ms 131660 KB Output is correct
23 Correct 577 ms 131648 KB Output is correct
24 Correct 589 ms 130740 KB Output is correct
25 Correct 603 ms 131480 KB Output is correct
26 Correct 586 ms 130656 KB Output is correct
27 Correct 15 ms 102236 KB Output is correct
28 Correct 15 ms 102240 KB Output is correct
29 Correct 14 ms 102232 KB Output is correct
30 Correct 14 ms 102236 KB Output is correct
31 Correct 14 ms 102236 KB Output is correct
32 Correct 14 ms 102212 KB Output is correct
33 Correct 15 ms 102232 KB Output is correct
34 Correct 14 ms 102252 KB Output is correct
35 Correct 15 ms 102240 KB Output is correct
36 Correct 26 ms 106596 KB Output is correct
37 Correct 119 ms 122052 KB Output is correct
38 Correct 109 ms 122036 KB Output is correct
39 Correct 100 ms 122592 KB Output is correct
40 Correct 101 ms 122196 KB Output is correct
41 Correct 105 ms 122192 KB Output is correct
42 Correct 102 ms 120824 KB Output is correct
43 Correct 102 ms 122132 KB Output is correct
44 Correct 122 ms 121924 KB Output is correct
45 Correct 103 ms 126292 KB Output is correct
46 Correct 116 ms 122576 KB Output is correct
47 Correct 34 ms 106748 KB Output is correct
48 Correct 489 ms 125804 KB Output is correct
49 Correct 461 ms 126424 KB Output is correct
50 Correct 463 ms 126288 KB Output is correct
51 Correct 355 ms 126548 KB Output is correct
52 Correct 340 ms 125268 KB Output is correct
53 Correct 311 ms 120704 KB Output is correct
54 Correct 425 ms 126164 KB Output is correct
55 Correct 494 ms 125784 KB Output is correct
56 Correct 518 ms 129364 KB Output is correct
57 Correct 410 ms 126612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 101976 KB Output is correct
2 Correct 26 ms 101980 KB Output is correct
3 Correct 16 ms 102080 KB Output is correct
4 Correct 14 ms 101980 KB Output is correct
5 Correct 14 ms 101980 KB Output is correct
6 Correct 14 ms 102236 KB Output is correct
7 Correct 15 ms 102228 KB Output is correct
8 Correct 14 ms 102236 KB Output is correct
9 Correct 16 ms 102132 KB Output is correct
10 Correct 78 ms 116720 KB Output is correct
11 Correct 106 ms 119488 KB Output is correct
12 Correct 99 ms 119340 KB Output is correct
13 Correct 112 ms 120036 KB Output is correct
14 Correct 106 ms 122848 KB Output is correct
15 Correct 140 ms 116708 KB Output is correct
16 Correct 555 ms 121264 KB Output is correct
17 Correct 449 ms 120736 KB Output is correct
18 Correct 543 ms 121588 KB Output is correct
19 Correct 657 ms 124496 KB Output is correct
20 Correct 594 ms 127000 KB Output is correct
21 Correct 607 ms 128084 KB Output is correct
22 Correct 601 ms 127496 KB Output is correct
23 Correct 548 ms 131660 KB Output is correct
24 Correct 577 ms 131648 KB Output is correct
25 Correct 589 ms 130740 KB Output is correct
26 Correct 603 ms 131480 KB Output is correct
27 Correct 586 ms 130656 KB Output is correct
28 Correct 15 ms 102236 KB Output is correct
29 Correct 15 ms 102240 KB Output is correct
30 Correct 14 ms 102232 KB Output is correct
31 Correct 14 ms 102236 KB Output is correct
32 Correct 14 ms 102236 KB Output is correct
33 Correct 14 ms 102212 KB Output is correct
34 Correct 15 ms 102232 KB Output is correct
35 Correct 14 ms 102252 KB Output is correct
36 Correct 15 ms 102240 KB Output is correct
37 Correct 26 ms 106596 KB Output is correct
38 Correct 119 ms 122052 KB Output is correct
39 Correct 109 ms 122036 KB Output is correct
40 Correct 100 ms 122592 KB Output is correct
41 Correct 101 ms 122196 KB Output is correct
42 Correct 105 ms 122192 KB Output is correct
43 Correct 102 ms 120824 KB Output is correct
44 Correct 102 ms 122132 KB Output is correct
45 Correct 122 ms 121924 KB Output is correct
46 Correct 103 ms 126292 KB Output is correct
47 Correct 116 ms 122576 KB Output is correct
48 Correct 34 ms 106748 KB Output is correct
49 Correct 489 ms 125804 KB Output is correct
50 Correct 461 ms 126424 KB Output is correct
51 Correct 463 ms 126288 KB Output is correct
52 Correct 355 ms 126548 KB Output is correct
53 Correct 340 ms 125268 KB Output is correct
54 Correct 311 ms 120704 KB Output is correct
55 Correct 425 ms 126164 KB Output is correct
56 Correct 494 ms 125784 KB Output is correct
57 Correct 518 ms 129364 KB Output is correct
58 Correct 410 ms 126612 KB Output is correct
59 Correct 116 ms 109908 KB Output is correct
60 Correct 510 ms 125404 KB Output is correct
61 Correct 534 ms 125132 KB Output is correct
62 Correct 498 ms 126064 KB Output is correct
63 Correct 580 ms 128880 KB Output is correct
64 Incorrect 14 ms 102140 KB Output isn't correct
65 Halted 0 ms 0 KB -