考虑离线操作,倒着做,就是维护不断往里加边的最小生成树。具体做法与魔法森林差不多。
至于如何找到一条边的标号,可以二分查找来做。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define N 2100010
#define ll long long
#define inf 0x3f3f3f3f
inline char gc(){
static char buf[1<<16],*S,*T;
if(S==T){T=(S=buf)+fread(buf,1,1<<16,stdin);if(T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0,f=1;char ch=gc();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=gc();
return x*f;
}
int n,m,Q,fa[N],c[N][2],v[N],mx[N],qq[N],tot=0,pa[100010],ans[100010];
bool rev[N],bad[N>>1];
struct quer{
int op,x,y,id;
}q[100010];
struct edge{
int x,y,val,id;
edge(){};
edge(int _x,int _y){x=_x;y=_y;}
friend bool operator<(edge a,edge b){return a.x==b.x?a.y<b.y:a.x<b.x;}
}e[N>>1];
inline bool cmp(edge a,edge b){return a.val<b.val;}
inline bool cmp2(edge a,edge b){return a.id<b.id;}
inline int find(int x){return x==pa[x]?x:pa[x]=find(pa[x]);}
inline bool isroot(int x){return x!=c[fa[x]][0]&&x!=c[fa[x]][1];}
inline void update(int x){
int l=c[x][0],r=c[x][1];
if(v[mx[l]]>=v[mx[r]]&&v[mx[l]]>=v[x]) mx[x]=mx[l];
else if(v[mx[r]]>v[x]) mx[x]=mx[r];else mx[x]=x;
}
inline void pushdown(int p){
if(!rev[p]) return;rev[p]=0;swap(c[p][0],c[p][1]);
rev[c[p][0]]^=1;rev[c[p][1]]^=1;
}
inline void rotate(int x){
int y=fa[x],z=fa[y],l=x==c[y][1],r=l^1;
if(!isroot(y)) c[z][y==c[z][1]]=x;
fa[c[x][r]]=y;fa[y]=x;fa[x]=z;
c[y][l]=c[x][r];c[x][r]=y;update(y);update(x);
}
inline void splay(int x){
int top=0;qq[++top]=x;
for(int xx=x;!isroot(xx);xx=fa[xx]) qq[++top]=fa[xx];
while(top) pushdown(qq[top--]);
while(!isroot(x)){
int y=fa[x],z=fa[y];
if(!isroot(y)){
if(x==c[y][1]^y==c[z][1]) rotate(x);
else rotate(y);
}rotate(x);
}
}
inline void access(int x){
int y=0;while(x){splay(x);c[x][1]=y;update(x);y=x;x=fa[x];}
}
inline void makeroot(int x){
access(x);splay(x);rev[x]^=1;
}
inline void link(int x,int y){
makeroot(x);fa[x]=y;
}
inline void cut(int x,int y){
makeroot(x);access(y);splay(y);c[y][0]=fa[x]=0,update(y);
}
inline int qmax(int x,int y){
makeroot(x);access(y);splay(y);return mx[y];
}
int main(){
// freopen("a.in","r",stdin);
n=read();m=read();Q=read();
for(int i=1;i<=m;++i){
e[i].x=read(),e[i].y=read(),e[i].val=read();v[i+n]=e[i].val;mx[i+n]=i+n;
if(e[i].x>e[i].y) swap(e[i].x,e[i].y);e[i].id=i;
}sort(e+1,e+m+1);
for(int i=1;i<=Q;++i){
q[i].op=read();q[i].x=read();q[i].y=read();
if(q[i].op==1){q[i].id=++tot;continue;}
if(q[i].x>q[i].y) swap(q[i].x,q[i].y);
q[i].id=lower_bound(e+1,e+m+1,edge(q[i].x,q[i].y))->id;bad[q[i].id]=1;
}sort(e+1,e+m+1,cmp);
for(int i=1;i<=n;++i) pa[i]=i;int ofo=0;
for(int i=1;i<=m;++i){
if(ofo==n-1) break;
if(bad[e[i].id]) continue;
int x=e[i].x,y=e[i].y,xx=find(x),yy=find(y);
if(xx==yy) continue;ofo++;pa[xx]=yy;link(x,e[i].id+n);link(e[i].id+n,y);
}sort(e+1,e+m+1,cmp2);
for(int i=Q;i;--i){
if(q[i].op==1){ans[q[i].id]=e[qmax(q[i].x,q[i].y)-n].val;continue;}
int x=q[i].x,y=q[i].y,t=q[i].id,old=qmax(x,y);
if(v[t+n]>=v[old]) continue;cut(e[old-n].x,old);cut(e[old-n].y,old);
link(x,t+n);link(t+n,y);
}for(int i=1;i<=tot;++i) printf("%d\n",ans[i]);
return 0;
}