# 洛谷P4172 [WC2006]水管局长 （LCT，最小生成树）

## 思路分析

#include<cstdio>
#include<algorithm>
using namespace std;
#define R register int
#define I inline void
#define lc c[x][0]
#define rc c[x][1]
const int N=1009,M=2009,L=100009;
int f[M],c[M][2],v[M],mx[M],ex[M];
//ex存放LCT中代表边的点的子节点，为卡常cut帮忙
int ff[N],l[N][N],k[L],a[L],b[L],ans[L];
bool r[M],g[N][N];//暴力搞邻接矩阵
struct EDGE{
int u,v,l;
inline bool operator<(EDGE x)const{
return l<x.l;
}
}e[L];
char ch;int z;
inline int in(){
while((ch=getchar())<'-');
z=ch&15;
while((ch=getchar())>'-')z*=10,z+=ch&15;
return z;
}
inline bool nroot(R x){return c[f[x]][0]==x||c[f[x]][1]==x;}
inline int get(R x,R y){return v[x]>v[y]?x:y;}
I pushup(R x){
mx[x]=get(x,get(mx[lc],mx[rc]));
}
I pushdown(R x){
if(r[x]){
R t=lc;
r[lc=rc]^=1;r[rc=t]^=1;r[x]=0;
}
}
I pushall(R x){
if(nroot(x))pushall(f[x]);
pushdown(x);
}
I rotate(R x){
R y=f[x],z=f[y],k=c[y][1]==x,w=c[x][!k];
if(nroot(y))c[z][c[z][1]==y]=x;c[x][!k]=y;c[y][k]=w;
f[w]=y;f[y]=x;f[x]=z;
pushup(y);
}
I splay(R x){
R y=x;
pushall(x);
while(nroot(x)){
if(nroot(y=f[x]))
rotate((c[y][0]==x)^(c[f[y]][0]==y)?x:y);
rotate(x);
}
pushup(x);
}
I access(R x){
for(R y=0;x;x=f[y=x])
splay(x),rc=y,pushup(x);
}
I mroot(R x){
access(x);splay(x);
r[x]^=1;
}
mroot(x);f[f[ex[E]=x]=E]=y;\
v[E]=l[x][y];pushup(E)
I cut(R x){
access(ex[x]);splay(x);
lc=rc=f[lc]=f[rc]=0;
}
int getf(R x){
if(x==ff[x])return x;
return ff[x]=getf(ff[x]);
}
int main(){
R n,m,Q,i,x,y,tmp,cnt;
n=in();m=in();Q=in();
for(i=1;i<=m;++i){
x=e[i].u=in();y=e[i].v=in();
l[x][y]=l[y][x]=e[i].l=in();
}
for(i=1;i<=Q;++i){
k[i]=in();a[i]=in();b[i]=in();
if(k[i]&2)g[a[i]][b[i]]=g[b[i]][a[i]]=1;
}
for(i=0;i<=n;++i)
ff[i]=i;
//接下来还是走一遍kruskal
sort(e+1,e+m+1);
for(cnt=n*2-1,i=1;cnt>n;++i){
x=e[i].u;y=e[i].v;
if(!g[x][y]&&getf(x)!=getf(y)){
ff[ff[x]]=ff[y];
}
}
for(cnt=0,i=Q;i;--i){
mroot(x=a[i]);
access(y=b[i]);splay(y);
if(k[i]&1)ans[++cnt]=v[mx[y]];//答案压到栈里面，最后反过来弹
else if(v[mx[y]]>l[x][y]){