Submission #1002106

# Submission time Handle Problem Language Result Execution time Memory
1002106 2024-06-19T09:59:10 Z underwaterkillerwhale Swapping Cities (APIO20_swap) C++17
100 / 100
226 ms 77568 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)4e5+10;
const int mxLg = 20;
int n, m, newN, dfs_time;
int st[mxN], en[mxN], 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;
	st[s] = dfs_time++;
	for(auto u : adj[s]) 
		if(u!=p) dfs(u,s);
	en[s] = dfs_time;
}
 
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]];
}
 
inline bool anc(int a, int b){
	return st[a]<=st[b] and en[a]>=en[b];
}
 
int lca(int a, int b){
	if(anc(a,b)) return a;
	if(anc(b,a)) return b;
	for(int i = mxLg-1; i>=0; i--)
		if(jmp[i][a]!=-1 and !anc(jmp[i][a],b))
			a = jmp[i][a];
	if(!anc(jmp[0][a],b)) 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 14 ms 41048 KB Output is correct
2 Correct 14 ms 41052 KB Output is correct
3 Correct 14 ms 41052 KB Output is correct
4 Correct 18 ms 41084 KB Output is correct
5 Correct 15 ms 41304 KB Output is correct
6 Correct 14 ms 41308 KB Output is correct
7 Correct 14 ms 41304 KB Output is correct
8 Correct 13 ms 41352 KB Output is correct
9 Correct 62 ms 54168 KB Output is correct
10 Correct 78 ms 57004 KB Output is correct
11 Correct 71 ms 56776 KB Output is correct
12 Correct 78 ms 57612 KB Output is correct
13 Correct 65 ms 59960 KB Output is correct
14 Correct 66 ms 54472 KB Output is correct
15 Correct 117 ms 61156 KB Output is correct
16 Correct 132 ms 60788 KB Output is correct
17 Correct 123 ms 61744 KB Output is correct
18 Correct 178 ms 63996 KB Output is correct
19 Correct 60 ms 48916 KB Output is correct
20 Correct 120 ms 62348 KB Output is correct
21 Correct 137 ms 62176 KB Output is correct
22 Correct 125 ms 63088 KB Output is correct
23 Correct 196 ms 65576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 41048 KB Output is correct
2 Correct 14 ms 41052 KB Output is correct
3 Correct 162 ms 66408 KB Output is correct
4 Correct 159 ms 67372 KB Output is correct
5 Correct 169 ms 66980 KB Output is correct
6 Correct 153 ms 67176 KB Output is correct
7 Correct 158 ms 67140 KB Output is correct
8 Correct 161 ms 66268 KB Output is correct
9 Correct 166 ms 66884 KB Output is correct
10 Correct 157 ms 66176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 41048 KB Output is correct
2 Correct 14 ms 41052 KB Output is correct
3 Correct 14 ms 41052 KB Output is correct
4 Correct 18 ms 41084 KB Output is correct
5 Correct 15 ms 41304 KB Output is correct
6 Correct 14 ms 41308 KB Output is correct
7 Correct 14 ms 41304 KB Output is correct
8 Correct 13 ms 41352 KB Output is correct
9 Correct 14 ms 41048 KB Output is correct
10 Correct 14 ms 41164 KB Output is correct
11 Correct 13 ms 41308 KB Output is correct
12 Correct 14 ms 41308 KB Output is correct
13 Correct 14 ms 41304 KB Output is correct
14 Correct 13 ms 41308 KB Output is correct
15 Correct 14 ms 41308 KB Output is correct
16 Correct 13 ms 41136 KB Output is correct
17 Correct 13 ms 41304 KB Output is correct
18 Correct 14 ms 41308 KB Output is correct
19 Correct 14 ms 41304 KB Output is correct
20 Correct 14 ms 41344 KB Output is correct
21 Correct 14 ms 41308 KB Output is correct
22 Correct 15 ms 41388 KB Output is correct
23 Correct 14 ms 41308 KB Output is correct
24 Correct 14 ms 41308 KB Output is correct
25 Correct 13 ms 41308 KB Output is correct
26 Correct 15 ms 41292 KB Output is correct
27 Correct 13 ms 41308 KB Output is correct
28 Correct 14 ms 41308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 41048 KB Output is correct
2 Correct 14 ms 41048 KB Output is correct
3 Correct 14 ms 41052 KB Output is correct
4 Correct 14 ms 41052 KB Output is correct
5 Correct 18 ms 41084 KB Output is correct
6 Correct 15 ms 41304 KB Output is correct
7 Correct 14 ms 41308 KB Output is correct
8 Correct 14 ms 41304 KB Output is correct
9 Correct 13 ms 41352 KB Output is correct
10 Correct 62 ms 54168 KB Output is correct
11 Correct 78 ms 57004 KB Output is correct
12 Correct 71 ms 56776 KB Output is correct
13 Correct 78 ms 57612 KB Output is correct
14 Correct 65 ms 59960 KB Output is correct
15 Correct 14 ms 41164 KB Output is correct
16 Correct 13 ms 41308 KB Output is correct
17 Correct 14 ms 41308 KB Output is correct
18 Correct 14 ms 41304 KB Output is correct
19 Correct 13 ms 41308 KB Output is correct
20 Correct 14 ms 41308 KB Output is correct
21 Correct 13 ms 41136 KB Output is correct
22 Correct 13 ms 41304 KB Output is correct
23 Correct 14 ms 41308 KB Output is correct
24 Correct 14 ms 41304 KB Output is correct
25 Correct 14 ms 41344 KB Output is correct
26 Correct 14 ms 41308 KB Output is correct
27 Correct 15 ms 41388 KB Output is correct
28 Correct 14 ms 41308 KB Output is correct
29 Correct 14 ms 41308 KB Output is correct
30 Correct 13 ms 41308 KB Output is correct
31 Correct 15 ms 41292 KB Output is correct
32 Correct 13 ms 41308 KB Output is correct
33 Correct 14 ms 41308 KB Output is correct
34 Correct 20 ms 43332 KB Output is correct
35 Correct 77 ms 57336 KB Output is correct
36 Correct 78 ms 57520 KB Output is correct
37 Correct 78 ms 57756 KB Output is correct
38 Correct 73 ms 57568 KB Output is correct
39 Correct 70 ms 57288 KB Output is correct
40 Correct 67 ms 56004 KB Output is correct
41 Correct 78 ms 57540 KB Output is correct
42 Correct 76 ms 57284 KB Output is correct
43 Correct 65 ms 60872 KB Output is correct
44 Correct 77 ms 57996 KB Output is correct
45 Correct 93 ms 66720 KB Output is correct
46 Correct 77 ms 57540 KB Output is correct
47 Correct 78 ms 58052 KB Output is correct
48 Correct 83 ms 60616 KB Output is correct
49 Correct 81 ms 65988 KB Output is correct
50 Correct 68 ms 60648 KB Output is correct
51 Correct 87 ms 63432 KB Output is correct
52 Correct 113 ms 67516 KB Output is correct
53 Correct 124 ms 69808 KB Output is correct
54 Correct 122 ms 73668 KB Output is correct
55 Correct 66 ms 60528 KB Output is correct
56 Correct 119 ms 71080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 41048 KB Output is correct
2 Correct 14 ms 41052 KB Output is correct
3 Correct 14 ms 41052 KB Output is correct
4 Correct 18 ms 41084 KB Output is correct
5 Correct 15 ms 41304 KB Output is correct
6 Correct 14 ms 41308 KB Output is correct
7 Correct 14 ms 41304 KB Output is correct
8 Correct 13 ms 41352 KB Output is correct
9 Correct 62 ms 54168 KB Output is correct
10 Correct 78 ms 57004 KB Output is correct
11 Correct 71 ms 56776 KB Output is correct
12 Correct 78 ms 57612 KB Output is correct
13 Correct 65 ms 59960 KB Output is correct
14 Correct 66 ms 54472 KB Output is correct
15 Correct 117 ms 61156 KB Output is correct
16 Correct 132 ms 60788 KB Output is correct
17 Correct 123 ms 61744 KB Output is correct
18 Correct 178 ms 63996 KB Output is correct
19 Correct 162 ms 66408 KB Output is correct
20 Correct 159 ms 67372 KB Output is correct
21 Correct 169 ms 66980 KB Output is correct
22 Correct 153 ms 67176 KB Output is correct
23 Correct 158 ms 67140 KB Output is correct
24 Correct 161 ms 66268 KB Output is correct
25 Correct 166 ms 66884 KB Output is correct
26 Correct 157 ms 66176 KB Output is correct
27 Correct 14 ms 41164 KB Output is correct
28 Correct 13 ms 41308 KB Output is correct
29 Correct 14 ms 41308 KB Output is correct
30 Correct 14 ms 41304 KB Output is correct
31 Correct 13 ms 41308 KB Output is correct
32 Correct 14 ms 41308 KB Output is correct
33 Correct 13 ms 41136 KB Output is correct
34 Correct 13 ms 41304 KB Output is correct
35 Correct 14 ms 41308 KB Output is correct
36 Correct 20 ms 43332 KB Output is correct
37 Correct 77 ms 57336 KB Output is correct
38 Correct 78 ms 57520 KB Output is correct
39 Correct 78 ms 57756 KB Output is correct
40 Correct 73 ms 57568 KB Output is correct
41 Correct 70 ms 57288 KB Output is correct
42 Correct 67 ms 56004 KB Output is correct
43 Correct 78 ms 57540 KB Output is correct
44 Correct 76 ms 57284 KB Output is correct
45 Correct 65 ms 60872 KB Output is correct
46 Correct 77 ms 57996 KB Output is correct
47 Correct 27 ms 43796 KB Output is correct
48 Correct 124 ms 62256 KB Output is correct
49 Correct 126 ms 62340 KB Output is correct
50 Correct 127 ms 62512 KB Output is correct
51 Correct 131 ms 62488 KB Output is correct
52 Correct 124 ms 61696 KB Output is correct
53 Correct 118 ms 58300 KB Output is correct
54 Correct 138 ms 63224 KB Output is correct
55 Correct 122 ms 62344 KB Output is correct
56 Correct 162 ms 65028 KB Output is correct
57 Correct 149 ms 63708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 41048 KB Output is correct
2 Correct 14 ms 41048 KB Output is correct
3 Correct 14 ms 41052 KB Output is correct
4 Correct 14 ms 41052 KB Output is correct
5 Correct 18 ms 41084 KB Output is correct
6 Correct 15 ms 41304 KB Output is correct
7 Correct 14 ms 41308 KB Output is correct
8 Correct 14 ms 41304 KB Output is correct
9 Correct 13 ms 41352 KB Output is correct
10 Correct 62 ms 54168 KB Output is correct
11 Correct 78 ms 57004 KB Output is correct
12 Correct 71 ms 56776 KB Output is correct
13 Correct 78 ms 57612 KB Output is correct
14 Correct 65 ms 59960 KB Output is correct
15 Correct 66 ms 54472 KB Output is correct
16 Correct 117 ms 61156 KB Output is correct
17 Correct 132 ms 60788 KB Output is correct
18 Correct 123 ms 61744 KB Output is correct
19 Correct 178 ms 63996 KB Output is correct
20 Correct 162 ms 66408 KB Output is correct
21 Correct 159 ms 67372 KB Output is correct
22 Correct 169 ms 66980 KB Output is correct
23 Correct 153 ms 67176 KB Output is correct
24 Correct 158 ms 67140 KB Output is correct
25 Correct 161 ms 66268 KB Output is correct
26 Correct 166 ms 66884 KB Output is correct
27 Correct 157 ms 66176 KB Output is correct
28 Correct 14 ms 41164 KB Output is correct
29 Correct 13 ms 41308 KB Output is correct
30 Correct 14 ms 41308 KB Output is correct
31 Correct 14 ms 41304 KB Output is correct
32 Correct 13 ms 41308 KB Output is correct
33 Correct 14 ms 41308 KB Output is correct
34 Correct 13 ms 41136 KB Output is correct
35 Correct 13 ms 41304 KB Output is correct
36 Correct 14 ms 41308 KB Output is correct
37 Correct 20 ms 43332 KB Output is correct
38 Correct 77 ms 57336 KB Output is correct
39 Correct 78 ms 57520 KB Output is correct
40 Correct 78 ms 57756 KB Output is correct
41 Correct 73 ms 57568 KB Output is correct
42 Correct 70 ms 57288 KB Output is correct
43 Correct 67 ms 56004 KB Output is correct
44 Correct 78 ms 57540 KB Output is correct
45 Correct 76 ms 57284 KB Output is correct
46 Correct 65 ms 60872 KB Output is correct
47 Correct 77 ms 57996 KB Output is correct
48 Correct 27 ms 43796 KB Output is correct
49 Correct 124 ms 62256 KB Output is correct
50 Correct 126 ms 62340 KB Output is correct
51 Correct 127 ms 62512 KB Output is correct
52 Correct 131 ms 62488 KB Output is correct
53 Correct 124 ms 61696 KB Output is correct
54 Correct 118 ms 58300 KB Output is correct
55 Correct 138 ms 63224 KB Output is correct
56 Correct 122 ms 62344 KB Output is correct
57 Correct 162 ms 65028 KB Output is correct
58 Correct 149 ms 63708 KB Output is correct
59 Correct 60 ms 48916 KB Output is correct
60 Correct 120 ms 62348 KB Output is correct
61 Correct 137 ms 62176 KB Output is correct
62 Correct 125 ms 63088 KB Output is correct
63 Correct 196 ms 65576 KB Output is correct
64 Correct 14 ms 41304 KB Output is correct
65 Correct 14 ms 41344 KB Output is correct
66 Correct 14 ms 41308 KB Output is correct
67 Correct 15 ms 41388 KB Output is correct
68 Correct 14 ms 41308 KB Output is correct
69 Correct 14 ms 41308 KB Output is correct
70 Correct 13 ms 41308 KB Output is correct
71 Correct 15 ms 41292 KB Output is correct
72 Correct 13 ms 41308 KB Output is correct
73 Correct 14 ms 41308 KB Output is correct
74 Correct 93 ms 66720 KB Output is correct
75 Correct 77 ms 57540 KB Output is correct
76 Correct 78 ms 58052 KB Output is correct
77 Correct 83 ms 60616 KB Output is correct
78 Correct 81 ms 65988 KB Output is correct
79 Correct 68 ms 60648 KB Output is correct
80 Correct 87 ms 63432 KB Output is correct
81 Correct 113 ms 67516 KB Output is correct
82 Correct 124 ms 69808 KB Output is correct
83 Correct 122 ms 73668 KB Output is correct
84 Correct 66 ms 60528 KB Output is correct
85 Correct 119 ms 71080 KB Output is correct
86 Correct 65 ms 50892 KB Output is correct
87 Correct 134 ms 62512 KB Output is correct
88 Correct 130 ms 62508 KB Output is correct
89 Correct 160 ms 64740 KB Output is correct
90 Correct 158 ms 71452 KB Output is correct
91 Correct 164 ms 69608 KB Output is correct
92 Correct 180 ms 67556 KB Output is correct
93 Correct 188 ms 72060 KB Output is correct
94 Correct 226 ms 74328 KB Output is correct
95 Correct 207 ms 77568 KB Output is correct
96 Correct 161 ms 65292 KB Output is correct
97 Correct 195 ms 70020 KB Output is correct