Submission #396474

# Submission time Handle Problem Language Result Execution time Memory
396474 2021-04-30T02:46:20 Z Kevin_Zhang_TW Swapping Cities (APIO20_swap) C++17
100 / 100
556 ms 23108 KB
#include <bits/stdc++.h>
#include "swap.h"
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l)==r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif

const int MAX_N = 300010, inf = 1e9 + 7;

int ok_t[MAX_N], wei[MAX_N];

struct dsu {
	vector<int> sz, good, deg;
	vector<vector<pair<int,int>>> g;
	dsu(int n = 0) { 
		g.resize(n), sz.resize(n, 1), good.resize(n), deg.resize(n);
		for (int i = 0;i < n;++i)
			g[i] = {{-1, i}};
	}
	// I want things before time j
	int F(int i, int j) {
		int a = prev(upper_bound(AI(g[i]), make_pair(j+1, -1)))->second;
		return i == a ? i : F(a, j);
	}
	int F(int i) { return i == g[i].back().second ? i : F(g[i].back().second); }

	void M(int a, int b, int t) {
		bool ok = max(++deg[a], ++deg[b]) > 2;
		a = F(a), b = F(b);
		if (a == b) {
			chmin(ok_t[a], t);
			return good[a] = true, void();
		}
		if (sz[a] < sz[b]) swap(a, b);

		if (good[a] |= ok || good[b])
			chmin(ok_t[a], t);

		g[b].pb(t, a);
		sz[a] += sz[b];

	}
}D;

bool valid(int t, int a, int b) {
	a = D.F(a, t), b = D.F(b, t);
	if (a != b) return false;
	return ok_t[a] <= t;
}

int n, m;

void init(int N, int M,
          std::vector<int> U, std::vector<int> V, std::vector<int> W) {

	n = N, m = M;
	D = dsu(n);

	vector<tuple<int,int,int>> alle;
	for (int i = 0;i < m;++i) {
		alle.pb(W[i], U[i], V[i]);
	}
	sort(AI(alle));

	for (int i = 0;i < n;++i)
		ok_t[i] = inf;

	for (int i = 0;i < m;++i) {
		auto [w, a, b] = alle[i];
		wei[i] = w;

		D.M(a, b, i);
	}
}

int getMinimumFuelCapacity(int X, int Y) {
	int l = 0, r = m-1, mid;
	while (l < r) {
		mid = l + r >> 1;
		if (valid(mid, X, Y))
			r = mid;
		else
			l = mid + 1;
	}
	return valid(l, X, Y) ? wei[l] : -1;
}

Compilation message

swap.cpp: In function 'int getMinimumFuelCapacity(int, int)':
swap.cpp:90:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   90 |   mid = l + r >> 1;
      |         ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 58 ms 9020 KB Output is correct
10 Correct 75 ms 10900 KB Output is correct
11 Correct 71 ms 10756 KB Output is correct
12 Correct 80 ms 11324 KB Output is correct
13 Correct 75 ms 11372 KB Output is correct
14 Correct 100 ms 9168 KB Output is correct
15 Correct 422 ms 12584 KB Output is correct
16 Correct 422 ms 12272 KB Output is correct
17 Correct 465 ms 15932 KB Output is correct
18 Correct 231 ms 15932 KB Output is correct
19 Correct 208 ms 7364 KB Output is correct
20 Correct 483 ms 16964 KB Output is correct
21 Correct 420 ms 16944 KB Output is correct
22 Correct 458 ms 17692 KB Output is correct
23 Correct 228 ms 17676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 216 ms 12548 KB Output is correct
4 Correct 217 ms 16760 KB Output is correct
5 Correct 261 ms 16864 KB Output is correct
6 Correct 215 ms 16636 KB Output is correct
7 Correct 230 ms 16816 KB Output is correct
8 Correct 237 ms 16300 KB Output is correct
9 Correct 221 ms 16588 KB Output is correct
10 Correct 230 ms 16324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 2 ms 328 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 424 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 2 ms 460 KB Output is correct
25 Correct 2 ms 460 KB Output is correct
26 Correct 2 ms 460 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 2 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 58 ms 9020 KB Output is correct
11 Correct 75 ms 10900 KB Output is correct
12 Correct 71 ms 10756 KB Output is correct
13 Correct 80 ms 11324 KB Output is correct
14 Correct 75 ms 11372 KB Output is correct
15 Correct 2 ms 328 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 2 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 424 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 2 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 2 ms 460 KB Output is correct
30 Correct 2 ms 460 KB Output is correct
31 Correct 2 ms 460 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 2 ms 460 KB Output is correct
34 Correct 9 ms 1932 KB Output is correct
35 Correct 79 ms 11320 KB Output is correct
36 Correct 71 ms 11316 KB Output is correct
37 Correct 72 ms 11316 KB Output is correct
38 Correct 74 ms 11320 KB Output is correct
39 Correct 70 ms 11064 KB Output is correct
40 Correct 62 ms 10244 KB Output is correct
41 Correct 74 ms 11320 KB Output is correct
42 Correct 88 ms 11332 KB Output is correct
43 Correct 66 ms 11404 KB Output is correct
44 Correct 76 ms 11316 KB Output is correct
45 Correct 99 ms 12132 KB Output is correct
46 Correct 68 ms 11324 KB Output is correct
47 Correct 75 ms 11328 KB Output is correct
48 Correct 85 ms 11656 KB Output is correct
49 Correct 84 ms 7228 KB Output is correct
50 Correct 62 ms 5680 KB Output is correct
51 Correct 80 ms 9864 KB Output is correct
52 Correct 114 ms 13844 KB Output is correct
53 Correct 125 ms 14416 KB Output is correct
54 Correct 162 ms 15308 KB Output is correct
55 Correct 66 ms 11328 KB Output is correct
56 Correct 115 ms 14140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 58 ms 9020 KB Output is correct
10 Correct 75 ms 10900 KB Output is correct
11 Correct 71 ms 10756 KB Output is correct
12 Correct 80 ms 11324 KB Output is correct
13 Correct 75 ms 11372 KB Output is correct
14 Correct 100 ms 9168 KB Output is correct
15 Correct 422 ms 12584 KB Output is correct
16 Correct 422 ms 12272 KB Output is correct
17 Correct 465 ms 15932 KB Output is correct
18 Correct 231 ms 15932 KB Output is correct
19 Correct 216 ms 12548 KB Output is correct
20 Correct 217 ms 16760 KB Output is correct
21 Correct 261 ms 16864 KB Output is correct
22 Correct 215 ms 16636 KB Output is correct
23 Correct 230 ms 16816 KB Output is correct
24 Correct 237 ms 16300 KB Output is correct
25 Correct 221 ms 16588 KB Output is correct
26 Correct 230 ms 16324 KB Output is correct
27 Correct 2 ms 328 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 2 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 424 KB Output is correct
36 Correct 9 ms 1932 KB Output is correct
37 Correct 79 ms 11320 KB Output is correct
38 Correct 71 ms 11316 KB Output is correct
39 Correct 72 ms 11316 KB Output is correct
40 Correct 74 ms 11320 KB Output is correct
41 Correct 70 ms 11064 KB Output is correct
42 Correct 62 ms 10244 KB Output is correct
43 Correct 74 ms 11320 KB Output is correct
44 Correct 88 ms 11332 KB Output is correct
45 Correct 66 ms 11404 KB Output is correct
46 Correct 76 ms 11316 KB Output is correct
47 Correct 34 ms 2376 KB Output is correct
48 Correct 440 ms 17068 KB Output is correct
49 Correct 445 ms 17060 KB Output is correct
50 Correct 425 ms 16960 KB Output is correct
51 Correct 425 ms 16968 KB Output is correct
52 Correct 422 ms 16168 KB Output is correct
53 Correct 349 ms 13960 KB Output is correct
54 Correct 445 ms 17664 KB Output is correct
55 Correct 457 ms 17084 KB Output is correct
56 Correct 227 ms 17084 KB Output is correct
57 Correct 429 ms 17648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 58 ms 9020 KB Output is correct
11 Correct 75 ms 10900 KB Output is correct
12 Correct 71 ms 10756 KB Output is correct
13 Correct 80 ms 11324 KB Output is correct
14 Correct 75 ms 11372 KB Output is correct
15 Correct 100 ms 9168 KB Output is correct
16 Correct 422 ms 12584 KB Output is correct
17 Correct 422 ms 12272 KB Output is correct
18 Correct 465 ms 15932 KB Output is correct
19 Correct 231 ms 15932 KB Output is correct
20 Correct 216 ms 12548 KB Output is correct
21 Correct 217 ms 16760 KB Output is correct
22 Correct 261 ms 16864 KB Output is correct
23 Correct 215 ms 16636 KB Output is correct
24 Correct 230 ms 16816 KB Output is correct
25 Correct 237 ms 16300 KB Output is correct
26 Correct 221 ms 16588 KB Output is correct
27 Correct 230 ms 16324 KB Output is correct
28 Correct 2 ms 328 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 2 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 1 ms 424 KB Output is correct
37 Correct 9 ms 1932 KB Output is correct
38 Correct 79 ms 11320 KB Output is correct
39 Correct 71 ms 11316 KB Output is correct
40 Correct 72 ms 11316 KB Output is correct
41 Correct 74 ms 11320 KB Output is correct
42 Correct 70 ms 11064 KB Output is correct
43 Correct 62 ms 10244 KB Output is correct
44 Correct 74 ms 11320 KB Output is correct
45 Correct 88 ms 11332 KB Output is correct
46 Correct 66 ms 11404 KB Output is correct
47 Correct 76 ms 11316 KB Output is correct
48 Correct 34 ms 2376 KB Output is correct
49 Correct 440 ms 17068 KB Output is correct
50 Correct 445 ms 17060 KB Output is correct
51 Correct 425 ms 16960 KB Output is correct
52 Correct 425 ms 16968 KB Output is correct
53 Correct 422 ms 16168 KB Output is correct
54 Correct 349 ms 13960 KB Output is correct
55 Correct 445 ms 17664 KB Output is correct
56 Correct 457 ms 17084 KB Output is correct
57 Correct 227 ms 17084 KB Output is correct
58 Correct 429 ms 17648 KB Output is correct
59 Correct 208 ms 7364 KB Output is correct
60 Correct 483 ms 16964 KB Output is correct
61 Correct 420 ms 16944 KB Output is correct
62 Correct 458 ms 17692 KB Output is correct
63 Correct 228 ms 17676 KB Output is correct
64 Correct 1 ms 332 KB Output is correct
65 Correct 1 ms 332 KB Output is correct
66 Correct 1 ms 332 KB Output is correct
67 Correct 2 ms 332 KB Output is correct
68 Correct 1 ms 332 KB Output is correct
69 Correct 2 ms 460 KB Output is correct
70 Correct 2 ms 460 KB Output is correct
71 Correct 2 ms 460 KB Output is correct
72 Correct 1 ms 332 KB Output is correct
73 Correct 2 ms 460 KB Output is correct
74 Correct 99 ms 12132 KB Output is correct
75 Correct 68 ms 11324 KB Output is correct
76 Correct 75 ms 11328 KB Output is correct
77 Correct 85 ms 11656 KB Output is correct
78 Correct 84 ms 7228 KB Output is correct
79 Correct 62 ms 5680 KB Output is correct
80 Correct 80 ms 9864 KB Output is correct
81 Correct 114 ms 13844 KB Output is correct
82 Correct 125 ms 14416 KB Output is correct
83 Correct 162 ms 15308 KB Output is correct
84 Correct 66 ms 11328 KB Output is correct
85 Correct 115 ms 14140 KB Output is correct
86 Correct 105 ms 6516 KB Output is correct
87 Correct 423 ms 17064 KB Output is correct
88 Correct 450 ms 17080 KB Output is correct
89 Correct 298 ms 16756 KB Output is correct
90 Correct 279 ms 13876 KB Output is correct
91 Correct 289 ms 14088 KB Output is correct
92 Correct 299 ms 16060 KB Output is correct
93 Correct 487 ms 21108 KB Output is correct
94 Correct 409 ms 21940 KB Output is correct
95 Correct 556 ms 23108 KB Output is correct
96 Correct 233 ms 17084 KB Output is correct
97 Correct 373 ms 20076 KB Output is correct