正解:LCT维护动态最小生成树。
这题就是要动态维护经过x到y的路径的最大值的最小值。那么我们可以先考虑离线做法,即倒着操作,因为删边以后没办法维护最值了。一开始先把没有限制的边加上,然后倒着询问时直接把边加上就行了。不难看出这题是要维护最小生成树,那么当加入一条边时如果两个点已经连通,就要把连通他们的最大的边删掉。所以这道题可以用LCT来动态维护添边删边的操作。于是这题就能解决了(不过加强版卡空间woc)。
//It is made by wfj_2048~#include <algorithm>#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <vector>#include <cmath>#include <queue>#include <stack>#include <map>#include <set>#define N (1100010)#define inf (1<<30)#define il inline#define RG register#define ll long long#define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)using namespace std;map <int,int> hsh[N];struct edge{ int x,y,w; }g[N],q[N];int ch[N][2],fa[N],mx[N],sum[N],val[N],lazy[N],st[N],vis[N],ans[N],n,m,Q;il int gi(){ RG int x=0,q=1; RG char ch=getchar(); while ((ch<'0' || ch>'9') && ch!='-') ch=getchar(); if (ch=='-') q=-1,ch=getchar(); while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar(); return q*x;}il int cmp(const edge &a,const edge &b){ return a.w<b.w; }il void pushdown(RG int x){ lazy[x]=0,lazy[ch[x][0]]^=1,lazy[ch[x][1]]^=1,swap(ch[x][0],ch[x][1]); return; }il void pushup(RG int x){ RG int t=mx[ch[x][mx[ch[x][1]]>mx[ch[x][0]]]]; mx[x]=(val[t]<val[x] ? x : t); return;}il int isroot(RG int x){ return ch[fa[x]][0]!=x && ch[fa[x]][1]!=x; }il void rotate(RG int x){ RG int y=fa[x],z=fa[y],k=ch[y][0]==x; if (!isroot(y)) ch[z][ch[z][1]==y]=x; fa[x]=z; ch[y][k^1]=ch[x][k],fa[ch[x][k]]=y,ch[x][k]=y,fa[y]=x,pushup(y),pushup(x); return;}il void splay(RG int x){ RG int top=0; st[++top]=x; for (RG int i=x;!isroot(i);i=fa[i]) st[++top]=fa[i]; for (RG int i=top;i;--i) if (lazy[st[i]]) pushdown(st[i]); while (!isroot(x)){ RG int y=fa[x],z=fa[y]; if (!isroot(y)){ if ((ch[z][0]==y)^(ch[y][0]==x)) rotate(x); else rotate(y); } rotate(x); } return;}il void access(RG int x){ RG int t=0; while (x) splay(x),ch[x][1]=t,pushup(x),t=x,x=fa[x]; return; }il void makeroot(RG int x){ access(x),splay(x),lazy[x]^=1; return; }il void link(RG int x,RG int y){ makeroot(x),fa[x]=y; return; }il void cut(RG int x,RG int y){ makeroot(x),access(y),splay(y),ch[y][0]=fa[x]=0; return; }il int querymax(RG int x,RG int y){ makeroot(x),access(y),splay(y); return mx[y]; }il int find(RG int x){ access(x),splay(x); while (ch[x][0]) x=ch[x][0]; return x; }il void work(){ n=gi(),m=gi(),Q=gi(); RG int top=0; for (RG int i=1;i<=m;++i){ g[i].x=gi(),g[i].y=gi(),g[i].w=gi(); if (g[i].x>g[i].y) swap(g[i].x,g[i].y); } sort(g+1,g+m+1,cmp); RG int k=0; for (RG int i=1;i<=m;++i){ val[n+i]=g[i].w; if (g[i].x>g[i].y) swap(g[i].x,g[i].y); hsh[g[i].x][g[i].y]=i; } for (RG int i=1;i<=Q;++i){ q[i].w=gi(),q[i].x=gi(),q[i].y=gi(); if (q[i].x>q[i].y) swap(q[i].x,q[i].y); if (q[i].w==2) vis[hsh[q[i].x][q[i].y]]=1; } for (RG int i=1;i<=m;++i){ if (find(g[i].x)!=find(g[i].y) && !vis[hsh[g[i].x][g[i].y]]) link(g[i].x,n+i),link(g[i].y,n+i),k++; if (k==n-1) break; } for (RG int i=Q;i;--i) if (q[i].w==2){ if (find(q[i].x)==find(q[i].y)){ RG int j=querymax(q[i].x,q[i].y); k=hsh[q[i].x][q[i].y]; if (val[j]>val[n+k]){ cut(g[j-n].x,j),cut(g[j-n].y,j); link(q[i].x,n+k),link(q[i].y,n+k); } }else link(q[i].x,n+i),link(q[i].y,n+i); }else ans[++top]=val[querymax(q[i].x,q[i].y)]; for (RG int i=top;i;--i) PRintf("%d/n",ans[i]); return;}int main(){ File("pipe"); work(); return 0;}
新闻热点
疑难解答