Submission #996977

# Submission time Handle Problem Language Result Execution time Memory
996977 2024-06-11T14:26:12 Z Dan4Life Swapping Cities (APIO20_swap) C++17
100 / 100
206 ms 60096 KB
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
 
#define all(a) begin(a),end(a)
#define sz(a) (int)a.size()
#define pb push_back
 
const int mxN = (int)3e5+10;
const int mxLg = 20;
int n, m, newN, deg[mxN];
int p[mxN], line[mxN], good[mxN]; 
vector<array<int,3>> edges;
int jmp[mxLg][mxN], dep[mxN];
vector<int> adj[mxN];
 
int findSet(int i){return p[i]==i?i:p[i]=findSet(p[i]);}
bool isSameSet(int i, int j) { return findSet(i)==findSet(j); }
 
void unionSet(int i, int j){
	deg[i]++, deg[j]++;
	
	int x = findSet(i), y = findSet(j);
	
	if(x==y) line[x] = 0;
	if(!line[x] or !line[y] or deg[i]>=3 or deg[j]>=3 or good[x] or good[y])
		line[x]=line[y]=0, good[newN]=1;
		
	adj[newN].pb(y),adj[y].pb(newN);
	if(x!=y) adj[newN].pb(x),adj[x].pb(newN);
	
	p[y] = p[x] = p[newN] = newN; newN++;
}
 
void dfs(int s, int p){
	if(p!=-1) dep[s] = dep[p]+1;
	else dep[s] = 0;
	jmp[0][s] = p;
	for(auto u : adj[s]) 
		if(u!=p) dfs(u,s);
}
 
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
	n = N, m = M; newN = N;
	for(int i = 0; i < N+M; i++) 
		p[i]=i,deg[i]=0,line[i]=1;
	for(int i = 0; i < M; i++)
		edges.pb({W[i],U[i],V[i]});
	sort(all(edges),[&](array<int,3> a, array<int,3> b){
		return a[0]<b[0];
	});
	
	memset(jmp,-1,sizeof(jmp));
	
	for(int i = 0; i < M; i++)
		unionSet(edges[i][1],edges[i][2]);
		
	for(int i = newN-1; i>=0; i--) 
		if(!dep[i]) dfs(i,-1);
		
	for(int i = 1; i < mxLg; i++)
		for(int j = 0; j < newN; j++)
			if(jmp[i-1][j]!=-1) jmp[i][j] = jmp[i-1][jmp[i-1][j]];
}
 
int jump(int s, int x){
	for(int k = mxLg-1; k>=0 and s!=-1; k--)
		if((x>>k)&1) s = jmp[k][s];
	return s;
}
 
int lca(int a, int b){
	if(a==b) return a;
	if(jmp[0][a]==jmp[0][b]) return jmp[0][a];
	if(dep[a]>dep[b]) swap(a,b);
	if(dep[a]!=dep[b]) b = jump(b,dep[b]-dep[a]); 
	for(int i = mxLg-1; i>=0; i--)
		if(jmp[i][a]!=jmp[i][b] and jmp[i][a]!=-1) 
			a=jmp[i][a],b=jmp[i][b];
	if(jmp[0][a]!=jmp[0][b] or jmp[0][a]==-1) return -1;
	return jmp[0][a];
}
 
int findNearestAnc(int s){
	if(s==-1 or good[s]) return s;
	for(int i = mxLg-1; i>=0; i--)
		if(jmp[i][s]!=-1 and !good[jmp[i][s]])
			s = jmp[i][s];
	if(jmp[0][s]==-1 or !good[jmp[0][s]]) return -1;
	return jmp[0][s];
}
 
int getMinimumFuelCapacity(int X, int Y) {
	int Z = findNearestAnc(lca(X,Y));
	return Z==-1?-1:edges[Z-n][0];
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 35416 KB Output is correct
2 Correct 5 ms 35420 KB Output is correct
3 Correct 5 ms 35420 KB Output is correct
4 Correct 5 ms 35468 KB Output is correct
5 Correct 5 ms 35420 KB Output is correct
6 Correct 5 ms 35608 KB Output is correct
7 Correct 7 ms 35420 KB Output is correct
8 Correct 5 ms 35416 KB Output is correct
9 Correct 58 ms 43720 KB Output is correct
10 Correct 98 ms 45644 KB Output is correct
11 Correct 69 ms 45376 KB Output is correct
12 Correct 112 ms 46024 KB Output is correct
13 Correct 59 ms 48332 KB Output is correct
14 Correct 68 ms 43716 KB Output is correct
15 Correct 142 ms 47380 KB Output is correct
16 Correct 138 ms 47116 KB Output is correct
17 Correct 160 ms 47608 KB Output is correct
18 Correct 174 ms 50112 KB Output is correct
19 Correct 68 ms 40424 KB Output is correct
20 Correct 145 ms 48540 KB Output is correct
21 Correct 156 ms 48372 KB Output is correct
22 Correct 135 ms 49176 KB Output is correct
23 Correct 179 ms 51468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 35416 KB Output is correct
2 Correct 5 ms 35420 KB Output is correct
3 Correct 137 ms 52844 KB Output is correct
4 Correct 142 ms 53492 KB Output is correct
5 Correct 136 ms 53180 KB Output is correct
6 Correct 143 ms 53384 KB Output is correct
7 Correct 164 ms 53320 KB Output is correct
8 Correct 132 ms 52836 KB Output is correct
9 Correct 134 ms 53192 KB Output is correct
10 Correct 141 ms 52612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 35416 KB Output is correct
2 Correct 5 ms 35420 KB Output is correct
3 Correct 5 ms 35420 KB Output is correct
4 Correct 5 ms 35468 KB Output is correct
5 Correct 5 ms 35420 KB Output is correct
6 Correct 5 ms 35608 KB Output is correct
7 Correct 7 ms 35420 KB Output is correct
8 Correct 5 ms 35416 KB Output is correct
9 Correct 5 ms 35420 KB Output is correct
10 Correct 5 ms 35564 KB Output is correct
11 Correct 5 ms 35420 KB Output is correct
12 Correct 5 ms 35604 KB Output is correct
13 Correct 4 ms 35420 KB Output is correct
14 Correct 5 ms 35776 KB Output is correct
15 Correct 5 ms 35420 KB Output is correct
16 Correct 5 ms 35444 KB Output is correct
17 Correct 5 ms 35420 KB Output is correct
18 Correct 5 ms 35420 KB Output is correct
19 Correct 5 ms 35620 KB Output is correct
20 Correct 5 ms 35552 KB Output is correct
21 Correct 5 ms 35420 KB Output is correct
22 Correct 5 ms 35676 KB Output is correct
23 Correct 5 ms 35552 KB Output is correct
24 Correct 6 ms 35676 KB Output is correct
25 Correct 5 ms 35672 KB Output is correct
26 Correct 6 ms 35676 KB Output is correct
27 Correct 5 ms 35420 KB Output is correct
28 Correct 5 ms 35676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 35420 KB Output is correct
2 Correct 4 ms 35416 KB Output is correct
3 Correct 5 ms 35420 KB Output is correct
4 Correct 5 ms 35420 KB Output is correct
5 Correct 5 ms 35468 KB Output is correct
6 Correct 5 ms 35420 KB Output is correct
7 Correct 5 ms 35608 KB Output is correct
8 Correct 7 ms 35420 KB Output is correct
9 Correct 5 ms 35416 KB Output is correct
10 Correct 58 ms 43720 KB Output is correct
11 Correct 98 ms 45644 KB Output is correct
12 Correct 69 ms 45376 KB Output is correct
13 Correct 112 ms 46024 KB Output is correct
14 Correct 59 ms 48332 KB Output is correct
15 Correct 5 ms 35564 KB Output is correct
16 Correct 5 ms 35420 KB Output is correct
17 Correct 5 ms 35604 KB Output is correct
18 Correct 4 ms 35420 KB Output is correct
19 Correct 5 ms 35776 KB Output is correct
20 Correct 5 ms 35420 KB Output is correct
21 Correct 5 ms 35444 KB Output is correct
22 Correct 5 ms 35420 KB Output is correct
23 Correct 5 ms 35420 KB Output is correct
24 Correct 5 ms 35620 KB Output is correct
25 Correct 5 ms 35552 KB Output is correct
26 Correct 5 ms 35420 KB Output is correct
27 Correct 5 ms 35676 KB Output is correct
28 Correct 5 ms 35552 KB Output is correct
29 Correct 6 ms 35676 KB Output is correct
30 Correct 5 ms 35672 KB Output is correct
31 Correct 6 ms 35676 KB Output is correct
32 Correct 5 ms 35420 KB Output is correct
33 Correct 5 ms 35676 KB Output is correct
34 Correct 12 ms 36828 KB Output is correct
35 Correct 79 ms 46092 KB Output is correct
36 Correct 75 ms 46024 KB Output is correct
37 Correct 70 ms 45984 KB Output is correct
38 Correct 64 ms 45828 KB Output is correct
39 Correct 71 ms 45764 KB Output is correct
40 Correct 63 ms 45000 KB Output is correct
41 Correct 89 ms 45952 KB Output is correct
42 Correct 73 ms 46024 KB Output is correct
43 Correct 61 ms 49168 KB Output is correct
44 Correct 76 ms 46024 KB Output is correct
45 Correct 93 ms 53444 KB Output is correct
46 Correct 81 ms 46060 KB Output is correct
47 Correct 76 ms 46060 KB Output is correct
48 Correct 76 ms 48436 KB Output is correct
49 Correct 69 ms 54840 KB Output is correct
50 Correct 85 ms 49536 KB Output is correct
51 Correct 78 ms 50668 KB Output is correct
52 Correct 134 ms 53064 KB Output is correct
53 Correct 116 ms 54308 KB Output is correct
54 Correct 154 ms 57896 KB Output is correct
55 Correct 61 ms 48780 KB Output is correct
56 Correct 109 ms 56816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 35416 KB Output is correct
2 Correct 5 ms 35420 KB Output is correct
3 Correct 5 ms 35420 KB Output is correct
4 Correct 5 ms 35468 KB Output is correct
5 Correct 5 ms 35420 KB Output is correct
6 Correct 5 ms 35608 KB Output is correct
7 Correct 7 ms 35420 KB Output is correct
8 Correct 5 ms 35416 KB Output is correct
9 Correct 58 ms 43720 KB Output is correct
10 Correct 98 ms 45644 KB Output is correct
11 Correct 69 ms 45376 KB Output is correct
12 Correct 112 ms 46024 KB Output is correct
13 Correct 59 ms 48332 KB Output is correct
14 Correct 68 ms 43716 KB Output is correct
15 Correct 142 ms 47380 KB Output is correct
16 Correct 138 ms 47116 KB Output is correct
17 Correct 160 ms 47608 KB Output is correct
18 Correct 174 ms 50112 KB Output is correct
19 Correct 137 ms 52844 KB Output is correct
20 Correct 142 ms 53492 KB Output is correct
21 Correct 136 ms 53180 KB Output is correct
22 Correct 143 ms 53384 KB Output is correct
23 Correct 164 ms 53320 KB Output is correct
24 Correct 132 ms 52836 KB Output is correct
25 Correct 134 ms 53192 KB Output is correct
26 Correct 141 ms 52612 KB Output is correct
27 Correct 5 ms 35564 KB Output is correct
28 Correct 5 ms 35420 KB Output is correct
29 Correct 5 ms 35604 KB Output is correct
30 Correct 4 ms 35420 KB Output is correct
31 Correct 5 ms 35776 KB Output is correct
32 Correct 5 ms 35420 KB Output is correct
33 Correct 5 ms 35444 KB Output is correct
34 Correct 5 ms 35420 KB Output is correct
35 Correct 5 ms 35420 KB Output is correct
36 Correct 12 ms 36828 KB Output is correct
37 Correct 79 ms 46092 KB Output is correct
38 Correct 75 ms 46024 KB Output is correct
39 Correct 70 ms 45984 KB Output is correct
40 Correct 64 ms 45828 KB Output is correct
41 Correct 71 ms 45764 KB Output is correct
42 Correct 63 ms 45000 KB Output is correct
43 Correct 89 ms 45952 KB Output is correct
44 Correct 73 ms 46024 KB Output is correct
45 Correct 61 ms 49168 KB Output is correct
46 Correct 76 ms 46024 KB Output is correct
47 Correct 22 ms 37140 KB Output is correct
48 Correct 134 ms 48432 KB Output is correct
49 Correct 131 ms 48432 KB Output is correct
50 Correct 170 ms 48432 KB Output is correct
51 Correct 129 ms 48436 KB Output is correct
52 Correct 121 ms 47988 KB Output is correct
53 Correct 110 ms 46012 KB Output is correct
54 Correct 130 ms 49176 KB Output is correct
55 Correct 125 ms 48428 KB Output is correct
56 Correct 143 ms 51048 KB Output is correct
57 Correct 141 ms 49196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 35420 KB Output is correct
2 Correct 4 ms 35416 KB Output is correct
3 Correct 5 ms 35420 KB Output is correct
4 Correct 5 ms 35420 KB Output is correct
5 Correct 5 ms 35468 KB Output is correct
6 Correct 5 ms 35420 KB Output is correct
7 Correct 5 ms 35608 KB Output is correct
8 Correct 7 ms 35420 KB Output is correct
9 Correct 5 ms 35416 KB Output is correct
10 Correct 58 ms 43720 KB Output is correct
11 Correct 98 ms 45644 KB Output is correct
12 Correct 69 ms 45376 KB Output is correct
13 Correct 112 ms 46024 KB Output is correct
14 Correct 59 ms 48332 KB Output is correct
15 Correct 68 ms 43716 KB Output is correct
16 Correct 142 ms 47380 KB Output is correct
17 Correct 138 ms 47116 KB Output is correct
18 Correct 160 ms 47608 KB Output is correct
19 Correct 174 ms 50112 KB Output is correct
20 Correct 137 ms 52844 KB Output is correct
21 Correct 142 ms 53492 KB Output is correct
22 Correct 136 ms 53180 KB Output is correct
23 Correct 143 ms 53384 KB Output is correct
24 Correct 164 ms 53320 KB Output is correct
25 Correct 132 ms 52836 KB Output is correct
26 Correct 134 ms 53192 KB Output is correct
27 Correct 141 ms 52612 KB Output is correct
28 Correct 5 ms 35564 KB Output is correct
29 Correct 5 ms 35420 KB Output is correct
30 Correct 5 ms 35604 KB Output is correct
31 Correct 4 ms 35420 KB Output is correct
32 Correct 5 ms 35776 KB Output is correct
33 Correct 5 ms 35420 KB Output is correct
34 Correct 5 ms 35444 KB Output is correct
35 Correct 5 ms 35420 KB Output is correct
36 Correct 5 ms 35420 KB Output is correct
37 Correct 12 ms 36828 KB Output is correct
38 Correct 79 ms 46092 KB Output is correct
39 Correct 75 ms 46024 KB Output is correct
40 Correct 70 ms 45984 KB Output is correct
41 Correct 64 ms 45828 KB Output is correct
42 Correct 71 ms 45764 KB Output is correct
43 Correct 63 ms 45000 KB Output is correct
44 Correct 89 ms 45952 KB Output is correct
45 Correct 73 ms 46024 KB Output is correct
46 Correct 61 ms 49168 KB Output is correct
47 Correct 76 ms 46024 KB Output is correct
48 Correct 22 ms 37140 KB Output is correct
49 Correct 134 ms 48432 KB Output is correct
50 Correct 131 ms 48432 KB Output is correct
51 Correct 170 ms 48432 KB Output is correct
52 Correct 129 ms 48436 KB Output is correct
53 Correct 121 ms 47988 KB Output is correct
54 Correct 110 ms 46012 KB Output is correct
55 Correct 130 ms 49176 KB Output is correct
56 Correct 125 ms 48428 KB Output is correct
57 Correct 143 ms 51048 KB Output is correct
58 Correct 141 ms 49196 KB Output is correct
59 Correct 68 ms 40424 KB Output is correct
60 Correct 145 ms 48540 KB Output is correct
61 Correct 156 ms 48372 KB Output is correct
62 Correct 135 ms 49176 KB Output is correct
63 Correct 179 ms 51468 KB Output is correct
64 Correct 5 ms 35620 KB Output is correct
65 Correct 5 ms 35552 KB Output is correct
66 Correct 5 ms 35420 KB Output is correct
67 Correct 5 ms 35676 KB Output is correct
68 Correct 5 ms 35552 KB Output is correct
69 Correct 6 ms 35676 KB Output is correct
70 Correct 5 ms 35672 KB Output is correct
71 Correct 6 ms 35676 KB Output is correct
72 Correct 5 ms 35420 KB Output is correct
73 Correct 5 ms 35676 KB Output is correct
74 Correct 93 ms 53444 KB Output is correct
75 Correct 81 ms 46060 KB Output is correct
76 Correct 76 ms 46060 KB Output is correct
77 Correct 76 ms 48436 KB Output is correct
78 Correct 69 ms 54840 KB Output is correct
79 Correct 85 ms 49536 KB Output is correct
80 Correct 78 ms 50668 KB Output is correct
81 Correct 134 ms 53064 KB Output is correct
82 Correct 116 ms 54308 KB Output is correct
83 Correct 154 ms 57896 KB Output is correct
84 Correct 61 ms 48780 KB Output is correct
85 Correct 109 ms 56816 KB Output is correct
86 Correct 50 ms 41852 KB Output is correct
87 Correct 140 ms 48428 KB Output is correct
88 Correct 121 ms 48428 KB Output is correct
89 Correct 153 ms 50448 KB Output is correct
90 Correct 134 ms 57108 KB Output is correct
91 Correct 145 ms 56320 KB Output is correct
92 Correct 170 ms 52720 KB Output is correct
93 Correct 206 ms 56040 KB Output is correct
94 Correct 187 ms 56480 KB Output is correct
95 Correct 188 ms 60096 KB Output is correct
96 Correct 139 ms 51244 KB Output is correct
97 Correct 163 ms 54052 KB Output is correct