题目在这里呀
这题是补上的题解,以前就A了,现在重新看了一遍qaq
题意:求1到n的路径上A的最大值+B的最大值最小?
好像是这样的吧有点忘记了。
题解
LCT+并查集,很容易想到要降维,所以就把a从小到大插入,同时用并查集来维护b形成的生成树。
具体来说是,如果u到v已经联通,则找出u到v的路径上最大的b,如果大于bi,则删掉那条边替换为u-v。如果1和n已经属于一个块了,就可以去更新一下答案。
差不多就这样?
//Suplex
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#define N 100000+1000
using namespace std;
const int inf=1e9;
int n,m,ans,top,q[N+N],mx[N+N],val[N+N],fa[N+N],f[N+N],tr[N+N][2];
bool rev[N+N];
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
struct edge{
int u,v,a,b;
}e[N];
int find(int x){
if(f[x]!=x) f[x]=find(f[x]);
return f[x];
}
bool cmp(edge x,edge y){return x.a<y.a;}
inline bool isroot(int x){
return tr[fa[x]][0]!=x && tr[fa[x]][1]!=x;
}
void update(int x){
int l=tr[x][0],r=tr[x][1];
mx[x]=x;
if(val[mx[l]]>val[mx[x]]) mx[x]=mx[l];
if(val[mx[r]]>val[mx[x]]) mx[x]=mx[r];
}
void pushdown(int x){
int l=tr[x][0],r=tr[x][1];
if(rev[x]){
rev[x]^=1;rev[l]^=1;rev[r]^=1;
swap(tr[x][0],tr[x][1]);
}
}
inline void rotate(int x){
int f=fa[x],ff=fa[f],l,r;
if(tr[f][0]==x) l=0;else l=1;r=l ^ 1;
if(!isroot(f)) tr[ff][tr[ff][1]==f]=x;
fa[tr[x][r]]=f;fa[f]=x;fa[x]=ff;
tr[f][l]=tr[x][r];tr[x][r]=f;
update(f);update(x);
}
inline void splay(int x){
q[++top]=x;
for(int i=x;!isroot(i);i=fa[i]) q[++top]=fa[i];
while(top) pushdown(q[top--]);
while(!isroot(x)){
int f=fa[x],ff=fa[f];
if(!isroot(f)){
if(tr[f][0]==x ^ tr[ff][0]==f) rotate(x);
else rotate(f);
}
rotate(x);
}
}
void access(int x){
for(int t=0;x;t=x,x=fa[x])
splay(x),tr[x][1]=t,update(x);
}
void makeroot(int x){
access(x);splay(x);rev[x]^=1;
}
void link(int x,int y){
makeroot(x);fa[x]=y;
}
void split(int x,int y){
makeroot(x);access(y);splay(y);
tr[y][0]=fa[x]=0;update(y);
}
int query(int x,int y){
makeroot(x);access(y);splay(y);
return mx[y];
}
int main()
{
n=read();m=read();
for(int i=1;i<=n;i++) f[i]=i;
for(int i=1;i<=m;i++){
e[i].u=read();e[i].v=read();e[i].a=read();e[i].b=read();
}
sort(e+1,e+m+1,cmp);
int tot=0;ans=inf;
for(int i=1;i<=m;i++){
int u=e[i].u,v=e[i].v,a=e[i].a,b=e[i].b;
if(find(u)==find(v)){
int t=query(u,v);
if(val[t]>e[i].b){
split(t,e[t-n].u);
split(t,e[t-n].v);
}else{
if(find(1)==find(n)) ans=min(ans,e[i].a+val[query(1,n)]);
continue;
}
}else f[find(v)]=find(u);
val[n+i]=e[i].b;mx[n+i]=n+i;
link(u,n+i);link(v,n+i);
if(find(1)==find(n)) ans=min(ans,e[i].a+val[query(1,n)]);
}
if(ans==inf) puts("-1");else printf("%d\n",ans);
return 0;
}