#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct EDGE {
ll u, v, w;
};
struct DSUNODE;
vector<DSUNODE> nodes;
struct DSUNODE {
ll vert;
ll parent;
ll distToParent;
ll size;
ll left;
ll right;
ll when;
ll leader(ll mxpath = 1e15) {
if (parent == -1 || distToParent > mxpath) return vert;
return nodes[nodes[parent].leader(mxpath)].vert;
}
};
vector<EDGE> edges;
void init(int N, int M, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
edges = vector<EDGE>(M);
nodes = vector<DSUNODE>(N);
for (int i = 0; i < N; i++) {
DSUNODE& a = nodes[i];
a.parent = -1;
a.distToParent = 0;
a.size = 1;
a.vert = a.left = a.right = i;
a.when = 1e15;
}
for (int i = 0; i < M; i++) {
edges[i] = {U[i], V[i], W[i]};
}
sort(edges.begin(), edges.end(), [](const EDGE& a, const EDGE& b){return a.w < b.w;});
//cout << "WORKS" << endl;
for (int i = 0; i < M; i++) {
//cout << nodes[edges[i].u].leader() << " " << nodes[edges[i].v].leader() << endl;
DSUNODE& u = nodes[nodes[edges[i].u].leader()];
DSUNODE& v = nodes[nodes[edges[i].v].leader()];
if (u.vert == v.vert) {
if ((edges[i].u == u.left && edges[i].v == u.right) || (edges[i].u == u.right && edges[i].v == u.left)) {
u.left = -1;
u.right = -1;
u.when = min(u.when, edges[i].w);
}
continue;
}
ll newleft = -1, newright = -1;
if (edges[i].u == u.left && edges[i].v == v.left) {
newleft = u.right;
newright = v.right;
} else if (edges[i].u == u.left && edges[i].v == v.right) {
newleft = u.right;
newright = v.left;
} else if (edges[i].u == u.right && edges[i].v == v.left) {
newleft = u.left;
newright = v.right;
} else if (edges[i].u == u.right && edges[i].v == v.right) {
newleft = u.left;
newright = v.left;
}
if (u.size > v.size) {
v.parent = u.vert;
v.distToParent = edges[i].w;
u.size += v.size;
u.left = newleft;
u.right = newright;
if (newleft == -1) {
u.when = min(u.when, edges[i].w);
}
} else {
u.parent = v.vert;
u.distToParent = edges[i].w;
v.size += u.size;
v.left = newleft;
v.right = newright;
if (newleft == -1) {
v.when = min(v.when, edges[i].w);
}
}
}
}
bool can(ll X, ll Y, ll M) {
ll u = nodes[X].leader(M), v = nodes[Y].leader(M);
return u == v && M >= nodes[u].when;
}
int getMinimumFuelCapacity(int X, int Y) {
if (!can(X, Y, 1e15)) return -1;
ll l = 0, r = 1e15;
while (l + 1 < r) {
ll m = (l + r) / 2;
if (can(X, Y, m)) r = m;
else l = m;
}
return r;
}
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;
}
Compilation message
/usr/bin/ld: /tmp/ccZj3ySa.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccIPL5Kc.o:swap.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status