Description
SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处运往y处,嘟嘟需要为供水公司找到一条从A至B的水管的路径,接着通过信息化的控制中心通知路径上的水管进入准备送水状态,等到路径上每一条水管都准备好了,供水公司就可以开始送水了。嘟嘟一次只能处理一项送水任务,等到当前的送水任务完成了,才能处理下一项。
在处理每项送水任务之前,路径上的水管都要进行一系列的准备操作,如清洗、消毒等等。嘟嘟在控制中心一声令下,这些水管的准备操作同时开始,但由于各条管道的长度、内径不同,进行准备操作需要的时间可能不同。供水公司总是希望嘟嘟能找到这样一条送水路径,路径上的所有管道全都准备就绪所需要的时间尽量短。嘟嘟希望你能帮助他完成这样的一个选择路径的系统,以满足供水公司的要求。另外,由于MY市的水管年代久远,一些水管会不时出现故障导致不能使用,你的程序必须考虑到这一点。
不妨将MY市的水管网络看作一幅简单无向图(即没有自环或重边):水管是图中的边,水管的连接处为图中的结点。
Input
输入文件第一行为3个整数:N,M,Q分别表示管道连接处(结点)的数目、目前水管(无向边)的数目,以及你的程序需要处理的任务数目(包括寻找一条满足要求的路径和接受某条水管坏掉的事实)。
接下来M行,每行3个整数x,y和t,描述一条对应的水管。x和y表示水管两端结点的编号,t表示准备送水所需要的时间。我们不妨为结点从1至N编号,这样所有的x和y都在范围[1,N]内。
接下来Q行,每行描述一项任务。其中第一个整数为k:若k=1则后跟两个整数A和B,表示你需要为供水公司寻找一条满足要求的从A到B的水管路径;若k=2,则后跟两个整数x和y,表示直接连接x和y的水管宣布报废(保证合法,即在此之前直接连接x和y尚未报废的水管一定存在)。
Output
按顺序对应输入文件中每一项k=1的任务,你需要输出一个数字和一个回车/换行符。该数字表示:你寻找到的水管路径中所有管道全都完成准备工作所需要的时间(当然要求最短)。
Sample Input
4 4 4
1 2 2
2 3 3
3 4 2
1 4 2
1 1 4
2 1 4
1 1 4
1 1 4
Sample Output
2 3 3
Hint
【数据范围】
对于60%的数据,满足N≤1000,M≤100000,Q≤100000;
对于100%的数据,满足N≤100000,M≤1000000,Q≤100000;
又来复习一波LCT,真是心力憔悴哇。。。
维护最小生成树,因为删边不好搞,所以倒着来。
#include<bits/stdc++.h>
using namespace std;
const int Maxn=2000005;
struct Edge{
int x,y,t,connect;
bool operator <(const Edge&A) const {
return x==A.x?y<A.y:x<A.x;
}
}e[Maxn],s[Maxn];
struct Splay{
int p[Maxn],ch[Maxn][2];
int v[Maxn],maxp[Maxn];
bool rev[Maxn];
#define Ls(x) ch[x][0]
#define rs(x) ch[x][1]
inline bool isroot(int x){
return (Ls(p[x])^x)&&(rs(p[x])^x);
}
inline void maintain(int x){
maxp[x]=x;
if(v[maxp[Ls(x)]]>v[maxp[x]])maxp[x]=maxp[Ls(x)];
if(v[maxp[rs(x)]]>v[maxp[x]])maxp[x]=maxp[rs(x)];
}
inline void rotate(int x){
int f=p[x],gf=p[f],tp=rs(f)==x,son=ch[x][!tp];
if(!isroot(f))ch[gf][rs(gf)==f]=x;p[x]=gf;
ch[p[son]=f][tp]=son,maintain(f);
ch[p[f]=x][!tp]=f,maintain(x);
}
inline void pushdown(int x){
if(rev[x]){
rev[Ls(x)]^=1,rev[rs(x)]^=1;
swap(Ls(x),rs(x));
rev[x]^=1;
}
}
int top,s[Maxn];
inline void splay(int x){
s[++top]=x;for(int i=x;!isroot(i);i=p[i])s[++top]=p[i];
while(top)pushdown(s[top--]);
while(!isroot(x)){
if(!isroot(p[x])&&((rs(p[p[x]])==p[x])==(rs(p[x])==x)))rotate(p[x]);
rotate(x);
}
}
};
struct LinkCutTree{
Splay splay;
inline void access(int x){
for(int lastx=0;x;lastx=x,x=splay.p[x])
splay.splay(x),splay.rs(x)=lastx,splay.maintain(x);
}
inline void makeroot(int x){
access(x),splay.splay(x),splay.rev[x]^=1;
}
inline void link(int x,int y){
makeroot(x),splay.p[x]=y;
}
inline void split(int x,int y){
makeroot(x),access(y),splay.splay(y);
}
inline void cut(int x,int y){
split(x,y);splay.Ls(y)=splay.p[x]=0;
splay.maintain(y);
}
inline int getroot(int x){
access(x),splay.splay(x);
for(;splay.Ls(x);x=splay.Ls(x));
return x;
}
}Lct;
int L[Maxn],r[Maxn];
int cnt,ans[Maxn];
inline void Mst(int n,int m){
for(int i=1;i<=m;++i){
Lct.splay.v[n+i]=e[i].t;
Lct.splay.maxp[n+i]=n+i;
}
for(int i=1;i<=m;++i){
int x=e[i].x,y=e[i].y;
if(!e[lower_bound(e+1,e+m+1,(Edge){x,y})-e].connect)continue;
if(Lct.getroot(x)==Lct.getroot(y)){
Lct.split(x,y);
int maxp=Lct.splay.maxp[y];
if(Lct.splay.v[maxp]>e[i].t){
Lct.cut(maxp,L[maxp]),Lct.cut(maxp,r[maxp]);
L[n+i]=x,r[n+i]=y;
Lct.link(x,n+i),Lct.link(n+i,y);
}
}else {
L[n+i]=x,r[n+i]=y;
Lct.link(x,n+i),Lct.link(n+i,y);
}
}
}
int main(){
int n,m,q;scanf("%d%d%d",&n,&m,&q);
for(int i=1;i<=m;++i){
int x,y,t;scanf("%d%d%d",&x,&y,&t);
if(x>y)swap(x,y);
e[i]=(Edge){x,y,t,1};
}
sort(e+1,e+m+1);
for(int i=1;i<=q;++i){
int op,x,y;scanf("%d%d%d",&op,&x,&y);
if(x>y)swap(x,y);
s[i]=(Edge){x,y,op};
op==2?e[lower_bound(e+1,e+m+1,(Edge){x,y})-e].connect=0:1;
}
Mst(n,m);
for(int i=q;i>=1;--i){
int op=s[i].t,x=s[i].x,y=s[i].y;
if(op==2){
Lct.split(x,y);
int maxp=Lct.splay.maxp[y];
int p=(lower_bound(e+1,e+m+1,(Edge){x,y})-e)+n;
if(Lct.splay.v[maxp]>Lct.splay.v[p]){
Lct.cut(maxp,L[maxp]),Lct.cut(maxp,r[maxp]);
L[p]=x,r[p]=y;
Lct.link(x,p),Lct.link(p,y);
}
}else {
Lct.split(x,y);
int maxp=Lct.splay.maxp[y];
ans[++cnt]=Lct.splay.v[maxp];
}
}
for(int i=cnt;i>=1;--i)cout<<ans[i]<<"\n";
return 0;
}