Submission #338618

# Submission time Handle Problem Language Result Execution time Memory
338618 2020-12-23T13:54:37 Z nandonathaniel Swapping Cities (APIO20_swap) C++14
100 / 100
502 ms 60892 KB
#include "swap.h"
#include "bits/stdc++.h"
using namespace std;
#define fi first
#define se second
const int MAXN=100005,MAXM=200005,LOG=19;
typedef pair<int,int> pii;
typedef pair<int,pii> piii;
int par[MAXN+MAXM],deg[MAXN],cost[MAXN+MAXM],N,depth[MAXN+MAXM],pa[LOG][MAXN+MAXM];
bool bisaswap[MAXN+MAXM];
vector<piii> edges;
vector<int> adj[MAXN+MAXM];

int find(int x){
	if(par[x]==x)return x;
	return par[x]=find(par[x]);
}

int LCA(int u,int v){
	if(depth[u]<depth[v])swap(u,v);
	int diff=depth[u]-depth[v];
	for(int i=LOG-1;i>=0;i--){
		if(diff & (1<<i))u=pa[i][u];
	}
	if(u==v)return u;
	for(int i=LOG-1;i>=0;i--){
		if(pa[i][u]!=pa[i][v]){
			u=pa[i][u];
			v=pa[i][v];
		}
	}
	return pa[0][u];
}

void dfs(int now){
	for(auto nxt : adj[now]){
		pa[0][nxt]=now;
		for(int i=1;i<LOG;i++)pa[i][nxt]=pa[i-1][pa[i-1][nxt]];
		depth[nxt]=depth[now]+1;
		dfs(nxt);
	}
}

void init(int n, int m,
          vector<int> U, vector<int> V, vector<int> W) {
    N=n;
	for(int i=0;i<m;i++){
		U[i]++;V[i]++;
		edges.push_back({W[i],{U[i],V[i]}});
	}
	for(int i=1;i<=N;i++){
		par[i]=i;
		deg[i]=0;
		bisaswap[i]=false;
	}
	sort(edges.begin(),edges.end());
	for(auto isi : edges){
		int ortua=find(isi.se.fi),ortub=find(isi.se.se);
		deg[isi.se.fi]++;
		deg[isi.se.se]++;
		N++;
		par[N]=N;
		par[ortua]=N;
		par[ortub]=N;
		if(ortua==ortub){
			bisaswap[N]=true;
		}
		else if(bisaswap[ortua] || bisaswap[ortub] || deg[isi.se.fi]>=3 || deg[isi.se.se]>=3){
			bisaswap[N]=true;
		}
		cost[N]=isi.first;
		adj[N].push_back(ortua);
		if(ortua!=ortub)adj[N].push_back(ortub);
	}
	dfs(N);
}

int getMinimumFuelCapacity(int X, int Y) {
	if(!bisaswap[N])return -1;
	X++;Y++;
	int anc=LCA(X,Y);
	if(bisaswap[anc])return cost[anc];
	for(int i=LOG-1;i>=0;i--){
		if((1<<i)>depth[anc])continue;
		if(!bisaswap[pa[i][anc]]){
			anc=pa[i][anc];
		}
	}
	return cost[pa[0][anc]];
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7532 KB Output is correct
2 Correct 5 ms 7532 KB Output is correct
3 Correct 5 ms 7532 KB Output is correct
4 Correct 6 ms 7660 KB Output is correct
5 Correct 6 ms 7788 KB Output is correct
6 Correct 6 ms 7788 KB Output is correct
7 Correct 6 ms 7788 KB Output is correct
8 Correct 6 ms 7788 KB Output is correct
9 Correct 114 ms 28000 KB Output is correct
10 Correct 168 ms 32480 KB Output is correct
11 Correct 155 ms 32104 KB Output is correct
12 Correct 146 ms 33504 KB Output is correct
13 Correct 144 ms 36704 KB Output is correct
14 Correct 118 ms 28384 KB Output is correct
15 Correct 210 ms 37008 KB Output is correct
16 Correct 203 ms 36112 KB Output is correct
17 Correct 243 ms 37832 KB Output is correct
18 Correct 184 ms 40776 KB Output is correct
19 Correct 107 ms 16812 KB Output is correct
20 Correct 316 ms 37984 KB Output is correct
21 Correct 352 ms 37424 KB Output is correct
22 Correct 339 ms 39112 KB Output is correct
23 Correct 468 ms 42312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7532 KB Output is correct
2 Correct 5 ms 7532 KB Output is correct
3 Correct 363 ms 43140 KB Output is correct
4 Correct 381 ms 44488 KB Output is correct
5 Correct 371 ms 43768 KB Output is correct
6 Correct 380 ms 44232 KB Output is correct
7 Correct 370 ms 44256 KB Output is correct
8 Correct 360 ms 42896 KB Output is correct
9 Correct 359 ms 44028 KB Output is correct
10 Correct 390 ms 42652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7532 KB Output is correct
2 Correct 5 ms 7532 KB Output is correct
3 Correct 5 ms 7532 KB Output is correct
4 Correct 5 ms 7532 KB Output is correct
5 Correct 6 ms 7660 KB Output is correct
6 Correct 6 ms 7788 KB Output is correct
7 Correct 6 ms 7788 KB Output is correct
8 Correct 6 ms 7788 KB Output is correct
9 Correct 6 ms 7788 KB Output is correct
10 Correct 5 ms 7788 KB Output is correct
11 Correct 6 ms 7788 KB Output is correct
12 Correct 6 ms 7788 KB Output is correct
13 Correct 7 ms 7660 KB Output is correct
14 Correct 6 ms 7660 KB Output is correct
15 Correct 6 ms 7788 KB Output is correct
16 Correct 6 ms 7788 KB Output is correct
17 Correct 6 ms 7788 KB Output is correct
18 Correct 6 ms 7788 KB Output is correct
19 Correct 6 ms 7788 KB Output is correct
20 Correct 6 ms 7788 KB Output is correct
21 Correct 6 ms 7788 KB Output is correct
22 Correct 7 ms 7916 KB Output is correct
23 Correct 6 ms 7660 KB Output is correct
24 Correct 6 ms 7916 KB Output is correct
25 Correct 6 ms 7916 KB Output is correct
26 Correct 7 ms 8044 KB Output is correct
27 Correct 6 ms 7788 KB Output is correct
28 Correct 7 ms 7936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7532 KB Output is correct
2 Correct 5 ms 7532 KB Output is correct
3 Correct 5 ms 7532 KB Output is correct
4 Correct 5 ms 7532 KB Output is correct
5 Correct 6 ms 7660 KB Output is correct
6 Correct 6 ms 7788 KB Output is correct
7 Correct 6 ms 7788 KB Output is correct
8 Correct 6 ms 7788 KB Output is correct
9 Correct 6 ms 7788 KB Output is correct
10 Correct 114 ms 28000 KB Output is correct
11 Correct 168 ms 32480 KB Output is correct
12 Correct 155 ms 32104 KB Output is correct
13 Correct 146 ms 33504 KB Output is correct
14 Correct 144 ms 36704 KB Output is correct
15 Correct 5 ms 7788 KB Output is correct
16 Correct 6 ms 7788 KB Output is correct
17 Correct 6 ms 7788 KB Output is correct
18 Correct 7 ms 7660 KB Output is correct
19 Correct 6 ms 7660 KB Output is correct
20 Correct 6 ms 7788 KB Output is correct
21 Correct 6 ms 7788 KB Output is correct
22 Correct 6 ms 7788 KB Output is correct
23 Correct 6 ms 7788 KB Output is correct
24 Correct 18 ms 11052 KB Output is correct
25 Correct 161 ms 33372 KB Output is correct
26 Correct 140 ms 33504 KB Output is correct
27 Correct 140 ms 33376 KB Output is correct
28 Correct 144 ms 33012 KB Output is correct
29 Correct 137 ms 32864 KB Output is correct
30 Correct 130 ms 30816 KB Output is correct
31 Correct 143 ms 33504 KB Output is correct
32 Correct 141 ms 33248 KB Output is correct
33 Correct 117 ms 37440 KB Output is correct
34 Correct 147 ms 33760 KB Output is correct
35 Correct 6 ms 7788 KB Output is correct
36 Correct 6 ms 7788 KB Output is correct
37 Correct 6 ms 7788 KB Output is correct
38 Correct 7 ms 7916 KB Output is correct
39 Correct 6 ms 7660 KB Output is correct
40 Correct 6 ms 7916 KB Output is correct
41 Correct 6 ms 7916 KB Output is correct
42 Correct 7 ms 8044 KB Output is correct
43 Correct 6 ms 7788 KB Output is correct
44 Correct 7 ms 7936 KB Output is correct
45 Correct 178 ms 45788 KB Output is correct
46 Correct 142 ms 33384 KB Output is correct
47 Correct 138 ms 33780 KB Output is correct
48 Correct 146 ms 37472 KB Output is correct
49 Correct 131 ms 44392 KB Output is correct
50 Correct 99 ms 36192 KB Output is correct
51 Correct 145 ms 40800 KB Output is correct
52 Correct 197 ms 48092 KB Output is correct
53 Correct 226 ms 51036 KB Output is correct
54 Correct 237 ms 56920 KB Output is correct
55 Correct 125 ms 37216 KB Output is correct
56 Correct 224 ms 52720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7532 KB Output is correct
2 Correct 5 ms 7532 KB Output is correct
3 Correct 5 ms 7532 KB Output is correct
4 Correct 6 ms 7660 KB Output is correct
5 Correct 6 ms 7788 KB Output is correct
6 Correct 6 ms 7788 KB Output is correct
7 Correct 6 ms 7788 KB Output is correct
8 Correct 6 ms 7788 KB Output is correct
9 Correct 114 ms 28000 KB Output is correct
10 Correct 168 ms 32480 KB Output is correct
11 Correct 155 ms 32104 KB Output is correct
12 Correct 146 ms 33504 KB Output is correct
13 Correct 144 ms 36704 KB Output is correct
14 Correct 118 ms 28384 KB Output is correct
15 Correct 210 ms 37008 KB Output is correct
16 Correct 203 ms 36112 KB Output is correct
17 Correct 243 ms 37832 KB Output is correct
18 Correct 184 ms 40776 KB Output is correct
19 Correct 363 ms 43140 KB Output is correct
20 Correct 381 ms 44488 KB Output is correct
21 Correct 371 ms 43768 KB Output is correct
22 Correct 380 ms 44232 KB Output is correct
23 Correct 370 ms 44256 KB Output is correct
24 Correct 360 ms 42896 KB Output is correct
25 Correct 359 ms 44028 KB Output is correct
26 Correct 390 ms 42652 KB Output is correct
27 Correct 5 ms 7788 KB Output is correct
28 Correct 6 ms 7788 KB Output is correct
29 Correct 6 ms 7788 KB Output is correct
30 Correct 7 ms 7660 KB Output is correct
31 Correct 6 ms 7660 KB Output is correct
32 Correct 6 ms 7788 KB Output is correct
33 Correct 6 ms 7788 KB Output is correct
34 Correct 6 ms 7788 KB Output is correct
35 Correct 6 ms 7788 KB Output is correct
36 Correct 18 ms 11052 KB Output is correct
37 Correct 161 ms 33372 KB Output is correct
38 Correct 140 ms 33504 KB Output is correct
39 Correct 140 ms 33376 KB Output is correct
40 Correct 144 ms 33012 KB Output is correct
41 Correct 137 ms 32864 KB Output is correct
42 Correct 130 ms 30816 KB Output is correct
43 Correct 143 ms 33504 KB Output is correct
44 Correct 141 ms 33248 KB Output is correct
45 Correct 117 ms 37440 KB Output is correct
46 Correct 147 ms 33760 KB Output is correct
47 Correct 26 ms 11308 KB Output is correct
48 Correct 334 ms 38472 KB Output is correct
49 Correct 308 ms 38276 KB Output is correct
50 Correct 301 ms 38188 KB Output is correct
51 Correct 297 ms 38136 KB Output is correct
52 Correct 289 ms 36868 KB Output is correct
53 Correct 242 ms 30852 KB Output is correct
54 Correct 320 ms 38984 KB Output is correct
55 Correct 328 ms 38344 KB Output is correct
56 Correct 402 ms 41544 KB Output is correct
57 Correct 308 ms 39240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7532 KB Output is correct
2 Correct 5 ms 7532 KB Output is correct
3 Correct 5 ms 7532 KB Output is correct
4 Correct 5 ms 7532 KB Output is correct
5 Correct 6 ms 7660 KB Output is correct
6 Correct 6 ms 7788 KB Output is correct
7 Correct 6 ms 7788 KB Output is correct
8 Correct 6 ms 7788 KB Output is correct
9 Correct 6 ms 7788 KB Output is correct
10 Correct 114 ms 28000 KB Output is correct
11 Correct 168 ms 32480 KB Output is correct
12 Correct 155 ms 32104 KB Output is correct
13 Correct 146 ms 33504 KB Output is correct
14 Correct 144 ms 36704 KB Output is correct
15 Correct 118 ms 28384 KB Output is correct
16 Correct 210 ms 37008 KB Output is correct
17 Correct 203 ms 36112 KB Output is correct
18 Correct 243 ms 37832 KB Output is correct
19 Correct 184 ms 40776 KB Output is correct
20 Correct 363 ms 43140 KB Output is correct
21 Correct 381 ms 44488 KB Output is correct
22 Correct 371 ms 43768 KB Output is correct
23 Correct 380 ms 44232 KB Output is correct
24 Correct 370 ms 44256 KB Output is correct
25 Correct 360 ms 42896 KB Output is correct
26 Correct 359 ms 44028 KB Output is correct
27 Correct 390 ms 42652 KB Output is correct
28 Correct 5 ms 7788 KB Output is correct
29 Correct 6 ms 7788 KB Output is correct
30 Correct 6 ms 7788 KB Output is correct
31 Correct 7 ms 7660 KB Output is correct
32 Correct 6 ms 7660 KB Output is correct
33 Correct 6 ms 7788 KB Output is correct
34 Correct 6 ms 7788 KB Output is correct
35 Correct 6 ms 7788 KB Output is correct
36 Correct 6 ms 7788 KB Output is correct
37 Correct 18 ms 11052 KB Output is correct
38 Correct 161 ms 33372 KB Output is correct
39 Correct 140 ms 33504 KB Output is correct
40 Correct 140 ms 33376 KB Output is correct
41 Correct 144 ms 33012 KB Output is correct
42 Correct 137 ms 32864 KB Output is correct
43 Correct 130 ms 30816 KB Output is correct
44 Correct 143 ms 33504 KB Output is correct
45 Correct 141 ms 33248 KB Output is correct
46 Correct 117 ms 37440 KB Output is correct
47 Correct 147 ms 33760 KB Output is correct
48 Correct 26 ms 11308 KB Output is correct
49 Correct 334 ms 38472 KB Output is correct
50 Correct 308 ms 38276 KB Output is correct
51 Correct 301 ms 38188 KB Output is correct
52 Correct 297 ms 38136 KB Output is correct
53 Correct 289 ms 36868 KB Output is correct
54 Correct 242 ms 30852 KB Output is correct
55 Correct 320 ms 38984 KB Output is correct
56 Correct 328 ms 38344 KB Output is correct
57 Correct 402 ms 41544 KB Output is correct
58 Correct 308 ms 39240 KB Output is correct
59 Correct 107 ms 16812 KB Output is correct
60 Correct 316 ms 37984 KB Output is correct
61 Correct 352 ms 37424 KB Output is correct
62 Correct 339 ms 39112 KB Output is correct
63 Correct 468 ms 42312 KB Output is correct
64 Correct 6 ms 7788 KB Output is correct
65 Correct 6 ms 7788 KB Output is correct
66 Correct 6 ms 7788 KB Output is correct
67 Correct 7 ms 7916 KB Output is correct
68 Correct 6 ms 7660 KB Output is correct
69 Correct 6 ms 7916 KB Output is correct
70 Correct 6 ms 7916 KB Output is correct
71 Correct 7 ms 8044 KB Output is correct
72 Correct 6 ms 7788 KB Output is correct
73 Correct 7 ms 7936 KB Output is correct
74 Correct 178 ms 45788 KB Output is correct
75 Correct 142 ms 33384 KB Output is correct
76 Correct 138 ms 33780 KB Output is correct
77 Correct 146 ms 37472 KB Output is correct
78 Correct 131 ms 44392 KB Output is correct
79 Correct 99 ms 36192 KB Output is correct
80 Correct 145 ms 40800 KB Output is correct
81 Correct 197 ms 48092 KB Output is correct
82 Correct 226 ms 51036 KB Output is correct
83 Correct 237 ms 56920 KB Output is correct
84 Correct 125 ms 37216 KB Output is correct
85 Correct 224 ms 52720 KB Output is correct
86 Correct 94 ms 21756 KB Output is correct
87 Correct 288 ms 38344 KB Output is correct
88 Correct 298 ms 38216 KB Output is correct
89 Correct 360 ms 41088 KB Output is correct
90 Correct 243 ms 50612 KB Output is correct
91 Correct 238 ms 47484 KB Output is correct
92 Correct 391 ms 44476 KB Output is correct
93 Correct 359 ms 52296 KB Output is correct
94 Correct 502 ms 55540 KB Output is correct
95 Correct 433 ms 60892 KB Output is correct
96 Correct 411 ms 42056 KB Output is correct
97 Correct 434 ms 48792 KB Output is correct