Submission #1000703

# Submission time Handle Problem Language Result Execution time Memory
1000703 2024-06-18T07:24:12 Z Dan4Life Swapping Cities (APIO20_swap) C++17
100 / 100
232 ms 78272 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 20 ms 42076 KB Output is correct
2 Correct 15 ms 42076 KB Output is correct
3 Correct 16 ms 42076 KB Output is correct
4 Correct 15 ms 42168 KB Output is correct
5 Correct 16 ms 42328 KB Output is correct
6 Correct 17 ms 42216 KB Output is correct
7 Correct 16 ms 42332 KB Output is correct
8 Correct 15 ms 42252 KB Output is correct
9 Correct 80 ms 55188 KB Output is correct
10 Correct 82 ms 58116 KB Output is correct
11 Correct 78 ms 57860 KB Output is correct
12 Correct 80 ms 58568 KB Output is correct
13 Correct 71 ms 61124 KB Output is correct
14 Correct 70 ms 55296 KB Output is correct
15 Correct 136 ms 62292 KB Output is correct
16 Correct 131 ms 61812 KB Output is correct
17 Correct 137 ms 62780 KB Output is correct
18 Correct 206 ms 65124 KB Output is correct
19 Correct 67 ms 49680 KB Output is correct
20 Correct 132 ms 63232 KB Output is correct
21 Correct 130 ms 63108 KB Output is correct
22 Correct 147 ms 64216 KB Output is correct
23 Correct 207 ms 66528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 42076 KB Output is correct
2 Correct 15 ms 42076 KB Output is correct
3 Correct 168 ms 66920 KB Output is correct
4 Correct 161 ms 67884 KB Output is correct
5 Correct 207 ms 67688 KB Output is correct
6 Correct 179 ms 67656 KB Output is correct
7 Correct 176 ms 67816 KB Output is correct
8 Correct 177 ms 67056 KB Output is correct
9 Correct 172 ms 67652 KB Output is correct
10 Correct 168 ms 66944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 42076 KB Output is correct
2 Correct 15 ms 42076 KB Output is correct
3 Correct 16 ms 42076 KB Output is correct
4 Correct 15 ms 42168 KB Output is correct
5 Correct 16 ms 42328 KB Output is correct
6 Correct 17 ms 42216 KB Output is correct
7 Correct 16 ms 42332 KB Output is correct
8 Correct 15 ms 42252 KB Output is correct
9 Correct 14 ms 42072 KB Output is correct
10 Correct 18 ms 42208 KB Output is correct
11 Correct 14 ms 42332 KB Output is correct
12 Correct 16 ms 42332 KB Output is correct
13 Correct 14 ms 42072 KB Output is correct
14 Correct 15 ms 42076 KB Output is correct
15 Correct 14 ms 42232 KB Output is correct
16 Correct 21 ms 42292 KB Output is correct
17 Correct 14 ms 41272 KB Output is correct
18 Correct 14 ms 41348 KB Output is correct
19 Correct 15 ms 41308 KB Output is correct
20 Correct 15 ms 41308 KB Output is correct
21 Correct 14 ms 41308 KB Output is correct
22 Correct 14 ms 41480 KB Output is correct
23 Correct 14 ms 41148 KB Output is correct
24 Correct 15 ms 41264 KB Output is correct
25 Correct 15 ms 41308 KB Output is correct
26 Correct 15 ms 41272 KB Output is correct
27 Correct 14 ms 41204 KB Output is correct
28 Correct 16 ms 41308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 42072 KB Output is correct
2 Correct 20 ms 42076 KB Output is correct
3 Correct 15 ms 42076 KB Output is correct
4 Correct 16 ms 42076 KB Output is correct
5 Correct 15 ms 42168 KB Output is correct
6 Correct 16 ms 42328 KB Output is correct
7 Correct 17 ms 42216 KB Output is correct
8 Correct 16 ms 42332 KB Output is correct
9 Correct 15 ms 42252 KB Output is correct
10 Correct 80 ms 55188 KB Output is correct
11 Correct 82 ms 58116 KB Output is correct
12 Correct 78 ms 57860 KB Output is correct
13 Correct 80 ms 58568 KB Output is correct
14 Correct 71 ms 61124 KB Output is correct
15 Correct 18 ms 42208 KB Output is correct
16 Correct 14 ms 42332 KB Output is correct
17 Correct 16 ms 42332 KB Output is correct
18 Correct 14 ms 42072 KB Output is correct
19 Correct 15 ms 42076 KB Output is correct
20 Correct 14 ms 42232 KB Output is correct
21 Correct 21 ms 42292 KB Output is correct
22 Correct 14 ms 41272 KB Output is correct
23 Correct 14 ms 41348 KB Output is correct
24 Correct 15 ms 41308 KB Output is correct
25 Correct 15 ms 41308 KB Output is correct
26 Correct 14 ms 41308 KB Output is correct
27 Correct 14 ms 41480 KB Output is correct
28 Correct 14 ms 41148 KB Output is correct
29 Correct 15 ms 41264 KB Output is correct
30 Correct 15 ms 41308 KB Output is correct
31 Correct 15 ms 41272 KB Output is correct
32 Correct 14 ms 41204 KB Output is correct
33 Correct 16 ms 41308 KB Output is correct
34 Correct 21 ms 43284 KB Output is correct
35 Correct 126 ms 57540 KB Output is correct
36 Correct 96 ms 58232 KB Output is correct
37 Correct 101 ms 58308 KB Output is correct
38 Correct 83 ms 57568 KB Output is correct
39 Correct 84 ms 57488 KB Output is correct
40 Correct 83 ms 56008 KB Output is correct
41 Correct 86 ms 57672 KB Output is correct
42 Correct 82 ms 58312 KB Output is correct
43 Correct 71 ms 61480 KB Output is correct
44 Correct 82 ms 58820 KB Output is correct
45 Correct 105 ms 67100 KB Output is correct
46 Correct 80 ms 58348 KB Output is correct
47 Correct 106 ms 58468 KB Output is correct
48 Correct 86 ms 61120 KB Output is correct
49 Correct 82 ms 66492 KB Output is correct
50 Correct 68 ms 61036 KB Output is correct
51 Correct 96 ms 63768 KB Output is correct
52 Correct 116 ms 68412 KB Output is correct
53 Correct 131 ms 70084 KB Output is correct
54 Correct 138 ms 74236 KB Output is correct
55 Correct 68 ms 61124 KB Output is correct
56 Correct 132 ms 71616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 42076 KB Output is correct
2 Correct 15 ms 42076 KB Output is correct
3 Correct 16 ms 42076 KB Output is correct
4 Correct 15 ms 42168 KB Output is correct
5 Correct 16 ms 42328 KB Output is correct
6 Correct 17 ms 42216 KB Output is correct
7 Correct 16 ms 42332 KB Output is correct
8 Correct 15 ms 42252 KB Output is correct
9 Correct 80 ms 55188 KB Output is correct
10 Correct 82 ms 58116 KB Output is correct
11 Correct 78 ms 57860 KB Output is correct
12 Correct 80 ms 58568 KB Output is correct
13 Correct 71 ms 61124 KB Output is correct
14 Correct 70 ms 55296 KB Output is correct
15 Correct 136 ms 62292 KB Output is correct
16 Correct 131 ms 61812 KB Output is correct
17 Correct 137 ms 62780 KB Output is correct
18 Correct 206 ms 65124 KB Output is correct
19 Correct 168 ms 66920 KB Output is correct
20 Correct 161 ms 67884 KB Output is correct
21 Correct 207 ms 67688 KB Output is correct
22 Correct 179 ms 67656 KB Output is correct
23 Correct 176 ms 67816 KB Output is correct
24 Correct 177 ms 67056 KB Output is correct
25 Correct 172 ms 67652 KB Output is correct
26 Correct 168 ms 66944 KB Output is correct
27 Correct 18 ms 42208 KB Output is correct
28 Correct 14 ms 42332 KB Output is correct
29 Correct 16 ms 42332 KB Output is correct
30 Correct 14 ms 42072 KB Output is correct
31 Correct 15 ms 42076 KB Output is correct
32 Correct 14 ms 42232 KB Output is correct
33 Correct 21 ms 42292 KB Output is correct
34 Correct 14 ms 41272 KB Output is correct
35 Correct 14 ms 41348 KB Output is correct
36 Correct 21 ms 43284 KB Output is correct
37 Correct 126 ms 57540 KB Output is correct
38 Correct 96 ms 58232 KB Output is correct
39 Correct 101 ms 58308 KB Output is correct
40 Correct 83 ms 57568 KB Output is correct
41 Correct 84 ms 57488 KB Output is correct
42 Correct 83 ms 56008 KB Output is correct
43 Correct 86 ms 57672 KB Output is correct
44 Correct 82 ms 58312 KB Output is correct
45 Correct 71 ms 61480 KB Output is correct
46 Correct 82 ms 58820 KB Output is correct
47 Correct 28 ms 44564 KB Output is correct
48 Correct 132 ms 63228 KB Output is correct
49 Correct 151 ms 63272 KB Output is correct
50 Correct 132 ms 63272 KB Output is correct
51 Correct 142 ms 63280 KB Output is correct
52 Correct 131 ms 62168 KB Output is correct
53 Correct 114 ms 58808 KB Output is correct
54 Correct 132 ms 64216 KB Output is correct
55 Correct 133 ms 63280 KB Output is correct
56 Correct 166 ms 65580 KB Output is correct
57 Correct 146 ms 64300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 42072 KB Output is correct
2 Correct 20 ms 42076 KB Output is correct
3 Correct 15 ms 42076 KB Output is correct
4 Correct 16 ms 42076 KB Output is correct
5 Correct 15 ms 42168 KB Output is correct
6 Correct 16 ms 42328 KB Output is correct
7 Correct 17 ms 42216 KB Output is correct
8 Correct 16 ms 42332 KB Output is correct
9 Correct 15 ms 42252 KB Output is correct
10 Correct 80 ms 55188 KB Output is correct
11 Correct 82 ms 58116 KB Output is correct
12 Correct 78 ms 57860 KB Output is correct
13 Correct 80 ms 58568 KB Output is correct
14 Correct 71 ms 61124 KB Output is correct
15 Correct 70 ms 55296 KB Output is correct
16 Correct 136 ms 62292 KB Output is correct
17 Correct 131 ms 61812 KB Output is correct
18 Correct 137 ms 62780 KB Output is correct
19 Correct 206 ms 65124 KB Output is correct
20 Correct 168 ms 66920 KB Output is correct
21 Correct 161 ms 67884 KB Output is correct
22 Correct 207 ms 67688 KB Output is correct
23 Correct 179 ms 67656 KB Output is correct
24 Correct 176 ms 67816 KB Output is correct
25 Correct 177 ms 67056 KB Output is correct
26 Correct 172 ms 67652 KB Output is correct
27 Correct 168 ms 66944 KB Output is correct
28 Correct 18 ms 42208 KB Output is correct
29 Correct 14 ms 42332 KB Output is correct
30 Correct 16 ms 42332 KB Output is correct
31 Correct 14 ms 42072 KB Output is correct
32 Correct 15 ms 42076 KB Output is correct
33 Correct 14 ms 42232 KB Output is correct
34 Correct 21 ms 42292 KB Output is correct
35 Correct 14 ms 41272 KB Output is correct
36 Correct 14 ms 41348 KB Output is correct
37 Correct 21 ms 43284 KB Output is correct
38 Correct 126 ms 57540 KB Output is correct
39 Correct 96 ms 58232 KB Output is correct
40 Correct 101 ms 58308 KB Output is correct
41 Correct 83 ms 57568 KB Output is correct
42 Correct 84 ms 57488 KB Output is correct
43 Correct 83 ms 56008 KB Output is correct
44 Correct 86 ms 57672 KB Output is correct
45 Correct 82 ms 58312 KB Output is correct
46 Correct 71 ms 61480 KB Output is correct
47 Correct 82 ms 58820 KB Output is correct
48 Correct 28 ms 44564 KB Output is correct
49 Correct 132 ms 63228 KB Output is correct
50 Correct 151 ms 63272 KB Output is correct
51 Correct 132 ms 63272 KB Output is correct
52 Correct 142 ms 63280 KB Output is correct
53 Correct 131 ms 62168 KB Output is correct
54 Correct 114 ms 58808 KB Output is correct
55 Correct 132 ms 64216 KB Output is correct
56 Correct 133 ms 63280 KB Output is correct
57 Correct 166 ms 65580 KB Output is correct
58 Correct 146 ms 64300 KB Output is correct
59 Correct 67 ms 49680 KB Output is correct
60 Correct 132 ms 63232 KB Output is correct
61 Correct 130 ms 63108 KB Output is correct
62 Correct 147 ms 64216 KB Output is correct
63 Correct 207 ms 66528 KB Output is correct
64 Correct 15 ms 41308 KB Output is correct
65 Correct 15 ms 41308 KB Output is correct
66 Correct 14 ms 41308 KB Output is correct
67 Correct 14 ms 41480 KB Output is correct
68 Correct 14 ms 41148 KB Output is correct
69 Correct 15 ms 41264 KB Output is correct
70 Correct 15 ms 41308 KB Output is correct
71 Correct 15 ms 41272 KB Output is correct
72 Correct 14 ms 41204 KB Output is correct
73 Correct 16 ms 41308 KB Output is correct
74 Correct 105 ms 67100 KB Output is correct
75 Correct 80 ms 58348 KB Output is correct
76 Correct 106 ms 58468 KB Output is correct
77 Correct 86 ms 61120 KB Output is correct
78 Correct 82 ms 66492 KB Output is correct
79 Correct 68 ms 61036 KB Output is correct
80 Correct 96 ms 63768 KB Output is correct
81 Correct 116 ms 68412 KB Output is correct
82 Correct 131 ms 70084 KB Output is correct
83 Correct 138 ms 74236 KB Output is correct
84 Correct 68 ms 61124 KB Output is correct
85 Correct 132 ms 71616 KB Output is correct
86 Correct 64 ms 51656 KB Output is correct
87 Correct 132 ms 63276 KB Output is correct
88 Correct 175 ms 63276 KB Output is correct
89 Correct 182 ms 65256 KB Output is correct
90 Correct 155 ms 71788 KB Output is correct
91 Correct 176 ms 69956 KB Output is correct
92 Correct 194 ms 67876 KB Output is correct
93 Correct 185 ms 72820 KB Output is correct
94 Correct 232 ms 74656 KB Output is correct
95 Correct 221 ms 78272 KB Output is correct
96 Correct 166 ms 65836 KB Output is correct
97 Correct 205 ms 70640 KB Output is correct