Submission #333579

# Submission time Handle Problem Language Result Execution time Memory
333579 2020-12-07T05:19:34 Z alishahali1382 Swapping Cities (APIO20_swap) C++14
100 / 100
462 ms 42348 KB
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;

const int inf=1000000010;
const int mod=1000000007;
const int MAXN=300010, LOG=19;

int n, m;
int par[LOG][MAXN], h[MAXN];
int dsu[MAXN], root[MAXN], deg[MAXN];
bool ok[MAXN];
int W[MAXN], N;

int getpar(int x){ return (dsu[x]==x?x:dsu[x]=getpar(dsu[x]));}

void init(int nn, int mm, vector<int> U, vector<int> V, vector<int> WW){
	iota(dsu, dsu+MAXN, 0);
	iota(root, root+MAXN, 0);
	n=nn;
	m=mm;
	vector<pair<int, pii>> E(m);
	for (int i=0; i<m; i++) E[i]={WW[i], {U[i], V[i]}};
	sort(all(E));
	N=n;
	for (auto p:E){
		int u=p.second.first, v=p.second.second, w=p.first;
		int x=getpar(u), y=getpar(v);
//		debug2(u, v)
//		debug2(x, y)
		
		deg[u]++;
		deg[v]++;
		if (x==y){
			par[0][root[x]]=N;
			root[x]=N;
			ok[N]=1;
			W[N++]=w;
		}
		else{
			par[0][root[x]]=N;
			par[0][root[y]]=N;
			ok[N]=(ok[root[x]]|ok[root[y]]|(deg[u]==3)|(deg[v]==3));
			dsu[y]=x;
			root[x]=N;
			W[N++]=w;
		}
	}
//	for (int i=0; i<N; i++) cerr<<i<<" : "<<W[i]<<" "<<ok[i]<<" "<<par[0][i]<<"\n";
	
	par[0][N-1]=N-1;
	for (int j=1; j<LOG; j++) for (int i=0; i<N; i++) par[j][i]=par[j-1][par[j-1][i]];
	for (int i=N-2; ~i; i--) h[i]=h[par[0][i]]+1;
	
}

int Lca(int x, int y){
	if (h[x]<h[y]) swap(x, y);
	for (int i=0; i<LOG; i++) if ((h[x]-h[y])&(1<<i)) x=par[i][x];
	if (x==y) return x;
	for (int i=LOG-1; ~i; i--) if (par[i][x]!=par[i][y]) x=par[i][x], y=par[i][y];
	return par[0][x];
}

int getMinimumFuelCapacity(int X, int Y){
	if (!ok[N-1]) return -1;
	int V=Lca(X, Y);
	if (ok[V]) return W[V];
	for (int i=LOG-1; ~i; i--) if (!ok[par[i][V]]) V=par[i][V];
	return W[par[0][V]];
}
/*
5 6 
0 1 4
0 2 4
1 2 1
1 3 2
1 4 10
2 3 3
1
0 1


*/
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2796 KB Output is correct
2 Correct 2 ms 2796 KB Output is correct
3 Correct 2 ms 2796 KB Output is correct
4 Correct 2 ms 2924 KB Output is correct
5 Correct 3 ms 3052 KB Output is correct
6 Correct 3 ms 3052 KB Output is correct
7 Correct 3 ms 3052 KB Output is correct
8 Correct 3 ms 3052 KB Output is correct
9 Correct 50 ms 20204 KB Output is correct
10 Correct 61 ms 24044 KB Output is correct
11 Correct 60 ms 23660 KB Output is correct
12 Correct 64 ms 24940 KB Output is correct
13 Correct 65 ms 25700 KB Output is correct
14 Correct 56 ms 20460 KB Output is correct
15 Correct 126 ms 28140 KB Output is correct
16 Correct 134 ms 27500 KB Output is correct
17 Correct 146 ms 28780 KB Output is correct
18 Correct 134 ms 29548 KB Output is correct
19 Correct 109 ms 11308 KB Output is correct
20 Correct 295 ms 28400 KB Output is correct
21 Correct 315 ms 27872 KB Output is correct
22 Correct 311 ms 29244 KB Output is correct
23 Correct 456 ms 30012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2796 KB Output is correct
2 Correct 2 ms 2796 KB Output is correct
3 Correct 404 ms 27572 KB Output is correct
4 Correct 392 ms 28348 KB Output is correct
5 Correct 405 ms 28184 KB Output is correct
6 Correct 388 ms 28096 KB Output is correct
7 Correct 410 ms 28396 KB Output is correct
8 Correct 399 ms 27468 KB Output is correct
9 Correct 385 ms 28016 KB Output is correct
10 Correct 392 ms 27364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2924 KB Output is correct
2 Correct 2 ms 2796 KB Output is correct
3 Correct 2 ms 2796 KB Output is correct
4 Correct 2 ms 2796 KB Output is correct
5 Correct 2 ms 2924 KB Output is correct
6 Correct 3 ms 3052 KB Output is correct
7 Correct 3 ms 3052 KB Output is correct
8 Correct 3 ms 3052 KB Output is correct
9 Correct 3 ms 3052 KB Output is correct
10 Correct 3 ms 3052 KB Output is correct
11 Correct 3 ms 3052 KB Output is correct
12 Correct 3 ms 3052 KB Output is correct
13 Correct 2 ms 2924 KB Output is correct
14 Correct 3 ms 3052 KB Output is correct
15 Correct 3 ms 3052 KB Output is correct
16 Correct 3 ms 3052 KB Output is correct
17 Correct 3 ms 3052 KB Output is correct
18 Correct 3 ms 3052 KB Output is correct
19 Correct 3 ms 3052 KB Output is correct
20 Correct 3 ms 3052 KB Output is correct
21 Correct 3 ms 3052 KB Output is correct
22 Correct 3 ms 3052 KB Output is correct
23 Correct 3 ms 2924 KB Output is correct
24 Correct 3 ms 3180 KB Output is correct
25 Correct 3 ms 3180 KB Output is correct
26 Correct 4 ms 3180 KB Output is correct
27 Correct 3 ms 3052 KB Output is correct
28 Correct 3 ms 3180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2924 KB Output is correct
2 Correct 2 ms 2796 KB Output is correct
3 Correct 2 ms 2796 KB Output is correct
4 Correct 2 ms 2796 KB Output is correct
5 Correct 2 ms 2924 KB Output is correct
6 Correct 3 ms 3052 KB Output is correct
7 Correct 3 ms 3052 KB Output is correct
8 Correct 3 ms 3052 KB Output is correct
9 Correct 3 ms 3052 KB Output is correct
10 Correct 50 ms 20204 KB Output is correct
11 Correct 61 ms 24044 KB Output is correct
12 Correct 60 ms 23660 KB Output is correct
13 Correct 64 ms 24940 KB Output is correct
14 Correct 65 ms 25700 KB Output is correct
15 Correct 3 ms 3052 KB Output is correct
16 Correct 3 ms 3052 KB Output is correct
17 Correct 3 ms 3052 KB Output is correct
18 Correct 2 ms 2924 KB Output is correct
19 Correct 3 ms 3052 KB Output is correct
20 Correct 3 ms 3052 KB Output is correct
21 Correct 3 ms 3052 KB Output is correct
22 Correct 3 ms 3052 KB Output is correct
23 Correct 3 ms 3052 KB Output is correct
24 Correct 10 ms 5748 KB Output is correct
25 Correct 62 ms 24684 KB Output is correct
26 Correct 60 ms 24684 KB Output is correct
27 Correct 61 ms 24684 KB Output is correct
28 Correct 60 ms 24300 KB Output is correct
29 Correct 61 ms 24172 KB Output is correct
30 Correct 55 ms 22380 KB Output is correct
31 Correct 64 ms 24960 KB Output is correct
32 Correct 61 ms 24684 KB Output is correct
33 Correct 63 ms 24812 KB Output is correct
34 Correct 66 ms 25068 KB Output is correct
35 Correct 3 ms 3052 KB Output is correct
36 Correct 3 ms 3052 KB Output is correct
37 Correct 3 ms 3052 KB Output is correct
38 Correct 3 ms 3052 KB Output is correct
39 Correct 3 ms 2924 KB Output is correct
40 Correct 3 ms 3180 KB Output is correct
41 Correct 3 ms 3180 KB Output is correct
42 Correct 4 ms 3180 KB Output is correct
43 Correct 3 ms 3052 KB Output is correct
44 Correct 3 ms 3180 KB Output is correct
45 Correct 98 ms 29676 KB Output is correct
46 Correct 61 ms 24684 KB Output is correct
47 Correct 61 ms 24684 KB Output is correct
48 Correct 71 ms 25964 KB Output is correct
49 Correct 79 ms 24300 KB Output is correct
50 Correct 75 ms 19948 KB Output is correct
51 Correct 83 ms 25836 KB Output is correct
52 Correct 100 ms 33516 KB Output is correct
53 Correct 128 ms 35948 KB Output is correct
54 Correct 146 ms 38508 KB Output is correct
55 Correct 61 ms 24684 KB Output is correct
56 Correct 115 ms 34924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2796 KB Output is correct
2 Correct 2 ms 2796 KB Output is correct
3 Correct 2 ms 2796 KB Output is correct
4 Correct 2 ms 2924 KB Output is correct
5 Correct 3 ms 3052 KB Output is correct
6 Correct 3 ms 3052 KB Output is correct
7 Correct 3 ms 3052 KB Output is correct
8 Correct 3 ms 3052 KB Output is correct
9 Correct 50 ms 20204 KB Output is correct
10 Correct 61 ms 24044 KB Output is correct
11 Correct 60 ms 23660 KB Output is correct
12 Correct 64 ms 24940 KB Output is correct
13 Correct 65 ms 25700 KB Output is correct
14 Correct 56 ms 20460 KB Output is correct
15 Correct 126 ms 28140 KB Output is correct
16 Correct 134 ms 27500 KB Output is correct
17 Correct 146 ms 28780 KB Output is correct
18 Correct 134 ms 29548 KB Output is correct
19 Correct 404 ms 27572 KB Output is correct
20 Correct 392 ms 28348 KB Output is correct
21 Correct 405 ms 28184 KB Output is correct
22 Correct 388 ms 28096 KB Output is correct
23 Correct 410 ms 28396 KB Output is correct
24 Correct 399 ms 27468 KB Output is correct
25 Correct 385 ms 28016 KB Output is correct
26 Correct 392 ms 27364 KB Output is correct
27 Correct 3 ms 3052 KB Output is correct
28 Correct 3 ms 3052 KB Output is correct
29 Correct 3 ms 3052 KB Output is correct
30 Correct 2 ms 2924 KB Output is correct
31 Correct 3 ms 3052 KB Output is correct
32 Correct 3 ms 3052 KB Output is correct
33 Correct 3 ms 3052 KB Output is correct
34 Correct 3 ms 3052 KB Output is correct
35 Correct 3 ms 3052 KB Output is correct
36 Correct 10 ms 5748 KB Output is correct
37 Correct 62 ms 24684 KB Output is correct
38 Correct 60 ms 24684 KB Output is correct
39 Correct 61 ms 24684 KB Output is correct
40 Correct 60 ms 24300 KB Output is correct
41 Correct 61 ms 24172 KB Output is correct
42 Correct 55 ms 22380 KB Output is correct
43 Correct 64 ms 24960 KB Output is correct
44 Correct 61 ms 24684 KB Output is correct
45 Correct 63 ms 24812 KB Output is correct
46 Correct 66 ms 25068 KB Output is correct
47 Correct 21 ms 5976 KB Output is correct
48 Correct 291 ms 28524 KB Output is correct
49 Correct 277 ms 28508 KB Output is correct
50 Correct 284 ms 28524 KB Output is correct
51 Correct 289 ms 28396 KB Output is correct
52 Correct 283 ms 27216 KB Output is correct
53 Correct 242 ms 22492 KB Output is correct
54 Correct 299 ms 29244 KB Output is correct
55 Correct 295 ms 28524 KB Output is correct
56 Correct 409 ms 28652 KB Output is correct
57 Correct 301 ms 29244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2924 KB Output is correct
2 Correct 2 ms 2796 KB Output is correct
3 Correct 2 ms 2796 KB Output is correct
4 Correct 2 ms 2796 KB Output is correct
5 Correct 2 ms 2924 KB Output is correct
6 Correct 3 ms 3052 KB Output is correct
7 Correct 3 ms 3052 KB Output is correct
8 Correct 3 ms 3052 KB Output is correct
9 Correct 3 ms 3052 KB Output is correct
10 Correct 50 ms 20204 KB Output is correct
11 Correct 61 ms 24044 KB Output is correct
12 Correct 60 ms 23660 KB Output is correct
13 Correct 64 ms 24940 KB Output is correct
14 Correct 65 ms 25700 KB Output is correct
15 Correct 56 ms 20460 KB Output is correct
16 Correct 126 ms 28140 KB Output is correct
17 Correct 134 ms 27500 KB Output is correct
18 Correct 146 ms 28780 KB Output is correct
19 Correct 134 ms 29548 KB Output is correct
20 Correct 404 ms 27572 KB Output is correct
21 Correct 392 ms 28348 KB Output is correct
22 Correct 405 ms 28184 KB Output is correct
23 Correct 388 ms 28096 KB Output is correct
24 Correct 410 ms 28396 KB Output is correct
25 Correct 399 ms 27468 KB Output is correct
26 Correct 385 ms 28016 KB Output is correct
27 Correct 392 ms 27364 KB Output is correct
28 Correct 3 ms 3052 KB Output is correct
29 Correct 3 ms 3052 KB Output is correct
30 Correct 3 ms 3052 KB Output is correct
31 Correct 2 ms 2924 KB Output is correct
32 Correct 3 ms 3052 KB Output is correct
33 Correct 3 ms 3052 KB Output is correct
34 Correct 3 ms 3052 KB Output is correct
35 Correct 3 ms 3052 KB Output is correct
36 Correct 3 ms 3052 KB Output is correct
37 Correct 10 ms 5748 KB Output is correct
38 Correct 62 ms 24684 KB Output is correct
39 Correct 60 ms 24684 KB Output is correct
40 Correct 61 ms 24684 KB Output is correct
41 Correct 60 ms 24300 KB Output is correct
42 Correct 61 ms 24172 KB Output is correct
43 Correct 55 ms 22380 KB Output is correct
44 Correct 64 ms 24960 KB Output is correct
45 Correct 61 ms 24684 KB Output is correct
46 Correct 63 ms 24812 KB Output is correct
47 Correct 66 ms 25068 KB Output is correct
48 Correct 21 ms 5976 KB Output is correct
49 Correct 291 ms 28524 KB Output is correct
50 Correct 277 ms 28508 KB Output is correct
51 Correct 284 ms 28524 KB Output is correct
52 Correct 289 ms 28396 KB Output is correct
53 Correct 283 ms 27216 KB Output is correct
54 Correct 242 ms 22492 KB Output is correct
55 Correct 299 ms 29244 KB Output is correct
56 Correct 295 ms 28524 KB Output is correct
57 Correct 409 ms 28652 KB Output is correct
58 Correct 301 ms 29244 KB Output is correct
59 Correct 109 ms 11308 KB Output is correct
60 Correct 295 ms 28400 KB Output is correct
61 Correct 315 ms 27872 KB Output is correct
62 Correct 311 ms 29244 KB Output is correct
63 Correct 456 ms 30012 KB Output is correct
64 Correct 3 ms 3052 KB Output is correct
65 Correct 3 ms 3052 KB Output is correct
66 Correct 3 ms 3052 KB Output is correct
67 Correct 3 ms 3052 KB Output is correct
68 Correct 3 ms 2924 KB Output is correct
69 Correct 3 ms 3180 KB Output is correct
70 Correct 3 ms 3180 KB Output is correct
71 Correct 4 ms 3180 KB Output is correct
72 Correct 3 ms 3052 KB Output is correct
73 Correct 3 ms 3180 KB Output is correct
74 Correct 98 ms 29676 KB Output is correct
75 Correct 61 ms 24684 KB Output is correct
76 Correct 61 ms 24684 KB Output is correct
77 Correct 71 ms 25964 KB Output is correct
78 Correct 79 ms 24300 KB Output is correct
79 Correct 75 ms 19948 KB Output is correct
80 Correct 83 ms 25836 KB Output is correct
81 Correct 100 ms 33516 KB Output is correct
82 Correct 128 ms 35948 KB Output is correct
83 Correct 146 ms 38508 KB Output is correct
84 Correct 61 ms 24684 KB Output is correct
85 Correct 115 ms 34924 KB Output is correct
86 Correct 81 ms 13292 KB Output is correct
87 Correct 276 ms 28524 KB Output is correct
88 Correct 272 ms 28524 KB Output is correct
89 Correct 389 ms 28652 KB Output is correct
90 Correct 193 ms 29036 KB Output is correct
91 Correct 215 ms 27756 KB Output is correct
92 Correct 370 ms 29100 KB Output is correct
93 Correct 341 ms 36972 KB Output is correct
94 Correct 462 ms 39560 KB Output is correct
95 Correct 363 ms 42348 KB Output is correct
96 Correct 409 ms 28524 KB Output is correct
97 Correct 403 ms 33772 KB Output is correct