【WC 2006】水管局长

 

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 3 
1 2 2 
2 3 3 
3 4 2 
1 4 2 
1 1 4 
2 1 4 
1 1 4

Sample Output


3

Hint

N ≤ 1000 
M ≤ 100000 
Q ≤ 100000

测试数据中宣布报废的水管不超过 5000 条;且任何时候我们考虑的水管网络都是连通的,即从任一结点 A 必有至少一条水管路径通往任一结点 B 。


把每条边拆成一个点
,然后倒着搞,把删边看成加边,动态维护最小生成树.
每次加边时判断一下这两个点是否联通,若不联通,直接加即可.若联通且这颗树中的最大的边比当前边大,那么把这条边删去,新加入这条边即可.
具体实现可以维护一个最大值和最大值的位置,对于每一条代表边的点,记录好他连的那两个点.
预处理很鬼,开了两个map乱搞慢的飞起。
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstdlib>
  4 #include<cstring>
  5 #include<string>
  6 #include<algorithm>
  7 #include<map>
  8 #include<complex>
  9 #include<queue>
 10 #include<stack>
 11 #include<cmath>
 12 #include<set>
 13 #include<vector>
 14 #define RG register
 15 #define mk make_pair
 16 #define pre t[x].fa
 17 #define ls t[x].ch[0]
 18 #define rs t[x].ch[1]
 19 #define maxn 200010
 20 using namespace std;
 21 int tot,ans[maxn/2];
 22 namespace lct{
 23   struct data{int w,fa,ch[2],lazy,zd,sp,bl1,bl2;}t[maxn];
 24   inline bool isrt(int x){return t[pre].ch[0]!=x && t[pre].ch[1]!=x;}
 25   inline bool son(int x){return t[pre].ch[1]==x;}
 26   inline void updata(int x){
 27     t[x].zd=t[x].w,t[x].sp=x;
 28     if(t[ls].zd>t[x].zd) t[x].zd=t[ls].zd,t[x].sp=t[ls].sp;
 29     if(t[rs].zd>t[x].zd) t[x].zd=t[rs].zd,t[x].sp=t[rs].sp;
 30   }
 31   inline void down(int x){
 32     if(t[x].lazy){
 33       swap(ls,rs),t[ls].lazy^=1,t[rs].lazy^=1,t[x].lazy=0;
 34     }
 35   }
 36   void pd(int x){if(!isrt(x))pd(pre);down(x);}
 37   inline void Rotate(int x){
 38     int f=t[x].fa,g=t[f].fa,c=son(x);
 39     if(!isrt(f)) t[g].ch[son(f)]=x;
 40     t[x].fa=g;
 41     t[f].ch[c]=t[x].ch[c^1],t[t[f].ch[c]].fa=f;
 42     t[x].ch[c^1]=f,t[f].fa=x;
 43     updata(f),updata(x);
 44   }
 45   inline void Splay(int x){
 46     pd(x);
 47     for(;!isrt(x);Rotate(x))
 48       if(!isrt(pre)) Rotate(son(pre)==son(x)?pre:x);
 49   }
 50   inline void access(RG int x){
 51     for(int y=0;x;y=x,x=pre) Splay(x),rs=y,updata(x);
 52   }
 53   inline void makert(int x){
 54     access(x),Splay(x),t[x].lazy^=1;
 55   }
 56   inline int findrt(int x){
 57     access(x),Splay(x);
 58     while(ls) down(x),x=ls;
 59     return x;
 60   }
 61   inline void cut(int x,int y){
 62     makert(x),access(y),Splay(y);
 63     t[x].fa=t[y].ch[0]=0;
 64     updata(y);
 65   }
 66   inline void link(RG int x,RG int y,RG int w){
 67     if(findrt(x)==findrt(y)){
 68       makert(x),access(y),Splay(y);
 69       if(t[y].zd<=w) return;
 70       int a1=t[t[y].sp].bl1,a2=t[t[y].sp].bl2,b=t[y].sp;
 71       cut(a1,b);
 72       cut(a2,b);
 73     }
 74     makert(x);t[x].fa=++tot;
 75     t[tot].w=w,makert(tot);
 76     t[tot].fa=y;
 77     t[tot].bl1=x,t[tot].bl2=y;
 78   }
 79   inline void query(RG int x,RG int y){
 80     makert(x),access(y),Splay(y);
 81     ans[++ans[0]]=t[y].zd;
 82   }
 83 };
 84 struct edge{
 85   int x,y,w;
 86 }e[maxn];
 87 struct quest{
 88   int type,x,y;
 89 }q[maxn];
 90 map<pair<int,int>,int>mp;
 91 map<pair<int,int>,bool>mp1;
 92 int main(){
 93   int n,m,qes;
 94   scanf("%d%d%d",&n,&m,&qes);tot=n;
 95   for(int i=1;i<=m;i++)
 96     scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].w),mp[mk(e[i].x,e[i].y)]=mp[mk(e[i].y,e[i].x)]=e[i].w;
 97   for(int i=1;i<=qes;i++){
 98     scanf("%d%d%d",&q[i].type,&q[i].x,&q[i].y);
 99     if(q[i].type==2) mp1[mk(q[i].x,q[i].y)]=1,mp1[mk(q[i].y,q[i].x)]=1;
100   }
101   for(int i=1;i<=m;i++)
102     if(!mp1.count(mk(e[i].x,e[i].y)))
103       lct::link(e[i].x,e[i].y,e[i].w);
104   for(int i=qes;i>=1;i--){
105     if(q[i].type==1) lct::query(q[i].x,q[i].y);
106     else lct::link(q[i].x,q[i].y,mp[mk(q[i].x,q[i].y)]);
107   }
108   for(int i=ans[0];i;i--)
109     printf("%d
",ans[i]);
110   return 0;
111 }
 
原文地址:https://www.cnblogs.com/pantakill/p/7282471.html