#include "swap.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <math.h>
#include <iomanip>
#define rep(i, s, e) for (ll i = s; i < e; i++)
#define upmax(a, b) a = max(a, b)
#define upmin(a, b) a = min(a, b)
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;
const ll INF = 2e18;
const ll MOD = 1e9 + 7;
const ll MAX_JUMP = 25;
ostream& operator<<(ostream & os, const pll & p)
{
cout << "{" << p.first << ", " << p.second << "}" << endl;
return os;
}
ll n, m;
vvpll g;
vvpll tree;
vvll dp;
vll deg;
vector<pair<ll, pll>> edges;
ll cur_node, cur_weight;
vll swappable_in_tree;
vll depth;
vvll papa, papa_max;
void dfs(ll node, ll p) {
for (auto& it : tree[node]) {
ll nei = it.first;
ll w = it.second;
if (nei == p) continue;
papa[0][nei] = node;
papa_max[0][nei] = w;
depth[nei] = depth[node] + 1;
dfs(nei, node);
}
}
struct DSU {
vll papa, sz, swappable, node_in_tree;
DSU(ll n) {
papa.resize(n), sz.resize(n, 1);
swappable.resize(n, false);
node_in_tree.resize(n, -1);
for (ll i = 0; i < n; i++) papa[i] = i, node_in_tree[i] = i;
}
ll find(ll i) {
if (papa[i] == i) return i;
papa[i] = find(papa[i]);
return papa[i];
}
bool unite(ll a, ll b) {
bool is_swappable = false;
if (deg[a] >= 3 || deg[b] >= 3) {
is_swappable = true;
}
a = find(a);
b = find(b);
if (swappable[a] || swappable[b]) is_swappable = true;
if (a == b) {
swappable[a] = true;
tree[cur_node].push_back({ node_in_tree[a], cur_weight});
tree[node_in_tree[a]].push_back({ cur_node, cur_weight});
swappable_in_tree[cur_node] = true;
node_in_tree[a] = cur_node;
return false;
}
if (sz[a] < sz[b]) swap(a, b);
tree[cur_node].push_back({ node_in_tree[a], cur_weight });
tree[node_in_tree[a]].push_back({ cur_node, cur_weight });
tree[cur_node].push_back({ node_in_tree[b], cur_weight });
tree[node_in_tree[b]].push_back({ cur_node, cur_weight });
sz[a] += sz[b];
papa[b] = a;
node_in_tree[a] = cur_node;
swappable[a] = is_swappable;
swappable_in_tree[cur_node] = is_swappable;
return true;
}
};
void dijkstra(ll x, ll y) {
dp.clear();
dp.resize(n, vll(n, INF));
dp[x][y] = 0;
priority_queue<pair<ll, pll>, vector<pair<ll, pll>>, greater<pair<ll, pll>>> pq;
pq.push({ 0, {x, y} });
while (!pq.empty()) {
ll cur_x = pq.top().second.first;
ll cur_y = pq.top().second.second;
ll max_edge = pq.top().first;
pq.pop();
for (auto& it : g[cur_x]) {
ll next_x = it.first;
ll w = it.second;
if (next_x == cur_y) continue;
if (max(w, max_edge) < dp[next_x][cur_y]) {
dp[next_x][cur_y] = max(w, max_edge);
pq.push({ dp[next_x][cur_y], {next_x, cur_y} });
}
}
for (auto& it : g[cur_y]) {
ll next_y = it.first;
ll w = it.second;
if (next_y == cur_x) continue;
if (max(w, max_edge) < dp[cur_x][next_y]) {
dp[cur_x][next_y] = max(w, max_edge);
pq.push({ dp[cur_x][next_y], {cur_x, next_y} });
}
}
}
}
ll LCA(ll a, ll b) {
if (depth[a] < depth[b]) swap(a, b);
for (ll i = MAX_JUMP - 1; i >= 0; i--) {
if (papa[i][a] != -1 && depth[papa[i][a]] >= depth[b]) {
a = papa[i][a];
}
}
if (a == b) return a;
for (ll i = MAX_JUMP - 1; i >= 0; i--) {
if (papa[i][a] != papa[i][b]) {
a = papa[i][a];
b = papa[i][b];
}
}
return papa[0][a];
}
ll get_max_edge(ll x, ll p) {
ll ans = 0;
for (ll i = MAX_JUMP - 1; i >= 0; i--) {
if (papa[i][x] != -1 && depth[papa[i][x]] >= depth[p]) {
upmax(ans, papa_max[i][x]);
x = papa[i][x];
}
}
if (x == p) return ans;
return max(ans, papa_max[0][x]);
}
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
n = N, m = M;
g.clear(), g.resize(n);
edges.clear();
rep(i, 0, m) {
g[U[i]].push_back({ V[i], W[i] });
g[V[i]].push_back({ U[i], W[i] });
edges.push_back({ W[i], {U[i], V[i]} });
}
sort(edges.begin(), edges.end());
swappable_in_tree.clear(), swappable_in_tree.resize(n + m, false);
DSU dsu(n + m);
deg.clear(), deg.resize(n, 0);
tree.clear(), tree.resize(n + m);
cur_node = n;
rep(i, 0, m) {
ll w = edges[i].first;
ll a = edges[i].second.first;
ll b = edges[i].second.second;
deg[a]++, deg[b]++;
cur_weight = w;
dsu.unite(a, b);
cur_node++;
}
/*rep(i, 0, cur_node) {
cout << "node = " << i << " is_swappable: " << swappable_in_tree[i] << endl;
for (auto& it : tree[i]) {
ll nei = it.first;
ll w = it.second;
cout << it;
}
}*/
// Prepare binary lifting on the new tree:
ll num_nodes = cur_node;
depth.clear(), depth.resize(num_nodes);
papa.clear(), papa.resize(MAX_JUMP, vll(num_nodes, -1));
papa_max.clear(), papa_max.resize(MAX_JUMP, vll(num_nodes, -1));
dfs(num_nodes - 1, -1);
rep(i, 1, MAX_JUMP) {
rep(j, 0, num_nodes) {
if (papa[i - 1][j] != -1) {
papa[i][j] = papa[i - 1][papa[i - 1][j]];
papa_max[i][j] = max(papa_max[i - 1][j], papa_max[i - 1][papa[i - 1][j]]);
}
}
}
}
int getMinimumFuelCapacity(int X, int Y) {
ll x = X, y = Y;
ll lca = LCA(x, y);
ll max_edge_on_path = get_max_edge(x, lca);
upmax(max_edge_on_path, (get_max_edge(y, lca)));
if (swappable_in_tree[lca]) return max_edge_on_path;
for (ll i = MAX_JUMP - 1; i >= 0; i--) {
if (papa[i][lca] != -1 && !swappable_in_tree[papa[i][lca]]) {
lca = papa[i][lca];
}
}
max_edge_on_path = papa_max[0][lca];
if (papa[0][lca] == -1) return -1;
return max_edge_on_path;
deg.clear(), deg.resize(n, 0);
DSU dsu(n);
rep(i, 0, m) {
ll w = edges[i].first;
ll a = edges[i].second.first;
ll b = edges[i].second.second;
deg[a]++, deg[b]++;
dsu.unite(a, b);
ll root_x = dsu.find(x);
ll root_y = dsu.find(y);
if (root_x == root_y && dsu.swappable[root_x]) {
return w;
}
}
return -1;
dijkstra(x, y);
if (dp[y][x] == INF) return -1;
return dp[y][x];
}
/*
5 4
0 1 3
0 2 10
0 3 5
0 4 4
8
0 1
0 2
0 3
0 4
1 2
1 3
1 4
2 3
3 3
0 1 1
1 2 2
2 0 3
3
0 1
0 2
1 2
4 3
0 1 1
1 2 2
2 3 1
3
0 1
0 2
1 2
5 6
0 1 4
0 2 4
1 2 1
1 3 2
1 4 10
2 3 3
3
1 2
2 4
0 1
*/