#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
//bin_climb_str
struct climb{
int w,x,y;
};
//seg_tree
struct pont{
int a,b;
int bal,jobb;
int e;
};
vector<int> f;
pont t[500001];
int p1=0;
int fa;
int seg_build(int x,int y) {
int p2=p1; p1++;
t[p2].a=x; t[p2].b=y;
if (x==y) {
t[p2].e=f[x];
return(p2);
}
t[p2].bal=seg_build(x,(x+y)/2);
t[p2].jobb=seg_build((x+y)/2+1,y);
t[p2].e=min(t[t[p2].bal].e,t[t[p2].jobb].e);
return(p2);
}
int seg_find(int q,int x,int y) {
if ((t[q].a>y)||(t[q].b<x)) {
return(1e8);
}
if ((t[q].a>=x)&&(t[q].b<=y)) {
return(t[q].e);
}
return(min(seg_find(t[q].bal,x,y),seg_find(t[q].jobb,x,y)));
}
struct el{
int w,x;
};
struct elek{
int a,b,w;
};
bool comp(elek a,elek b) {
return((a.w<b.w)||((a.w==b.w)&&(a.a<b.a)));
}
bool comp2(el a,el b) {
return((a.w<b.w)||((a.w==b.w)&&(a.x<b.x)));
}
vector<el> t1[100001];
int n,m;
el z;
int har[100001];
vector<el> t2[100001];
vector<elek> g;
elek z2;
//dsu
int r[100001];
int s[100001];
void dsu_init() {
for (int i=0;i<n;i++) {
r[i]=i;
s[i]=1;
}
}
int dsu_find(int a) {
if (a==r[a]) {
return(a);
}
r[a]=dsu_find(r[a]);
return(r[a]);
}
void dsu_merge(int a,int b) {
if (s[a]<s[b]) {
r[a]=b;
} else {
r[b]=a;
}
}
//bin_climb
vector<climb> cl[100001];
climb z3;
//dfs
int h[100001];
int l=1;
int d[100001];
int c[100001];
int par[100001];
int par2[100001];
void dfs(int p) {
h[p]=l;
d[l]=p; l++;
c[p]=f.size();
f.push_back(h[p]);
if (p>0) {
z3.w=par2[p];
z3.x=par[p];
z3.y=har[par[p]];
cl[p].push_back(z3);
int e=0;
while (e>-1) {
if (cl[cl[p][e].x].size()>e) {
z3.x=cl[cl[p][e].x][e].x;
z3.w=max(cl[p][e].w,cl[cl[p][e].x][e].w);
z3.y=min(cl[p][e].y,cl[cl[p][e].x][e].y);
cl[p].push_back(z3);
e++;
} else {
e=-1;
}
}
}
for (int i=0;i<t2[p].size();i++) {
if (h[t2[p][i].x]==0) {
par[t2[p][i].x]=p;
par2[t2[p][i].x]=t2[p][i].w;
dfs(t2[p][i].x);
f.push_back(h[p]);
}
}
}
int lca(int a,int b) {
return(d[seg_find(fa,min(c[a],c[b]),max(c[a],c[b]))]);
}
//update
void dff(int p,int ha) {
har[p]=ha;
for (int i=0;i<t1[p].size();i++) {
if (har[t1[p][i].x]>max(ha,t1[p][i].w)) {
dff(t1[p][i].x,max(ha,t1[p][i].w));
}
}
}
vector<el> o;
void init(int N1, int M1, std::vector<int> U1, std::vector<int> V1, std::vector<int> W1) {
n=N1;
m=M1;
for (int i=0;i<m;i++) {
z.w=W1[i];
z.x=V1[i];
t1[U1[i]].push_back(z);
z.x=U1[i];
t1[V1[i]].push_back(z);
z2.a=U1[i];
z2.b=V1[i];
z2.w=W1[i];
g.push_back(z2);
}
for (int i=0;i<n;i++) {
sort(t1[i].begin(),t1[i].end(),comp2);
if (t1[i].size()>=3) {
har[i]=t1[i][2].w;
} else {
har[i]=1e9;
}
}
dsu_init();
sort(g.begin(),g.end(),comp);
for (int i=0;i<m;i++) {
if (dsu_find(g[i].a)!=dsu_find(g[i].b)) {
dsu_merge(dsu_find(g[i].a),dsu_find(g[i].b));
z.w=g[i].w;
z.x=g[i].a;
t2[g[i].b].push_back(z);
z.x=g[i].b;
t2[g[i].a].push_back(z);
} else {
har[g[i].a]=min(har[g[i].a],g[i].w);
har[g[i].b]=min(har[g[i].b],g[i].w);
}
}
for (int i=0;i<n;i++) {
z.w=har[i];
z.x=i;
o.push_back(z);
}
sort(o.begin(),o.end(),comp2);
for (int i=0;i<n;i++) {
dff(i,har[i]);
}
par[0]=-1;
dfs(0);
fa=seg_build(0,f.size()-1);
}
pair<int,int> get_climb(int fr,int ma,int q) {
pair<int,int> ans;
pair<int,int> ker;
ans.first=0;
ans.second=1e9;
if (fr==ma) {
return(ans);
}
int l=min(q,int(cl[fr].size())-1);
while ((l>=0)&&(h[cl[fr][l].x]<h[ma])) {
l--;
}
ker=get_climb(cl[fr][l].x,ma,l-1);
ans.first=max(ker.first,cl[fr][l].w);
ans.second=min(ker.second,cl[fr][l].y);
return(ans);
}
int getMinimumFuelCapacity(int X1, int Y1) {
int r1,r2,r3;
int x,y;
x=X1; y=Y1;
int lca2=lca(x,y);
pair<int,int> ker;
ker=get_climb(x,lca2,100);
r1=0;
r2=min(har[x],har[y]);
r1=max(r1,ker.first);
r2=min(r2,ker.second);
ker=get_climb(y,lca2,100);
r1=max(r1,ker.first);
r2=min(r2,ker.second);
r3=max(r1,r2);
if (r3==1e9) {
return(-1);
} else {
return(r3);
}
}
int main() {
int n,m,a,b,c;
vector<int> u,v,w;
cin >> n >> m;
for (int i=0;i<m;i++) {
cin >> a >> b >> c;
u.push_back(a);
v.push_back(b);
w.push_back(c);
}
init(n,m,u,v,w);
/*
for (int i=0;i<n;i++) {
for (int j=0;j<t2[i].size();j++) {
cout << t2[i][j].x << " ";
}
cout << endl;
}
cout << f.size() << endl;
for (int i=0;i<f.size();i++) {
cout << f[i] << " ";
}
cout << endl;
cout << seg_find(fa,2,2) << endl;
cout << lca(0,2) << endl;
for (int i=0;i<n;i++) {
for (int j=0;j<cl[i].size();j++) {
cout << cl[i][j].y << " ";
}
cout << endl;
}
for (int i=0;i<n;i++) {
cout << har[i] << endl;
}
*/
int q;
cin >> q;
for (int i=0;i<q;i++) {
cin >> a >> b;
cout << getMinimumFuelCapacity(a,b) << endl;
}
return(0);
}
Compilation message
swap.cpp: In function 'void dfs(int)':
swap.cpp:121:38: warning: comparison of integer expressions of different signedness: 'std::vector<climb>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
121 | if (cl[cl[p][e].x].size()>e) {
| ~~~~~~~~~~~~~~~~~~~~~^~
swap.cpp:133:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<el>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
133 | for (int i=0;i<t2[p].size();i++) {
| ~^~~~~~~~~~~~~
swap.cpp: In function 'void dff(int, int)':
swap.cpp:151:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<el>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
151 | for (int i=0;i<t1[p].size();i++) {
| ~^~~~~~~~~~~~~
/usr/bin/ld: /tmp/ccLUpFiV.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccbhF9CS.o:swap.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status