水管局长题解

题目描述:

  SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一条从A至B的水管的路径,接着通过信息化的控制中心通知路径上的水管进入准备送水状态,等到路径上每一条水管都准备好了,供水公司就可以开始送水了。嘟嘟一次只能处理一项送水任务,等到当前的送水任务完成了,才能处理下一项。
  在处理每项送水任务之前,路径上的水管都要进行一系列的准备操作,如清洗、消毒等等。嘟嘟在控制中心一声令下,这些水管的准备操作同时开始,但由于各条管道的长度、内径不同,进行准备操作需要的时间可能不同。供水公司总是希望嘟嘟能找到这样一条送水路径,路径上的所有管道全都准备就绪所需要的时间尽量短。嘟嘟希望你能帮助他完成这样的一个选择路径的系统,以满足供水公司的要求。另外,由于MY市的水管年代久远,一些水管会不时出现故障导致不能使用,你的程序必须考虑到这一点。
  不妨将MY市的水管网络看作一幅简单无向图(即没有自环或重边):水管是图中的边,水管的连接处为图中的结点。

输入格式:

  输入文件第一行为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尚未报废的水管一定存在)。

输出格式:

  按顺序对应输入文件中每一项k=1的任务,你需要输出一个数字和一个回车/换行符。该数字表示:你寻找到的水管路径中所有管道全都完成准备工作所需要的时间(当然要求最短)。

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

  删边不好做,就时光倒流,将删边变为加边。

  先将所有操作读入,用Kruskal求出最小生成树。

  每次增加一条边,一定会把这条边连接的两个点间路径上的最小边弹掉,这个可以用LCT维护。

  经过几次Link和Cut,这个问题就解决了。

  时间复杂度$O(nlog_2n)$

Code:

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<algorithm>
  4 #define LL long long
  5 using namespace std;
  6 const int N=1100010;
  7 const int M=1000010;
  8 const LL Q=100010;
  9 const int mo=2333333;
 10 int n,m,q,top;
 11 int rev[N],f[N],ch[N][2],a[N],p[N],st[N],an[Q];
 12 int cu[M];
 13 struct edge{
 14     int u,v,l;
 15 }e[M];
 16 bool comp(const edge a1,const edge a2)
 17 {
 18     return a1.l<a2.l;
 19 }
 20 struct hash_map{
 21     int fi[mo+10],val[mo+10],ne[mo+10];
 22     LL key[mo+10];
 23     int cnt;
 24     void insert(LL x,int y){
 25         LL pos=x%mo;
 26         key[++cnt]=x;val[cnt]=y;
 27         ne[cnt]=fi[pos];fi[pos]=cnt;
 28     }
 29     int find(LL x){
 30         LL pos=x%mo;
 31         for(int i=fi[pos];i;i=ne[i]){
 32             if(key[i]==x) return val[i];
 33         }
 34         return 0;
 35     }
 36 }ha;
 37 struct work{
 38     int id,op,a,b;
 39 }w[Q];
 40 int read()
 41 {
 42     int s=0;char c=getchar();
 43     while(c<'0'||c>'9') c=getchar();
 44     while(c>='0'&&c<='9'){
 45         s=(s<<3)+(s<<1)+c-'0';
 46         c=getchar();
 47     }
 48     return s;
 49 }
 50 int get(int x)
 51 {
 52     return ch[f[x]][1]==x;
 53 }
 54 bool isroot(int x)
 55 {
 56     return ch[f[x]][0]!=x&&ch[f[x]][1]!=x;
 57 }
 58 void pushdown(int x)
 59 {
 60     if(rev[x]){
 61         swap(ch[x][0],ch[x][1]);
 62         rev[ch[x][0]]^=1;
 63         rev[ch[x][1]]^=1;
 64         rev[x]=0;
 65     }
 66 }
 67 void pushup(int x)
 68 {
 69     p[x]=a[x];
 70     if(e[p[ch[x][0]]].l>e[p[x]].l) p[x]=p[ch[x][0]];
 71     if(e[p[ch[x][1]]].l>e[p[x]].l) p[x]=p[ch[x][1]];
 72 }
 73 void rotate(int x)
 74 {
 75     int y=f[x],z=f[y],k=get(x);
 76     if(!isroot(y)){
 77         if(ch[z][0]==y) ch[z][0]=x;
 78         else ch[z][1]=x;
 79     }
 80     f[x]=z;f[y]=x;f[ch[x][k^1]]=y;
 81     ch[y][k]=ch[x][k^1];ch[x][k^1]=y;
 82     pushup(y);pushup(x);
 83 }
 84 void splay(int x)
 85 {
 86     top=0;st[++top]=x;
 87     for(int i=x;!isroot(i);i=f[i]) st[++top]=f[i];
 88     for(int i=top;i>=1;i--) pushdown(st[i]);
 89     while(!isroot(x)){
 90         int y=f[x];
 91         if(!isroot(y)){
 92             if(get(x)==get(y)) rotate(y);
 93             else rotate(x);
 94         }
 95         rotate(x);
 96     }
 97 }
 98 void access(int x)
 99 {
100     int n=0;
101     for(int y=0;x;y=x,x=f[x]){
102         splay(x);ch[x][1]=y;pushup(x);
103     }
104 }
105 void makeroot(int x)
106 {
107     access(x);splay(x);
108     rev[x]^=1;
109 }
110 void split(int x,int y)
111 {
112     makeroot(x);
113     access(y);splay(y);
114 }
115 void link(int x,int y)
116 {
117     makeroot(x);
118     f[x]=y;
119 }
120 void cut(int x,int y)
121 {
122     split(x,y);
123     f[x]=ch[y][0]=0;
124 }
125 int find(int x)
126 {
127     access(x);splay(x);
128     while(ch[x][0]){
129         pushdown(x);
130         x=ch[x][0];
131     }
132     splay(x);
133     return x;
134 }
135 void build_tree()
136 {
137     int tot=0;
138     for(int i=1;i<=m;i++){
139         if(cu[i]==1) continue;
140         if(find(e[i].u)!=find(e[i].v)){
141             link(e[i].u,i+n);
142             link(e[i].v,i+n);
143             tot++;
144         }
145         if(tot==n-1) break;
146     }
147 }
148 int main()
149 {
150     n=read();m=read();q=read();ha.cnt=0;
151     for(int i=1;i<=m;i++){
152         e[i].u=read();e[i].v=read();e[i].l=read();
153         if(e[i].u>e[i].v) swap(e[i].u,e[i].v);
154     }
155     sort(e+1,e+m+1,comp);
156     for(int i=1;i<=m;i++){
157         ha.insert((LL)e[i].u*Q+(LL)e[i].v,i);
158         p[i+n]=a[i+n]=i;
159     }
160     for(int i=1;i<=q;i++){
161         w[i].op=read();w[i].a=read();w[i].b=read();
162         if(w[i].a>w[i].b) swap(w[i].a,w[i].b);
163         if(w[i].op==2){
164             w[i].id=ha.find((LL)w[i].a*Q+(LL)w[i].b);
165             cu[w[i].id]=1;
166         }
167     }
168     build_tree();
169     for(int i=q;i>=1;i--){
170         int x=w[i].a,y=w[i].b;
171         if(w[i].op==1){
172             split(x,y);
173             an[i]=e[p[y]].l;
174         }
175         else{
176             split(x,y);
177             int now=w[i].id;
178             int ed=p[y];
179             if(e[ed].l>e[now].l){
180                 cut(e[ed].u,ed+n);
181                 cut(e[ed].v,ed+n);
182                 link(e[now].u,now+n);
183                 link(e[now].v,now+n);
184             }
185         }
186     }
187     for(int i=1;i<=q;i++){
188         if(w[i].op==1)
189             printf("%d
",an[i]);
190     }
191     return 0;
192 }
View Code
原文地址:https://www.cnblogs.com/hz-Rockstar/p/11573171.html