답안 #568595

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
568595 2022-05-25T18:44:03 Z _karan_gandhi 자매 도시 (APIO20_swap) C++17
7 / 100
564 ms 51084 KB
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define all(v) v.begin(), v.end()
#define endl '\n'
#define pl(var) " [" << #var << ": " << (var) << "] "

template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "[" << p.first << ", " << p.second << "]"; }
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) { cout << "["; for(int i = 0; i < (int)v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";}
template<typename A, typename B> istream& operator>>(istream& cin, pair<A, B> &p) { cin >> p.first; return cin >> p.second; }

int n, m;
// vector<int> _u, _v, w;
vector<int> dp, dep;
vector<vector<pair<int, int>>> adj;
vector<vector<int>> jumpNode, jumpMax;
const int LOG = 20;
int inf = 1e9 + 7;

void dfs(int u, int par, int wt) {
	if (u != par) dep[u] = dep[par] + 1;
	else dep[u] = 0;

	jumpNode[u][0] = par;
	if (wt != -1) jumpMax[u][0] = wt;

	if (adj[u].size() >= 3) {
		priority_queue<int> best;
		for (auto [_, _wt] : adj[u]) best.push(-_wt);
		best.pop();
		best.pop();
		// cout << pl(u) << pl(-best.top()) << endl;
		dp[u] = -best.top();
		// cout << pl(best.top()) << endl;
	}

	for (auto [v, _wt] : adj[u]) {
		if (v == par) continue;

		dfs(v, u, _wt);
	}

	for (auto [v, _] : adj[u]) {
		if (v == par) continue;
		dp[u] = min(dp[u], max(dp[v], _));
		dp[v] = min(dp[v], max(dp[u], _));
	}
}

void dfs2(int u, int par) {
	for (auto [v, _] : adj[u]) {
		if (v == par) continue;
		dp[u] = min(dp[u], max(dp[v], _));
		dp[v] = min(dp[v], max(dp[u], _));
	}

	for (auto [v, _wt] : adj[u]) {
		if (v == par) continue;

		dfs2(v, u);
	}
}

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
	// solve the tree case
	// get the max on the path from u to v <- log(n)
	n = N;
	m = M;
	assert(m == n - 1); // just the tree case

	dp = vector<int>(n, inf);
	// dp.resize(n);
	adj.resize(n);
	dep.resize(n);

	jumpNode = vector<vector<int>>(n, vector<int>(LOG));
	jumpMax = vector<vector<int>>(n, vector<int>(LOG));

	for (int i = 0; i < m; i++) {
		adj[U[i]].emplace_back(V[i], W[i]);
		adj[V[i]].emplace_back(U[i], W[i]);
	}

	dfs(0, 0, -1);
	dfs2(0, 0);

	for (int i = 1; i < LOG; i++) {
		for (int j = 0; j < n; j++) {
			jumpNode[j][i] = jumpNode[jumpNode[j][i - 1]][i - 1];
			jumpMax[j][i] = max(jumpMax[j][i - 1], jumpMax[jumpNode[j][i - 1]][i - 1]);
		}
	}

	// for (auto i : jumpMax) {
	// 	cout << i << endl;
	// }
}

pair<int, int> kth(int u, int k) {
	int res = 0;
	int pow = LOG - 1;
	// cout << pl(k) << endl;
	while (k > 0) {
		while (k < (1 << pow)) pow--;
		// cout << pl(u) << pl(1 << pow) << pl(k) << endl;
		res = max(res, jumpMax[u][pow]);
		u = jumpNode[u][pow];

		k -= (1 << k);
	}

	return make_pair(u, res);
}

int get_cost(int a, int b) {
	int res = 0;

	if (dep[a] < dep[b]) {
		swap(a, b);
	}
	// cout << pl(a) << pl(b) << endl;

	pair<int, int> rres = kth(a, dep[a] - dep[b]);
	res = rres.second;
	a = rres.first;
	int pow = LOG - 1;
	// cout << pl(a) << pl(b) << endl;
	if (a == b) {
		return res;
	}
	// cout << "HERE" << endl;

	while (pow >= 0) {
		if (jumpNode[a][pow] != jumpNode[b][pow]) {
			res = max({
				res,
				jumpMax[a][pow],
				jumpMax[b][pow]
			});

			a = jumpNode[a][pow];
			b = jumpNode[b][pow];
		}

		pow--;
	}

	assert(jumpNode[a][0] == jumpNode[b][0]);
	return max({res, jumpMax[a][0], jumpMax[b][0]});
}

int getMinimumFuelCapacity(int x, int y) {
	if (n == 2) return -1;
	// cout << pl(dp[x]) << endl;
	int res = max(dp[x], get_cost(x, y));
	return res >= inf ? -1 : res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 148 ms 33888 KB Output is correct
10 Correct 182 ms 44236 KB Output is correct
11 Correct 195 ms 42388 KB Output is correct
12 Correct 188 ms 45512 KB Output is correct
13 Correct 219 ms 49240 KB Output is correct
14 Correct 200 ms 32384 KB Output is correct
15 Correct 458 ms 45512 KB Output is correct
16 Correct 500 ms 40856 KB Output is correct
17 Correct 564 ms 51084 KB Output is correct
18 Correct 522 ms 47928 KB Output is correct
19 Runtime error 33 ms 3724 KB Execution killed with signal 6
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 213 ms 34560 KB Output is correct
4 Correct 200 ms 36188 KB Output is correct
5 Correct 205 ms 35292 KB Output is correct
6 Correct 215 ms 36028 KB Output is correct
7 Correct 248 ms 35772 KB Output is correct
8 Correct 209 ms 34444 KB Output is correct
9 Correct 209 ms 35476 KB Output is correct
10 Correct 207 ms 34136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Runtime error 1 ms 340 KB Execution killed with signal 6
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 340 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 148 ms 33888 KB Output is correct
10 Correct 182 ms 44236 KB Output is correct
11 Correct 195 ms 42388 KB Output is correct
12 Correct 188 ms 45512 KB Output is correct
13 Correct 219 ms 49240 KB Output is correct
14 Correct 200 ms 32384 KB Output is correct
15 Correct 458 ms 45512 KB Output is correct
16 Correct 500 ms 40856 KB Output is correct
17 Correct 564 ms 51084 KB Output is correct
18 Correct 522 ms 47928 KB Output is correct
19 Correct 213 ms 34560 KB Output is correct
20 Correct 200 ms 36188 KB Output is correct
21 Correct 205 ms 35292 KB Output is correct
22 Correct 215 ms 36028 KB Output is correct
23 Correct 248 ms 35772 KB Output is correct
24 Correct 209 ms 34444 KB Output is correct
25 Correct 209 ms 35476 KB Output is correct
26 Correct 207 ms 34136 KB Output is correct
27 Incorrect 2 ms 596 KB Output isn't correct
28 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 340 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -