제출 #870728

#제출 시각아이디문제언어결과실행 시간메모리
870728marvinthang자매 도시 (APIO20_swap)C++17
100 / 100
407 ms65932 KiB
/*************************************
*    author: marvinthang             *
*    created: 09.11.2023 08:43:17    *
*************************************/

#include "swap.h"
#include <bits/stdc++.h>

using namespace std;

#define                  fi  first
#define                  se  second
#define                left  ___left
#define               right  ___right
#define                TIME  (1.0 * clock() / CLOCKS_PER_SEC)
#define             MASK(i)  (1LL << (i))
#define           BIT(x, i)  ((x) >> (i) & 1)
#define  __builtin_popcount  __builtin_popcountll
#define              ALL(v)  (v).begin(), (v).end()
#define           REP(i, n)  for (int i = 0, _n = (n); i < _n; ++i)
#define          REPD(i, n)  for (int i = (n); i-- > 0; )
#define        FOR(i, a, b)  for (int i = (a), _b = (b); i < _b; ++i) 
#define       FORD(i, b, a)  for (int i = (b), _a = (a); --i >= _a; ) 
#define       FORE(i, a, b)  for (int i = (a), _b = (b); i <= _b; ++i) 
#define      FORDE(i, b, a)  for (int i = (b), _a = (a); i >= _a; --i) 
#define        scan_op(...)  istream & operator >> (istream &in, __VA_ARGS__ &u)
#define       print_op(...)  ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
		#include "debug.h"
#else
		#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
		#define DB(...) 23
		#define db(...) 23
		#define debug(...) 23
#endif

template <class U, class V> scan_op(pair <U, V>)  { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>)  { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>)  { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")";  else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }

// end of template

const int LOG = 20;

vector <vector <int>> adj;
vector <bool> can_connect;
vector <int> weights, anc[20], depth;

void init(int n, int m, vector <int> U, vector <int> V, vector <int> W) {
	vector <tuple <int, int, int>> edges;
	REP(i, m) edges.emplace_back(W[i], U[i], V[i]);
	sort(ALL(edges));
	vector <int> par(n + m);
	iota(ALL(par), 0);
	vector <int> deg(n);
	adj.resize(n + m);
	can_connect.resize(n + m);
	weights.resize(n + m);
	function <int(int)> find = [&] (int u) {
		return par[u] == u ? u : par[u] = find(par[u]);
	};
	auto join = [&] (int u, int v) {
		int pu = find(u), pv = find(v);
		par[pu] = par[pv] = n;
		adj[n].push_back(pu);
		can_connect[n] = can_connect[pu] || can_connect[pv];
		if (pu == pv) {
			can_connect[n] = true;
		} else {
			adj[n].push_back(pv);
			if (max(++deg[u], ++deg[v]) > 2) can_connect[n] = true;
		}
		++n;
	};

	for (auto [w, u, v]: edges) {
		weights[n] = w;
		join(u, v);
	}
	REP(i, LOG) anc[i].resize(n);
	depth.resize(n);
	function<void(int)> dfs = [&] (int u) {
		FOR(i, 1, LOG) anc[i][u] = anc[i - 1][anc[i - 1][u]];
		for (int v: adj[u]) if (v != anc[0][u]) {
			depth[v] = depth[u] + 1;
			anc[0][v] = u;
			dfs(v);
		}
	};
	anc[0][n - 1] = n - 1;
	dfs(n - 1);
}

int getMinimumFuelCapacity(int u, int v) {
	if (depth[u] < depth[v]) swap(u, v);
	REPD(i, LOG) if (depth[anc[i][u]] >= depth[v]) u = anc[i][u];
	if (u != v) {
		REPD(i, LOG) if (anc[i][u] != anc[i][v]) u = anc[i][u], v = anc[i][v];
		u = anc[0][u];
	}
	if (can_connect[u]) return weights[u];
	REPD(i, LOG) if (!can_connect[anc[i][u]]) u = anc[i][u];
	return anc[0][u] == u ? -1 : weights[anc[0][u]];
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...