#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(u) << endl;
while (k > 0) {
while (k < (1 << pow)) pow--;
// cout << pl(u) << pl(pow) << endl;
res = max(res, jumpMax[u][pow]);
u = jumpNode[u][pow];
// cout << pl(u) << endl;
k -= (1 << pow);
}
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]);
// cout << pl(dep[a] - dep[b]) << endl;
res = rres.second;
a = rres.first;
int pow = LOG - 1;
// cout << pl(a) << pl(b) << pl(res) << 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 |
0 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 |
696 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 |
692 KB |
Output is correct |
9 |
Correct |
163 ms |
33788 KB |
Output is correct |
10 |
Correct |
192 ms |
44232 KB |
Output is correct |
11 |
Correct |
204 ms |
42524 KB |
Output is correct |
12 |
Correct |
215 ms |
45392 KB |
Output is correct |
13 |
Correct |
215 ms |
49500 KB |
Output is correct |
14 |
Correct |
205 ms |
32376 KB |
Output is correct |
15 |
Correct |
487 ms |
45536 KB |
Output is correct |
16 |
Correct |
521 ms |
40904 KB |
Output is correct |
17 |
Correct |
467 ms |
51048 KB |
Output is correct |
18 |
Correct |
513 ms |
48004 KB |
Output is correct |
19 |
Runtime error |
39 ms |
3916 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 |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
207 ms |
34508 KB |
Output is correct |
4 |
Correct |
217 ms |
36092 KB |
Output is correct |
5 |
Correct |
216 ms |
35208 KB |
Output is correct |
6 |
Correct |
221 ms |
35856 KB |
Output is correct |
7 |
Correct |
206 ms |
35648 KB |
Output is correct |
8 |
Correct |
213 ms |
34356 KB |
Output is correct |
9 |
Correct |
220 ms |
35444 KB |
Output is correct |
10 |
Correct |
209 ms |
34056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
696 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 |
692 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 |
0 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 |
696 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 |
692 KB |
Output is correct |
9 |
Correct |
163 ms |
33788 KB |
Output is correct |
10 |
Correct |
192 ms |
44232 KB |
Output is correct |
11 |
Correct |
204 ms |
42524 KB |
Output is correct |
12 |
Correct |
215 ms |
45392 KB |
Output is correct |
13 |
Correct |
215 ms |
49500 KB |
Output is correct |
14 |
Correct |
205 ms |
32376 KB |
Output is correct |
15 |
Correct |
487 ms |
45536 KB |
Output is correct |
16 |
Correct |
521 ms |
40904 KB |
Output is correct |
17 |
Correct |
467 ms |
51048 KB |
Output is correct |
18 |
Correct |
513 ms |
48004 KB |
Output is correct |
19 |
Correct |
207 ms |
34508 KB |
Output is correct |
20 |
Correct |
217 ms |
36092 KB |
Output is correct |
21 |
Correct |
216 ms |
35208 KB |
Output is correct |
22 |
Correct |
221 ms |
35856 KB |
Output is correct |
23 |
Correct |
206 ms |
35648 KB |
Output is correct |
24 |
Correct |
213 ms |
34356 KB |
Output is correct |
25 |
Correct |
220 ms |
35444 KB |
Output is correct |
26 |
Correct |
209 ms |
34056 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
2 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
2 ms |
596 KB |
Output is correct |
32 |
Correct |
2 ms |
712 KB |
Output is correct |
33 |
Correct |
2 ms |
696 KB |
Output is correct |
34 |
Correct |
1 ms |
724 KB |
Output is correct |
35 |
Correct |
2 ms |
596 KB |
Output is correct |
36 |
Correct |
16 ms |
4948 KB |
Output is correct |
37 |
Correct |
200 ms |
46136 KB |
Output is correct |
38 |
Correct |
202 ms |
40396 KB |
Output is correct |
39 |
Correct |
178 ms |
36256 KB |
Output is correct |
40 |
Correct |
174 ms |
34880 KB |
Output is correct |
41 |
Correct |
155 ms |
33808 KB |
Output is correct |
42 |
Correct |
153 ms |
31088 KB |
Output is correct |
43 |
Correct |
199 ms |
42356 KB |
Output is correct |
44 |
Correct |
194 ms |
44652 KB |
Output is correct |
45 |
Correct |
212 ms |
47428 KB |
Output is correct |
46 |
Correct |
151 ms |
35116 KB |
Output is correct |
47 |
Correct |
22 ms |
5128 KB |
Output is correct |
48 |
Correct |
550 ms |
46880 KB |
Output is correct |
49 |
Correct |
577 ms |
43540 KB |
Output is correct |
50 |
Correct |
528 ms |
41176 KB |
Output is correct |
51 |
Correct |
444 ms |
40100 KB |
Output is correct |
52 |
Correct |
364 ms |
37204 KB |
Output is correct |
53 |
Correct |
295 ms |
29112 KB |
Output is correct |
54 |
Correct |
607 ms |
46088 KB |
Output is correct |
55 |
Correct |
497 ms |
48052 KB |
Output is correct |
56 |
Correct |
464 ms |
53684 KB |
Output is correct |
57 |
Correct |
369 ms |
40564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |