bzoj2594 [Wc2006]水管局长数据加强版

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

2
3

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

【加强版数据范围】
N ≤ 100000
M ≤ 1000000
Q ≤ 100000
任何时候我们考虑的水管网络都是连通的,即从任一结点A必有至少一条水管路径通往任一结点B。

【C/C++选手注意事项】
由于此题输入规模较大(最大的测试点约20MB),因此即使使用scanf读入数据也会花费较多的时间。为了节省读入耗时,建议使用以下函数读入正整数(返回值为输入文件中下一个正整数):
int getint()
{
char ch = getchar();
for ( ; ch > '9' || ch < '0'; ch = getchar());
int tmp = 0;
for ( ; '0' <= ch && ch <= '9'; ch = getchar())
tmp = tmp * 10 + int(ch) - 48;
return tmp;
}

 

正解:LCT维护动态最小生成树。

这题就是要动态维护经过x到y的路径的最大值的最小值。那么我们可以先考虑离线做法,即倒着操作,因为删边以后没办法维护最值了。一开始先把没有限制的边加上,然后倒着询问时直接把边加上就行了。不难看出这题是要维护最小生成树,那么当加入一条边时如果两个点已经连通,就要把连通他们的最大的边删掉。所以这道题可以用LCT来动态维护添边删边的操作。于是这题就能解决了(不过加强版卡空间woc)。

 

  1 //It is made by wfj_2048~
  2 #include <algorithm>
  3 #include <iostream>
  4 #include <cstring>
  5 #include <cstdlib>
  6 #include <cstdio>
  7 #include <vector>
  8 #include <cmath>
  9 #include <queue>
 10 #include <stack>
 11 #include <map>
 12 #include <set>
 13 #define N (1100010)
 14 #define inf (1<<30)
 15 #define il inline
 16 #define RG register
 17 #define ll long long
 18 #define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
 19 
 20 using namespace std;
 21 
 22 map <int,int> hsh[N];
 23 
 24 struct edge{ int x,y,w; }g[N],q[N];
 25 
 26 int ch[N][2],fa[N],mx[N],sum[N],val[N],lazy[N],st[N],vis[N],ans[N],n,m,Q;
 27 
 28 il int gi(){
 29     RG int x=0,q=1; RG char ch=getchar(); while ((ch<'0' || ch>'9') && ch!='-') ch=getchar();
 30     if (ch=='-') q=-1,ch=getchar(); while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar(); return q*x;
 31 }
 32 
 33 il int cmp(const edge &a,const edge &b){ return a.w<b.w; }
 34 
 35 il void pushdown(RG int x){ lazy[x]=0,lazy[ch[x][0]]^=1,lazy[ch[x][1]]^=1,swap(ch[x][0],ch[x][1]); return; }
 36 
 37 il void pushup(RG int x){
 38     RG int t=mx[ch[x][mx[ch[x][1]]>mx[ch[x][0]]]];
 39     mx[x]=(val[t]<val[x] ? x : t); return;
 40 }
 41 
 42 il int isroot(RG int x){ return ch[fa[x]][0]!=x && ch[fa[x]][1]!=x; }
 43 
 44 il void rotate(RG int x){
 45     RG int y=fa[x],z=fa[y],k=ch[y][0]==x; if (!isroot(y)) ch[z][ch[z][1]==y]=x; fa[x]=z;
 46     ch[y][k^1]=ch[x][k],fa[ch[x][k]]=y,ch[x][k]=y,fa[y]=x,pushup(y),pushup(x); return;
 47 }
 48 
 49 il void splay(RG int x){
 50     RG int top=0; st[++top]=x;
 51     for (RG int i=x;!isroot(i);i=fa[i]) st[++top]=fa[i];
 52     for (RG int i=top;i;--i) if (lazy[st[i]]) pushdown(st[i]);
 53     while (!isroot(x)){
 54     RG int y=fa[x],z=fa[y];
 55     if (!isroot(y)){
 56         if ((ch[z][0]==y)^(ch[y][0]==x)) rotate(x); else rotate(y);
 57     }
 58     rotate(x);
 59     }
 60     return;
 61 }
 62 
 63 il void access(RG int x){ RG int t=0; while (x) splay(x),ch[x][1]=t,pushup(x),t=x,x=fa[x]; return; }
 64 
 65 il void makeroot(RG int x){ access(x),splay(x),lazy[x]^=1; return; }
 66 
 67 il void link(RG int x,RG int y){ makeroot(x),fa[x]=y; return; }
 68 
 69 il void cut(RG int x,RG int y){ makeroot(x),access(y),splay(y),ch[y][0]=fa[x]=0; return; }
 70 
 71 il int querymax(RG int x,RG int y){ makeroot(x),access(y),splay(y); return mx[y]; }
 72 
 73 il int find(RG int x){ access(x),splay(x); while (ch[x][0]) x=ch[x][0]; return x; }
 74 
 75 il void work(){
 76     n=gi(),m=gi(),Q=gi(); RG int top=0;
 77     for (RG int i=1;i<=m;++i){ g[i].x=gi(),g[i].y=gi(),g[i].w=gi(); if (g[i].x>g[i].y) swap(g[i].x,g[i].y); }
 78     sort(g+1,g+m+1,cmp); RG int k=0;
 79     for (RG int i=1;i<=m;++i){ val[n+i]=g[i].w; if (g[i].x>g[i].y) swap(g[i].x,g[i].y); hsh[g[i].x][g[i].y]=i; }
 80     for (RG int i=1;i<=Q;++i){
 81     q[i].w=gi(),q[i].x=gi(),q[i].y=gi();
 82     if (q[i].x>q[i].y) swap(q[i].x,q[i].y);
 83     if (q[i].w==2) vis[hsh[q[i].x][q[i].y]]=1;
 84     }
 85     for (RG int i=1;i<=m;++i){
 86     if (find(g[i].x)!=find(g[i].y) && !vis[hsh[g[i].x][g[i].y]]) link(g[i].x,n+i),link(g[i].y,n+i),k++;
 87     if (k==n-1) break;
 88     }
 89     for (RG int i=Q;i;--i)
 90     if (q[i].w==2){
 91         if (find(q[i].x)==find(q[i].y)){
 92         RG int j=querymax(q[i].x,q[i].y); k=hsh[q[i].x][q[i].y];
 93         if (val[j]>val[n+k]){
 94             cut(g[j-n].x,j),cut(g[j-n].y,j);
 95             link(q[i].x,n+k),link(q[i].y,n+k);
 96         }
 97         }else link(q[i].x,n+i),link(q[i].y,n+i);
 98     }else ans[++top]=val[querymax(q[i].x,q[i].y)];
 99     for (RG int i=top;i;--i) printf("%d\n",ans[i]); return;
100 }
101 
102 int main(){
103     File("pipe");
104     work();
105     return 0;
106 }

 

posted @ 2017-02-17 08:52  wfj_2048  阅读(162)  评论(0编辑  收藏  举报