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

题解:

裸LCT+离线+二分+MST。。。

代码:(几乎摘抄自hzwer

  1 #include<cstdio>
  2 
  3 #include<cstdlib>
  4 
  5 #include<cmath>
  6 
  7 #include<cstring>
  8 
  9 #include<algorithm>
 10 
 11 #include<iostream>
 12 
 13 #include<vector>
 14 
 15 #include<map>
 16 
 17 #include<set>
 18 
 19 #include<queue>
 20 
 21 #include<string>
 22 
 23 #define inf 1000000000
 24 
 25 #define maxn 1500000+5
 26 
 27 #define maxm 200000+5
 28 
 29 #define eps 1e-10
 30 
 31 #define ll long long
 32 
 33 #define pa pair<int,int>
 34 
 35 #define for0(i,n) for(int i=0;i<=(n);i++)
 36 
 37 #define for1(i,n) for(int i=1;i<=(n);i++)
 38 
 39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
 40 
 41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
 42 
 43 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go)
 44 
 45 #define for5(n,m) for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)
 46 
 47 #define mod 1000000007
 48 
 49 using namespace std;
 50 
 51 inline int read()
 52 
 53 {
 54 
 55     int x=0,f=1;char ch=getchar();
 56 
 57     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 58 
 59     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
 60 
 61     return x*f;
 62 
 63 }
 64 int b[20];
 65 inline void print(int x)
 66 {
 67     for(b[0]=0;x;b[++b[0]]=x%10,x/=10);
 68     for3(i,b[0],1)putchar('0'+b[i]);
 69     putchar('
');
 70 }
 71 int n,m,k,top;
 72 int fa[maxn],f[maxn],c[maxn][2],sta[maxn],mx[maxn],val[maxn],ans[maxn];
 73 struct rec{int u,v,w,id,del;}e[maxn];
 74 bool rev[maxn];
 75 bool operator<(rec a,rec b){return a.u==b.u?a.v<b.v:a.u<b.u;}
 76 inline bool cmp(rec a,rec b){return a.w<b.w;}
 77 inline bool cmp1(rec a,rec b){return a.id<b.id;}
 78 inline int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
 79 inline int find(int u,int v)
 80 {
 81     int l=1,r=m;
 82     while(l<=r)
 83     {
 84         int mid=(l+r)>>1;
 85         if(e[mid].u<u||(e[mid].u==u&&e[mid].v<v))l=mid+1;else r=mid-1;
 86     }
 87     return l;
 88 }
 89 inline bool isroot(int x){return c[fa[x]][0]!=x&&c[fa[x]][1]!=x;}
 90 inline void pushup(int x)
 91 {
 92     int l=c[x][0],r=c[x][1];
 93     mx[x]=x;
 94     if(val[mx[l]]>val[mx[x]])mx[x]=mx[l];
 95     if(val[mx[r]]>val[mx[x]])mx[x]=mx[r];
 96 }
 97 inline void rotate(int x)
 98 {
 99     int y=fa[x],z=fa[y],l=c[y][1]==x,r=l^1;
100     if(!isroot(y))c[z][c[z][1]==y]=x;
101     fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
102     c[y][l]=c[x][r];c[x][r]=y;
103     pushup(y);pushup(x);
104 }
105 inline void update(int x)
106 {
107     rev[x]^=1;
108     swap(c[x][0],c[x][1]);
109 }
110 inline void pushdown(int x)
111 {
112     if(!rev[x])return;
113     rev[x]=0;
114     update(c[x][0]);update(c[x][1]);
115 }
116 inline void splay(int x)
117 {
118     sta[top=1]=x;
119     for(int y=x;!isroot(y);y=fa[y])sta[++top]=fa[y];
120     while(top)pushdown(sta[top--]);
121     while(!isroot(x))
122     {
123         int y=fa[x],z=fa[y];
124         if(!isroot(y))
125         {
126             if((c[z][0]==y)^(c[y][0]==x))rotate(x);else rotate(y);
127         }
128         rotate(x);
129     }
130 }
131 inline void access(int x)
132 {
133     for(int y=0;x;x=fa[x])
134     {
135         splay(x);c[x][1]=y;pushup(x);y=x;
136     }
137 }
138 inline void makeroot(int x)
139 {
140     access(x);splay(x);update(x);
141 }
142 inline void cut(int x,int y)
143 {
144     makeroot(x);access(y);splay(y);c[y][0]=fa[x]=0;
145 }
146 inline void link(int x,int y)
147 {
148     makeroot(x);fa[x]=y;
149 }
150 inline int query(int x,int y)
151 {
152     makeroot(x);access(y);splay(y);return mx[y];
153 }
154 
155 int main()
156 
157 {
158 
159     freopen("input.txt","r",stdin);
160 
161     freopen("output.txt","w",stdout);
162 
163     n=read();m=read();k=read();
164     for1(i,n)f[i]=i;
165     for1(i,m){e[i].u=read();e[i].v=read();e[i].w=read();if(e[i].u>e[i].v)swap(e[i].u,e[i].v);}
166     sort(e+1,e+m+1,cmp);
167     for1(i,m)
168     {
169         e[i].id=i;
170         val[n+i]=e[i].w;
171         mx[n+i]=n+i;
172     }
173     sort(e+1,e+m+1);
174     for1(i,k)
175     {
176         int ch=read();e[m+i].u=read();e[m+i].v=read();
177         if(ch==2)
178         {
179             if(e[m+i].u>e[m+i].v)swap(e[m+i].u,e[m+i].v);
180             int t=find(e[m+i].u,e[m+i].v);
181             e[t].del=1;e[m+i].w=e[t].id;
182         }
183     }
184     sort(e+1,e+m+1,cmp1);
185     int cnt=0;
186     for1(i,m)if(!e[i].del)
187     {
188         int u=e[i].u,v=e[i].v,x=find(u),y=find(v);
189         if(x!=y)
190         {
191             f[y]=x;
192             link(u,i+n);link(v,i+n);
193             if(++cnt==n-1)break;
194         }
195     }
196     for3(i,k,1)
197     {
198         //cout<<i<<endl;
199         if(!e[m+i].w)ans[i]=val[query(e[m+i].u,e[m+i].v)];
200         else
201         {
202             int u=e[m+i].u,v=e[m+i].v,t1=e[m+i].w,t2=query(u,v);
203             if(e[t1].w<val[t2])
204             {
205                 cut(e[t2-n].u,t2);cut(e[t2-n].v,t2);
206                 link(u,t1+n);link(v,t1+n);
207             }
208         }
209     }
210     for1(i,k)if(ans[i])print(ans[i]);    
211 
212     return 0;
213 
214 }  
View Code

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

Time Limit: 25 Sec  Memory Limit: 128 MB
Submit: 1046  Solved: 332
[Submit][Status]

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;
}


HINT

Source



原文地址:https://www.cnblogs.com/zyfzyf/p/4236779.html