Submission #982339

# Submission time Handle Problem Language Result Execution time Memory
982339 2024-05-14T07:01:58 Z vjudge1 Swapping Cities (APIO20_swap) C++17
100 / 100
376 ms 75276 KB
#include "swap.h"

#include <bits/stdc++.h>

namespace operators
{
	template <typename T1, typename T2>
	std::istream &operator>>(std::istream &in, std::pair<T1, T2> &x)
	{
		in >> x.first >> x.second;
		return in;
	}

	template <typename T1, typename T2>
	std::ostream &operator<<(std::ostream &out, std::pair<T1, T2> x)
	{
		out << x.first << " " << x.second;
		return out;
	}

	template <typename T1>
	std::istream &operator>>(std::istream &in, std::vector<T1> &x)
	{
		for (auto &i : x)
			in >> i;
		return in;
	}

	template <typename T1>
	std::ostream &operator<<(std::ostream &out, std::vector<T1> &x)
	{
		for (auto &i : x)
			out << i << " ";
		return out;
	}

	template <typename T1>
	std::ostream &operator<<(std::ostream &out, std::set<T1> &x)
	{
		for (auto &i : x)
			out << i << " ";
		return out;
	}
}

// name spaces
using namespace std;
using namespace operators;
// end of name spaces

// defines
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define f first
#define s second
#define uint unsigned int
#define all(vc) vc.begin(), vc.end()
// end of defines

const ll INF = (1e18) + 500;
const int BIG = (1e9) * 2 + 100;
const int MAXN = (1e5) + 5;
const int MOD7 = (1e9) + 7;
const int MOD9 = (1e9) + 9;
const uint MODFFT = 998244353;

vector<pair<int, pii>> edges;

struct DSU
{
	vector<int> p;

	void init(int n)
	{
		p.resize(n);
		for (int i = 0; i < n; ++i)
			p[i] = i;
	}

	int getRoot(int a)
	{
		return p[a] == a ? a : p[a] = getRoot(p[a]);
	}

} dsu;
int n, m;
vector<vector<int>> tree, jump;
vector<int> good, cost, tin, tout;
int timer = 0;

void dfs(int v, int pr, int curCost)
{
	tin[v] = ++timer;
	if (good[v])
		curCost = cost[v];
	cost[v] = curCost;

	jump[v][0] = pr;
	for (int i = 1; i < 20; ++i)
		jump[v][i] = jump[jump[v][i - 1]][i - 1];

	for (auto tov : tree[v])
		dfs(tov, v, curCost);
	tout[v] = ++timer;
}

bool isP(int pr, int v)
{
	return tin[pr] <= tin[v] and tout[v] <= tout[pr];
}

int lca(int v, int u)
{
	if (isP(v, u))
		return v;
	if (isP(u, v))
		return u;

	for (int i = 19; i >= 0; --i)
		if (!isP(jump[v][i], u))
			v = jump[v][i];

	return jump[v][0];
}

void init(int N, int M, std::vector<int> U, std::vector<int> V, std::vector<int> W)
{
	tree.resize(N + M);
	jump.resize(N + M, vector<int>(20));
	good.resize(N + M);
	cost.resize(N + M);
	tin.resize(N + M);
	tout.resize(N + M);

	dsu.init(N + M);

	n = N;
	m = M;

	for (int i = 0; i < M; ++i)
		edges.pb({W[i], {U[i], V[i]}});

	sort(all(edges));
	vector<int> deg(N, 0);
	int cnt = 0;
	for (auto [w, e] : edges)
	{
		auto [v, tov] = e;
		deg[v]++;
		deg[tov]++;

		bool isGood = (deg[v] >= 3 or deg[tov] >= 3);
		v = dsu.getRoot(v);
		tov = dsu.getRoot(tov);
		isGood |= (good[v] or good[tov]);

		tree[N + cnt].pb(v);
		dsu.p[v] = dsu.p[tov] = N + cnt;
		if (v != tov)
			tree[N + cnt].pb(tov);
		else
			isGood = true;
		cost[N + cnt] = w;
		good[N + cnt] = isGood;

		cnt++;
	}
	dfs(N + M - 1, N + M - 1, BIG);
}

int getMinimumFuelCapacity(int X, int Y)
{
	int cV = lca(X, Y);
	if (cost[cV] == BIG)
		return -1;
	return cost[cV];
}
// //////////////////////////////
// #include "swap.h"

// #include <cassert>
// #include <cstdio>

// #include <string>
// #include <vector>

// int main()
// {
// 	int N, M;
// 	assert(2 == scanf("%d %d", &N, &M));

// 	std::vector<int> U(M), V(M), W(M);
// 	for (int i = 0; i < M; ++i)
// 	{
// 		assert(3 == scanf("%d %d %d", &U[i], &V[i], &W[i]));
// 	}

// 	int Q;
// 	assert(1 == scanf("%d", &Q));

// 	std::vector<int> X(Q), Y(Q);
// 	for (int i = 0; i < Q; ++i)
// 	{
// 		assert(2 == scanf("%d %d", &X[i], &Y[i]));
// 	}

// 	init(N, M, U, V, W);

// 	std::vector<int> minimum_fuel_capacities(Q);
// 	for (int i = 0; i < Q; ++i)
// 	{
// 		minimum_fuel_capacities[i] = getMinimumFuelCapacity(X[i], Y[i]);
// 	}

// 	for (int i = 0; i < Q; ++i)
// 	{
// 		printf("%d\n", minimum_fuel_capacities[i]);
// 	}

// 	return 0;
// }
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 436 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 720 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 106 ms 32816 KB Output is correct
10 Correct 139 ms 39876 KB Output is correct
11 Correct 145 ms 39408 KB Output is correct
12 Correct 154 ms 41668 KB Output is correct
13 Correct 167 ms 43948 KB Output is correct
14 Correct 136 ms 32864 KB Output is correct
15 Correct 217 ms 44108 KB Output is correct
16 Correct 200 ms 43176 KB Output is correct
17 Correct 211 ms 45612 KB Output is correct
18 Correct 278 ms 48304 KB Output is correct
19 Correct 64 ms 11948 KB Output is correct
20 Correct 250 ms 45640 KB Output is correct
21 Correct 217 ms 44452 KB Output is correct
22 Correct 217 ms 46968 KB Output is correct
23 Correct 307 ms 49452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 238 ms 48492 KB Output is correct
4 Correct 259 ms 50716 KB Output is correct
5 Correct 250 ms 49532 KB Output is correct
6 Correct 244 ms 50632 KB Output is correct
7 Correct 247 ms 50444 KB Output is correct
8 Correct 239 ms 48296 KB Output is correct
9 Correct 253 ms 49880 KB Output is correct
10 Correct 229 ms 48004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 436 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 720 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 856 KB Output is correct
15 Correct 1 ms 704 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 1 ms 856 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 860 KB Output is correct
22 Correct 1 ms 860 KB Output is correct
23 Correct 1 ms 704 KB Output is correct
24 Correct 2 ms 860 KB Output is correct
25 Correct 2 ms 1368 KB Output is correct
26 Correct 2 ms 1112 KB Output is correct
27 Correct 1 ms 860 KB Output is correct
28 Correct 2 ms 1116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 436 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 720 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 106 ms 32816 KB Output is correct
11 Correct 139 ms 39876 KB Output is correct
12 Correct 145 ms 39408 KB Output is correct
13 Correct 154 ms 41668 KB Output is correct
14 Correct 167 ms 43948 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 856 KB Output is correct
20 Correct 1 ms 704 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 1 ms 856 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 860 KB Output is correct
27 Correct 1 ms 860 KB Output is correct
28 Correct 1 ms 704 KB Output is correct
29 Correct 2 ms 860 KB Output is correct
30 Correct 2 ms 1368 KB Output is correct
31 Correct 2 ms 1112 KB Output is correct
32 Correct 1 ms 860 KB Output is correct
33 Correct 2 ms 1116 KB Output is correct
34 Correct 12 ms 5920 KB Output is correct
35 Correct 165 ms 41408 KB Output is correct
36 Correct 143 ms 41404 KB Output is correct
37 Correct 141 ms 41164 KB Output is correct
38 Correct 147 ms 40740 KB Output is correct
39 Correct 143 ms 40548 KB Output is correct
40 Correct 139 ms 37312 KB Output is correct
41 Correct 151 ms 41672 KB Output is correct
42 Correct 145 ms 41156 KB Output is correct
43 Correct 125 ms 44408 KB Output is correct
44 Correct 146 ms 41544 KB Output is correct
45 Correct 163 ms 53948 KB Output is correct
46 Correct 147 ms 41528 KB Output is correct
47 Correct 184 ms 41428 KB Output is correct
48 Correct 162 ms 45256 KB Output is correct
49 Correct 103 ms 46788 KB Output is correct
50 Correct 89 ms 36732 KB Output is correct
51 Correct 141 ms 46532 KB Output is correct
52 Correct 196 ms 59820 KB Output is correct
53 Correct 251 ms 63932 KB Output is correct
54 Correct 222 ms 70848 KB Output is correct
55 Correct 120 ms 44108 KB Output is correct
56 Correct 234 ms 64696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 436 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 720 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 106 ms 32816 KB Output is correct
10 Correct 139 ms 39876 KB Output is correct
11 Correct 145 ms 39408 KB Output is correct
12 Correct 154 ms 41668 KB Output is correct
13 Correct 167 ms 43948 KB Output is correct
14 Correct 136 ms 32864 KB Output is correct
15 Correct 217 ms 44108 KB Output is correct
16 Correct 200 ms 43176 KB Output is correct
17 Correct 211 ms 45612 KB Output is correct
18 Correct 278 ms 48304 KB Output is correct
19 Correct 238 ms 48492 KB Output is correct
20 Correct 259 ms 50716 KB Output is correct
21 Correct 250 ms 49532 KB Output is correct
22 Correct 244 ms 50632 KB Output is correct
23 Correct 247 ms 50444 KB Output is correct
24 Correct 239 ms 48296 KB Output is correct
25 Correct 253 ms 49880 KB Output is correct
26 Correct 229 ms 48004 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 856 KB Output is correct
32 Correct 1 ms 704 KB Output is correct
33 Correct 1 ms 600 KB Output is correct
34 Correct 1 ms 856 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 12 ms 5920 KB Output is correct
37 Correct 165 ms 41408 KB Output is correct
38 Correct 143 ms 41404 KB Output is correct
39 Correct 141 ms 41164 KB Output is correct
40 Correct 147 ms 40740 KB Output is correct
41 Correct 143 ms 40548 KB Output is correct
42 Correct 139 ms 37312 KB Output is correct
43 Correct 151 ms 41672 KB Output is correct
44 Correct 145 ms 41156 KB Output is correct
45 Correct 125 ms 44408 KB Output is correct
46 Correct 146 ms 41544 KB Output is correct
47 Correct 17 ms 5916 KB Output is correct
48 Correct 217 ms 46128 KB Output is correct
49 Correct 212 ms 46392 KB Output is correct
50 Correct 211 ms 46124 KB Output is correct
51 Correct 255 ms 45856 KB Output is correct
52 Correct 216 ms 43640 KB Output is correct
53 Correct 159 ms 33472 KB Output is correct
54 Correct 216 ms 47132 KB Output is correct
55 Correct 227 ms 46124 KB Output is correct
56 Correct 260 ms 48540 KB Output is correct
57 Correct 235 ms 47024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 436 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 720 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 106 ms 32816 KB Output is correct
11 Correct 139 ms 39876 KB Output is correct
12 Correct 145 ms 39408 KB Output is correct
13 Correct 154 ms 41668 KB Output is correct
14 Correct 167 ms 43948 KB Output is correct
15 Correct 136 ms 32864 KB Output is correct
16 Correct 217 ms 44108 KB Output is correct
17 Correct 200 ms 43176 KB Output is correct
18 Correct 211 ms 45612 KB Output is correct
19 Correct 278 ms 48304 KB Output is correct
20 Correct 238 ms 48492 KB Output is correct
21 Correct 259 ms 50716 KB Output is correct
22 Correct 250 ms 49532 KB Output is correct
23 Correct 244 ms 50632 KB Output is correct
24 Correct 247 ms 50444 KB Output is correct
25 Correct 239 ms 48296 KB Output is correct
26 Correct 253 ms 49880 KB Output is correct
27 Correct 229 ms 48004 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 856 KB Output is correct
33 Correct 1 ms 704 KB Output is correct
34 Correct 1 ms 600 KB Output is correct
35 Correct 1 ms 856 KB Output is correct
36 Correct 1 ms 604 KB Output is correct
37 Correct 12 ms 5920 KB Output is correct
38 Correct 165 ms 41408 KB Output is correct
39 Correct 143 ms 41404 KB Output is correct
40 Correct 141 ms 41164 KB Output is correct
41 Correct 147 ms 40740 KB Output is correct
42 Correct 143 ms 40548 KB Output is correct
43 Correct 139 ms 37312 KB Output is correct
44 Correct 151 ms 41672 KB Output is correct
45 Correct 145 ms 41156 KB Output is correct
46 Correct 125 ms 44408 KB Output is correct
47 Correct 146 ms 41544 KB Output is correct
48 Correct 17 ms 5916 KB Output is correct
49 Correct 217 ms 46128 KB Output is correct
50 Correct 212 ms 46392 KB Output is correct
51 Correct 211 ms 46124 KB Output is correct
52 Correct 255 ms 45856 KB Output is correct
53 Correct 216 ms 43640 KB Output is correct
54 Correct 159 ms 33472 KB Output is correct
55 Correct 216 ms 47132 KB Output is correct
56 Correct 227 ms 46124 KB Output is correct
57 Correct 260 ms 48540 KB Output is correct
58 Correct 235 ms 47024 KB Output is correct
59 Correct 64 ms 11948 KB Output is correct
60 Correct 250 ms 45640 KB Output is correct
61 Correct 217 ms 44452 KB Output is correct
62 Correct 217 ms 46968 KB Output is correct
63 Correct 307 ms 49452 KB Output is correct
64 Correct 1 ms 604 KB Output is correct
65 Correct 1 ms 604 KB Output is correct
66 Correct 1 ms 860 KB Output is correct
67 Correct 1 ms 860 KB Output is correct
68 Correct 1 ms 704 KB Output is correct
69 Correct 2 ms 860 KB Output is correct
70 Correct 2 ms 1368 KB Output is correct
71 Correct 2 ms 1112 KB Output is correct
72 Correct 1 ms 860 KB Output is correct
73 Correct 2 ms 1116 KB Output is correct
74 Correct 163 ms 53948 KB Output is correct
75 Correct 147 ms 41528 KB Output is correct
76 Correct 184 ms 41428 KB Output is correct
77 Correct 162 ms 45256 KB Output is correct
78 Correct 103 ms 46788 KB Output is correct
79 Correct 89 ms 36732 KB Output is correct
80 Correct 141 ms 46532 KB Output is correct
81 Correct 196 ms 59820 KB Output is correct
82 Correct 251 ms 63932 KB Output is correct
83 Correct 222 ms 70848 KB Output is correct
84 Correct 120 ms 44108 KB Output is correct
85 Correct 234 ms 64696 KB Output is correct
86 Correct 68 ms 20344 KB Output is correct
87 Correct 217 ms 46132 KB Output is correct
88 Correct 228 ms 46080 KB Output is correct
89 Correct 254 ms 47996 KB Output is correct
90 Correct 177 ms 53036 KB Output is correct
91 Correct 183 ms 50668 KB Output is correct
92 Correct 251 ms 50032 KB Output is correct
93 Correct 268 ms 63644 KB Output is correct
94 Correct 376 ms 68228 KB Output is correct
95 Correct 312 ms 75276 KB Output is correct
96 Correct 249 ms 48944 KB Output is correct
97 Correct 311 ms 58356 KB Output is correct