BZOJ2594:水管局长数据加强版

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

HINT

【原题数据范围】
N ≤ 1000
M ≤ 100000
Q ≤ 100000
测试数据中宣布报废的水管不超过5000条;且任何时候我们考虑的水管网络都是连通的,即从任一结点A必有至少一条水管路径通往任一结点B。
【加强版数据范围】
N ≤ 100000
M ≤ 1000000
Q ≤ 100000
任何时候我们考虑的水管网络都是连通的,即从任一结点A必有至少一条水管路径通往任一结点B。
 
题解:
边上带权的LCT,可以考虑把边看做一个节点,则边权变成了点权。
每次加入时,若形成环,则cut环上最短的边,再link新边(若新边更短,则不操作)。
 
代码:
  1 #include<iostream>
  2 #include<algorithm>
  3 #include<cstdio>
  4 #include<cstring>
  5 using namespace std;
  6 inline int read()
  7 {
  8     int x=0,f=1;char ch=getchar();
  9     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 10     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
 11     return x*f;
 12 }
 13 int n,m,Q,top;
 14 int f[1500005];
 15 int fa[1500005],c[1500005][2],s[1500005];
 16 int mx[1500005],val[1500005];
 17 bool rev[1500005];
 18 struct edge{int u,v,w,id;bool d;}e[1000005];
 19 struct que{int f,x,y,ans,id;}q[100005];
 20 bool operator<(edge a,edge b)
 21 {
 22     return a.u<b.u||(a.u==b.u&&a.v<b.v);
 23 }
 24 bool cmp(edge a,edge b)
 25 {
 26     return a.w<b.w;
 27 }
 28 bool cmp2(edge a,edge b)
 29 {
 30     return a.id<b.id;
 31 }
 32 int getf(int x){return x==f[x]?x:f[x]=getf(f[x]);}
 33 int find(int u,int v)
 34 {
 35     int l=1,r=m;
 36     while(l<=r)
 37     {
 38         int mid=(l+r)>>1;
 39         if(e[mid].u<u||(e[mid].u==u&&e[mid].v<v))l=mid+1;
 40         else if(e[mid].u==u&&e[mid].v==v)return mid;
 41         else r=mid-1;
 42     }
 43 }
 44 bool isroot(int x)
 45 {
 46     return c[fa[x]][0]!=x&&c[fa[x]][1]!=x;
 47 }
 48 void update(int x)
 49 {
 50     int l=c[x][0],r=c[x][1];
 51     mx[x]=x;
 52     if(val[mx[l]]>val[mx[x]])mx[x]=mx[l];
 53     if(val[mx[r]]>val[mx[x]])mx[x]=mx[r];
 54 }
 55 void rotate(int x)
 56 {
 57     int y=fa[x],z=fa[y],l,r;
 58     if(c[y][0]==x)l=0;else l=1;r=l^1;
 59     if(!isroot(y))
 60     {
 61         if(c[z][0]==y)c[z][0]=x;else c[z][1]=x;
 62     }
 63     fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
 64     c[y][l]=c[x][r];c[x][r]=y;
 65     update(y);update(x);
 66 }
 67 void pushdown(int x)
 68 {
 69     int l=c[x][0],r=c[x][1];
 70     if(rev[x])
 71     {
 72         rev[x]^=1;
 73         rev[l]^=1;rev[r]^=1;
 74         swap(c[x][0],c[x][1]);
 75     }
 76 }
 77 void splay(int x)
 78 {
 79     top=0;s[++top]=x;
 80     for(int i=x;!isroot(i);i=fa[i])
 81         s[++top]=fa[i];
 82     for(int i=top;i;i--)
 83         pushdown(s[i]);
 84     while(!isroot(x))
 85     {
 86         int y=fa[x],z=fa[y];
 87         if(!isroot(y))
 88         {
 89             if(c[y][0]==x^c[z][0]==y)rotate(x);
 90             else rotate(y);
 91         }
 92         rotate(x);
 93     }
 94 }
 95 void access(int x)
 96 {
 97     int t=0;
 98     while(x)
 99     {
100         splay(x);c[x][1]=t;update(x);t=x;x=fa[x];
101     }
102 }
103 void makeroot(int x)
104 {
105     access(x);splay(x);rev[x]^=1;
106 }
107 void link(int x,int y)
108 {
109     makeroot(x);fa[x]=y;
110 }
111 void cut(int x,int y)
112 {
113     makeroot(x);access(y);splay(y);c[y][0]=fa[x]=0;
114 }
115 int query(int x,int y)
116 {
117     makeroot(x);access(y);splay(y);return mx[y];
118 }
119 int main()
120 {
121     n=read();m=read();Q=read();
122     for(int i=1;i<=n;i++)f[i]=i;
123     for(int i=1;i<=m;i++)
124     {
125         e[i].u=read(),e[i].v=read(),e[i].w=read();
126         if(e[i].u>e[i].v)swap(e[i].u,e[i].v);
127     }
128     sort(e+1,e+m+1,cmp);
129     for(int i=1;i<=m;i++)
130     {
131         e[i].id=i;
132         val[n+i]=e[i].w;    
133         mx[n+i]=n+i;
134     }
135     sort(e+1,e+m+1);
136     for(int i=1;i<=Q;i++)
137     {
138         q[i].f=read(),q[i].x=read(),q[i].y=read();
139         if(q[i].f==2)
140         {
141             if(q[i].x>q[i].y)swap(q[i].x,q[i].y);
142             int t=find(q[i].x,q[i].y);
143             e[t].d=1;q[i].id=e[t].id;
144         }
145     }
146     sort(e+1,e+m+1,cmp2);
147     int tot=0;
148     for(int i=1;i<=m;i++)
149         if(!e[i].d)
150         {
151             int u=e[i].u,v=e[i].v,x=getf(u),y=getf(v);
152             if(x!=y)
153             {
154                 f[x]=y;
155                 link(u,i+n);link(v,i+n);
156                 tot++;
157                 if(tot==n-1)break;
158             }
159         }
160     for(int i=Q;i;i--)
161     {
162         if(q[i].f==1)
163             q[i].ans=val[query(q[i].x,q[i].y)];
164         else
165         {
166             int u=q[i].x,v=q[i].y,k=q[i].id;
167                 int t=query(u,v);
168                 if(e[k].w<val[t])
169                 {
170                     cut(e[t-n].u,t);cut(e[t-n].v,t);
171                     link(u,k+n);link(v,k+n);
172              
173             }    
174         }
175     }
176     for(int i=1;i<=Q;i++)
177         if(q[i].f==1)printf("%d\n",q[i].ans);
178     return 0;
179 }
View Code
posted @ 2017-02-13 19:51  GhoStreach  阅读(227)  评论(0编辑  收藏  举报