这题好坑。。。
离线然后用LCT倒着维护最大生成树,这个是一眼
但是注意:边权是用多加一个点(类似kruskal同构树)来实现,实际上点有m+n个,注意数组不要开小,不然会TLE。。。
附数据生成器(但是如果造小数据,每个AC程序输出都可能不一样;大数据没事)
#include<bits/stdc++.h>
#include<set>
using namespace std;
typedef pair<int,int> data;
int dx[1001000],dy[1001000],z[1001000];
set<data>p;
int main(){
freopen("in.txt","w",stdout);
int n=100000,m=1000000,q=100000;
srand(time(0));
printf("%d %d %d\n",n,m,q);
for(int i=1;i<=m;++i){
while(dx[i]==dy[i]||p.end()!=p.find(data(dx[i],dy[i])))dx[i]=rand()%n+1,dy[i]=rand()%n+1;
p.insert(data(dx[i],dy[i])),p.insert(data(dy[i],dx[i])),printf("%d %d %d\n",dx[i],dy[i],rand());
}
for(int i=1;i<=m;++i)z[i]=i;
random_shuffle(z+1,z+m+1);
int p=1;
for(int i=1;i<=q;++i){
int op=rand()%2+1;
if(op==1)printf("%d %d %d\n",op,rand()%n+1,rand()%n+1);
else printf("%d %d %d\n",op,dx[z[p]],dy[z[p]]),p++;
}
}
AC code:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define maxm 2002000
#define maxn 200100
#define inf (1<<30)
using namespace std;
typedef pair<int,int> par;
struct ask{
int op,x,y,w;
}a[maxn];
struct data{
int x,y,w;
data(){}
data(int x,int y):x(x),y(y){}
}d[maxm];
struct edge{
int u,v,w;
}e[maxm>>1];
int s[maxm],pos[maxm],fa[maxm],tag[maxm],ch[maxm][2];
int key[maxm],n,m,q,f[maxn],ptr,anses[maxn],vis[maxn],tp;
int comp(const edge& a,const edge& b){return a.w<b.w;}
int compd(const data& a,const data& b){return a.x==b.x?a.y<b.y:a.x<b.x;}
int find(int x){
return x==f[x]?x:f[x]=find(f[x]);
}
void pushdown(int x){
if(!tag[x])return ;
tag[x]=0;
if(ch[x][0])tag[ch[x][0]]^=1;
if(ch[x][1])tag[ch[x][1]]^=1;
swap(ch[x][0],ch[x][1]);
}
void update(int x){
if(!x)return ;
s[x]=key[x],pos[x]=x;
if(s[x]<s[ch[x][0]])s[x]=s[ch[x][0]],pos[x]=pos[ch[x][0]];
if(s[x]<s[ch[x][1]])s[x]=s[ch[x][1]],pos[x]=pos[ch[x][1]];
}
bool notroot(int x){
return (ch[fa[x]][0]==x||ch[fa[x]][1]==x);
}
void rotate(int p){
int q=fa[p],y=fa[q],k=(ch[q][1]==p);
if(y&¬root(q))ch[y][ch[y][1]==q]=p;
fa[ch[q][k]=ch[p][k^1]]=q;
fa[ch[p][k^1]=q]=p;
fa[p]=y;
update(q),update(p),update(y);
}
void splay(int x){
int y=0;
while(notroot(x)){
if(notroot(y=fa[x]))pushdown(fa[y]);
pushdown(y);pushdown(x);
if(notroot(y)){
if((ch[fa[x]][1]==x)^(ch[fa[y]][1]==y))rotate(x);
else rotate(y);
}
rotate(x);
}
pushdown(x);
update(x);
}
void access(int x){
for(int y=0;x;y=x,x=fa[x]){
splay(x);
ch[x][1]=y;
fa[y]=x;
update(x);
}
}
void rever(int u){
access(u),splay(u);
tag[u]^=1,pushdown(u);
}
void cut(int u,int v){
rever(u);access(v);splay(v);
fa[u]=ch[v][0]=0;
}
void link(int u,int v){
rever(u);access(v);splay(v);
fa[v]=u,ch[u][0]=v;
}
int read(){
register int x=0;
static char act=0;
while(act=getchar(),act<'0');
x=act-'0';
while(act=getchar(),act>='0')x=x*10+act-'0';
return x;
}
int main(){
n=read(),m=read(),q=read();
ptr=max(n,q);
for(int i=0;i<=ptr;++i)f[i]=i,key[i]=-inf,s[i]=-inf;
for(int i=1;i<=m;++i){
e[i].u=read(),e[i].v=read(),e[i].w=read();
if(e[i].u>e[i].v)swap(e[i].u,e[i].v);
}
for(int i=1;i<=q;++i){
a[i].op=read(),a[i].x=read();
a[i].y=read();
if(a[i].x>a[i].y)swap(a[i].x,a[i].y);
if(a[i].op==2)d[++tp]=data(a[i].x,a[i].y);
}
sort(d+1,d+tp+1,compd);
sort(e+1,e+m+1,comp);
for(int i=1;i<=m;++i){
int p=lower_bound(d+1,d+tp+1,data(e[i].u,e[i].v),compd)-d;
if(!(d[p].x==e[i].u&&d[p].y==e[i].v)){
int u=find(e[i].u),v=find(e[i].v);
if(u==v)continue;
key[++ptr]=e[i].w;
d[ptr]=data(e[i].u,e[i].v);
f[u]=v,link(e[i].u,ptr),link(ptr,e[i].v);
} else d[p].w=e[i].w;
}
for(int i=q;i>=1;--i){
int x=a[i].x,y=a[i].y,sx,px;
if(a[i].op==1){
if(find(x)!=find(y)){
anses[i]=0;
} else {
rever(x);access(y);splay(y);
anses[i]=max(key[y],s[ch[y][0]]);
}
vis[i]=1;
} else {
int p=lower_bound(d+1,d+tp+1,data(x,y),compd)-d;
a[i].w=d[p].w;
if(find(x)!=find(y)){
f[find(x)]=find(y);
} else {
rever(x);
access(y);
splay(y);sx=key[y],px=y;
if(sx<s[ch[y][0]])sx=s[ch[y][0]],px=pos[ch[y][0]];
if(sx<a[i].w)continue;
cut(d[px].x,px);
cut(d[px].y,px);
key[++ptr]=a[i].w;
d[ptr]=data(x,y);
link(x,ptr);
link(y,ptr);
}
}
}
for(int i=1;i<=q;++i)if(vis[i])printf("%d\n",anses[i]==-inf?0:anses[i]);
}