Submission #347075

# Submission time Handle Problem Language Result Execution time Memory
347075 2021-01-11T16:53:30 Z patrikpavic2 Swapping Cities (APIO20_swap) C++17
100 / 100
374 ms 17244 KB
#include "swap.h"

#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>

#define X first
#define Y second
#define PB push_back

using namespace std;

typedef vector < int > vi;
typedef pair < int, int > pii;

const int N = 2e5 + 500;
const int INF = 0x3f3f3f3f;
const int LOG = 18;

int par[N], kad[N], postao[N], siz[N], deg[N];
vector < pair < int, pii > > edg;

int pr(int x, int tren){
	if(par[x] == x || kad[x] > tren) 
		return x;
	return pr(par[x], tren);
}

void mrg(int x, int y, int z){
	if(pr(x, z) == pr(y, z)){
		x = pr(x, z);
		postao[x] = min(postao[x], z);
		return;
	}
	deg[x]++; deg[y]++;
	bool dobar = (deg[x] >= 3 || deg[y] >= 3);
	x = pr(x, z), y = pr(y, z);
	if(siz[x] < siz[y])
		{x ^= y, y ^= x, x ^= y;}
	par[y] = x; kad[y] = z;
	siz[x] += siz[y];
	postao[x] = min(postao[x], max(postao[y], z));
	if(dobar)
		postao[x] = min(postao[x], z);
}


void init(int n, int m, vi U, vi V, vi W) {
	for(int i = 0;i < m;i++)
		edg.PB({W[i], {U[i], V[i]}});
	for(int i = 0;i < n;i++)
		par[i] = i, siz[i] = 1, postao[i] = INF;
	sort(edg.begin(), edg.end());
	for(pair < int, pii > cur : edg){
		mrg(cur.Y.X, cur.Y.Y, cur.X);
	}
}

bool probaj(int x, int y, int z){
	x = pr(x, z); y = pr(y, z);
	if(x != y)
		return 0;
	return (z >= postao[x]);
}


int getMinimumFuelCapacity(int x, int y){
	int ret = -1;
	for(int i = 30;i >= 0;i--)
		if(!probaj(x, y, ret + (1 << i)))
			ret += (1 << i);
	if(ret + 1 == INF)
		return -1;
	return ret + 1;
}













# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 41 ms 6368 KB Output is correct
10 Correct 51 ms 7776 KB Output is correct
11 Correct 49 ms 7648 KB Output is correct
12 Correct 51 ms 8032 KB Output is correct
13 Correct 55 ms 8032 KB Output is correct
14 Correct 58 ms 6752 KB Output is correct
15 Correct 246 ms 12140 KB Output is correct
16 Correct 239 ms 11792 KB Output is correct
17 Correct 242 ms 12104 KB Output is correct
18 Correct 159 ms 12104 KB Output is correct
19 Correct 161 ms 6832 KB Output is correct
20 Correct 268 ms 12768 KB Output is correct
21 Correct 285 ms 13212 KB Output is correct
22 Correct 293 ms 13640 KB Output is correct
23 Correct 168 ms 13512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 175 ms 12548 KB Output is correct
4 Correct 176 ms 12488 KB Output is correct
5 Correct 181 ms 12920 KB Output is correct
6 Correct 174 ms 12360 KB Output is correct
7 Correct 182 ms 12768 KB Output is correct
8 Correct 174 ms 12432 KB Output is correct
9 Correct 174 ms 12512 KB Output is correct
10 Correct 195 ms 12444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 492 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 2 ms 492 KB Output is correct
25 Correct 1 ms 492 KB Output is correct
26 Correct 1 ms 492 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 41 ms 6368 KB Output is correct
11 Correct 51 ms 7776 KB Output is correct
12 Correct 49 ms 7648 KB Output is correct
13 Correct 51 ms 8032 KB Output is correct
14 Correct 55 ms 8032 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 7 ms 1452 KB Output is correct
25 Correct 63 ms 7776 KB Output is correct
26 Correct 53 ms 7776 KB Output is correct
27 Correct 59 ms 7776 KB Output is correct
28 Correct 48 ms 7648 KB Output is correct
29 Correct 48 ms 7648 KB Output is correct
30 Correct 49 ms 7152 KB Output is correct
31 Correct 51 ms 8032 KB Output is correct
32 Correct 50 ms 7904 KB Output is correct
33 Correct 53 ms 7776 KB Output is correct
34 Correct 51 ms 8052 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 1 ms 492 KB Output is correct
37 Correct 1 ms 364 KB Output is correct
38 Correct 1 ms 492 KB Output is correct
39 Correct 1 ms 364 KB Output is correct
40 Correct 2 ms 492 KB Output is correct
41 Correct 1 ms 492 KB Output is correct
42 Correct 1 ms 492 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 1 ms 492 KB Output is correct
45 Correct 77 ms 10332 KB Output is correct
46 Correct 50 ms 7904 KB Output is correct
47 Correct 49 ms 7904 KB Output is correct
48 Correct 55 ms 8316 KB Output is correct
49 Correct 75 ms 10080 KB Output is correct
50 Correct 59 ms 7140 KB Output is correct
51 Correct 68 ms 8928 KB Output is correct
52 Correct 87 ms 11612 KB Output is correct
53 Correct 92 ms 12508 KB Output is correct
54 Correct 108 ms 13404 KB Output is correct
55 Correct 47 ms 7776 KB Output is correct
56 Correct 90 ms 12124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 41 ms 6368 KB Output is correct
10 Correct 51 ms 7776 KB Output is correct
11 Correct 49 ms 7648 KB Output is correct
12 Correct 51 ms 8032 KB Output is correct
13 Correct 55 ms 8032 KB Output is correct
14 Correct 58 ms 6752 KB Output is correct
15 Correct 246 ms 12140 KB Output is correct
16 Correct 239 ms 11792 KB Output is correct
17 Correct 242 ms 12104 KB Output is correct
18 Correct 159 ms 12104 KB Output is correct
19 Correct 175 ms 12548 KB Output is correct
20 Correct 176 ms 12488 KB Output is correct
21 Correct 181 ms 12920 KB Output is correct
22 Correct 174 ms 12360 KB Output is correct
23 Correct 182 ms 12768 KB Output is correct
24 Correct 174 ms 12432 KB Output is correct
25 Correct 174 ms 12512 KB Output is correct
26 Correct 195 ms 12444 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 7 ms 1452 KB Output is correct
37 Correct 63 ms 7776 KB Output is correct
38 Correct 53 ms 7776 KB Output is correct
39 Correct 59 ms 7776 KB Output is correct
40 Correct 48 ms 7648 KB Output is correct
41 Correct 48 ms 7648 KB Output is correct
42 Correct 49 ms 7152 KB Output is correct
43 Correct 51 ms 8032 KB Output is correct
44 Correct 50 ms 7904 KB Output is correct
45 Correct 53 ms 7776 KB Output is correct
46 Correct 51 ms 8052 KB Output is correct
47 Correct 29 ms 1964 KB Output is correct
48 Correct 288 ms 12616 KB Output is correct
49 Correct 272 ms 12616 KB Output is correct
50 Correct 270 ms 12616 KB Output is correct
51 Correct 281 ms 12628 KB Output is correct
52 Correct 258 ms 12304 KB Output is correct
53 Correct 278 ms 11476 KB Output is correct
54 Correct 295 ms 13640 KB Output is correct
55 Correct 297 ms 12596 KB Output is correct
56 Correct 182 ms 12616 KB Output is correct
57 Correct 342 ms 13680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 41 ms 6368 KB Output is correct
11 Correct 51 ms 7776 KB Output is correct
12 Correct 49 ms 7648 KB Output is correct
13 Correct 51 ms 8032 KB Output is correct
14 Correct 55 ms 8032 KB Output is correct
15 Correct 58 ms 6752 KB Output is correct
16 Correct 246 ms 12140 KB Output is correct
17 Correct 239 ms 11792 KB Output is correct
18 Correct 242 ms 12104 KB Output is correct
19 Correct 159 ms 12104 KB Output is correct
20 Correct 175 ms 12548 KB Output is correct
21 Correct 176 ms 12488 KB Output is correct
22 Correct 181 ms 12920 KB Output is correct
23 Correct 174 ms 12360 KB Output is correct
24 Correct 182 ms 12768 KB Output is correct
25 Correct 174 ms 12432 KB Output is correct
26 Correct 174 ms 12512 KB Output is correct
27 Correct 195 ms 12444 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 1 ms 364 KB Output is correct
37 Correct 7 ms 1452 KB Output is correct
38 Correct 63 ms 7776 KB Output is correct
39 Correct 53 ms 7776 KB Output is correct
40 Correct 59 ms 7776 KB Output is correct
41 Correct 48 ms 7648 KB Output is correct
42 Correct 48 ms 7648 KB Output is correct
43 Correct 49 ms 7152 KB Output is correct
44 Correct 51 ms 8032 KB Output is correct
45 Correct 50 ms 7904 KB Output is correct
46 Correct 53 ms 7776 KB Output is correct
47 Correct 51 ms 8052 KB Output is correct
48 Correct 29 ms 1964 KB Output is correct
49 Correct 288 ms 12616 KB Output is correct
50 Correct 272 ms 12616 KB Output is correct
51 Correct 270 ms 12616 KB Output is correct
52 Correct 281 ms 12628 KB Output is correct
53 Correct 258 ms 12304 KB Output is correct
54 Correct 278 ms 11476 KB Output is correct
55 Correct 295 ms 13640 KB Output is correct
56 Correct 297 ms 12596 KB Output is correct
57 Correct 182 ms 12616 KB Output is correct
58 Correct 342 ms 13680 KB Output is correct
59 Correct 161 ms 6832 KB Output is correct
60 Correct 268 ms 12768 KB Output is correct
61 Correct 285 ms 13212 KB Output is correct
62 Correct 293 ms 13640 KB Output is correct
63 Correct 168 ms 13512 KB Output is correct
64 Correct 1 ms 364 KB Output is correct
65 Correct 1 ms 492 KB Output is correct
66 Correct 1 ms 364 KB Output is correct
67 Correct 1 ms 492 KB Output is correct
68 Correct 1 ms 364 KB Output is correct
69 Correct 2 ms 492 KB Output is correct
70 Correct 1 ms 492 KB Output is correct
71 Correct 1 ms 492 KB Output is correct
72 Correct 1 ms 364 KB Output is correct
73 Correct 1 ms 492 KB Output is correct
74 Correct 77 ms 10332 KB Output is correct
75 Correct 50 ms 7904 KB Output is correct
76 Correct 49 ms 7904 KB Output is correct
77 Correct 55 ms 8316 KB Output is correct
78 Correct 75 ms 10080 KB Output is correct
79 Correct 59 ms 7140 KB Output is correct
80 Correct 68 ms 8928 KB Output is correct
81 Correct 87 ms 11612 KB Output is correct
82 Correct 92 ms 12508 KB Output is correct
83 Correct 108 ms 13404 KB Output is correct
84 Correct 47 ms 7776 KB Output is correct
85 Correct 90 ms 12124 KB Output is correct
86 Correct 68 ms 5036 KB Output is correct
87 Correct 275 ms 12656 KB Output is correct
88 Correct 285 ms 12764 KB Output is correct
89 Correct 213 ms 12804 KB Output is correct
90 Correct 233 ms 13832 KB Output is correct
91 Correct 234 ms 13500 KB Output is correct
92 Correct 241 ms 13800 KB Output is correct
93 Correct 323 ms 16092 KB Output is correct
94 Correct 284 ms 16828 KB Output is correct
95 Correct 374 ms 17244 KB Output is correct
96 Correct 179 ms 12764 KB Output is correct
97 Correct 256 ms 15020 KB Output is correct