1. 程式人生 > >bzoj2594: [Wc2006]水管局長數據加強版

bzoj2594: [Wc2006]水管局長數據加強版

date get push roo 表示 sort 選擇 mit 管道

地址:http://www.lydsy.com/JudgeOnline/problem.php?id=2594

題目:

2594: [Wc2006]水管局長數據加強版

Time Limit: 25 Sec Memory Limit: 128 MB
Submit: 3770 Solved: 1194
[Submit][Status][Discuss]

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

吐槽:   代碼一小時,debug四小時,寫錯一個變量,花式re,TLE,MLE,wa。   最後還是靠換成fread的fastio卡過去的。   總結,老年人不適合寫代碼。 思路:   動態維護最小生成樹,直接處理TLE,因為邊太多了。   把詢問拿下來逆序處理,要刪的邊先標記,把其他邊跑個最小生成樹。   然後倒著處理詢問,這樣所有操作都是向最小生成樹上加邊。   根據kruskal的思想,如果要加入的邊的端點不在同一並查集中,直接連邊。   在同一集合中的話,加邊後會成環,此時把環上最大一條邊刪除即可。
  lct怎麽處理邊權?把第i條邊當做第i+n個點即可。
  1 /**************************************************************
  2     Problem: 2594
  3     User: weeping
  4     Language: C++
  5     Result: Accepted
  6     Time:25656 ms
  7     Memory:103836 kb
  8 ****************************************************************/
  9  
 10 #include <bits/stdc++.h>
 11  
 12 using namespace std;
 13 namespace fastIO{
 14     #define BUF_SIZE 500000
 15     bool IOerror=0;
 16     inline char nc(){
 17         static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
 18         if(p1==pend){
 19             p1=buf;
 20             pend=buf+fread(buf,1,BUF_SIZE,stdin);
 21             if(pend==p1){
 22                 IOerror=1;
 23                 return -1;
 24             }
 25         }return *p1++;
 26     }
 27     inline bool blank(char ch){
 28         return ch== ||ch==\n||ch==\r||ch==\t;
 29     }
 30     inline bool read(int &x){
 31         char ch;
 32         while(blank(ch=nc()));
 33         if(IOerror)return 0;
 34         for(x=ch-0;(ch=nc())>=0&&ch<=9;x=x*10+ch-0);
 35         return 1;
 36     }
 37     #undef BUF_SIZE
 38 };
 39  
 40 using namespace fastIO;
 41  
 42 struct node
 43 {
 44     int u,v,w,f;
 45     node(){}
 46     node(int x,int y,int z){u=x,v=y,w=z;}
 47 }eg[1000007],qs[100007];
 48 int n,m,q,f[100007],vis[1000007],ans[100007];
 49 map<pair<int,int>,int>hs;
 50  
 51 struct Link_Cut_Tree
 52 {
 53     static const int MAXN = 1200000 + 7;
 54  
 55     int ch[MAXN][2], fa[MAXN], rev[MAXN], sz[MAXN], v[MAXN], mx[MAXN], id[MAXN];
 56     int sk[MAXN];
 57  
 58     bool isroot(int x)
 59     {
 60         return ch[fa[x]][0] != x && ch[fa[x]][1] != x;
 61     }
 62  
 63     void reverse(int x)
 64     {
 65         rev[x] ^= 1, swap(ch[x][0],ch[x][1]);
 66     }
 67  
 68     void update(int x)
 69     {
 70         int lc = ch[x][0], rc = ch[x][1];
 71         if(mx[lc]>mx[rc])
 72             mx[x]=mx[lc],id[x]=id[lc];
 73         else
 74             mx[x]=mx[rc],id[x]=id[rc];
 75         if(v[x]>mx[x])
 76             mx[x]=v[x],id[x]=x;
 77     }
 78  
 79     void push_down(int x)
 80     {
 81         if(!rev[x]) return ;
 82         if(ch[x][0]) reverse(ch[x][0]);
 83         if(ch[x][1]) reverse(ch[x][1]);
 84         rev[x]=0;
 85     }
 86  
 87     void rotate(int x)
 88     {
 89         int f = fa[x], gf = fa[f];
 90         int t1 = ( x != ch[f][0]), t2 = ( f != ch[gf][0]), tmp = ch[x][1^t1];
 91         if(!isroot(f)) ch[gf][0^t2] = x;
 92         fa[tmp] = f, fa[x] = gf, ch[x][1^t1] = f, fa[f] = x, ch[f][0^t1] = tmp;
 93         update(f);
 94     }
 95  
 96     void splay(int x)
 97     {
 98         int top = 0;
 99         sk[++top] = x;
100         for(int i = x; !isroot(i); i = fa[i])   sk[++top] = fa[i];
101         while(top)  push_down(sk[top--]);
102         for(int f = fa[x], gf = fa[f]; !isroot(x); rotate(x), f = fa[x],gf = fa[f])
103         if(!isroot(f))
104             rotate((x==ch[f][0]) ^ (f==ch[gf][0]) ? x : f);
105         update(x);
106     }
107  
108     void access(int x)
109     {
110         for(int p = 0; x; p = x, x = fa[x])
111             splay(x), ch[x][1] = p, update(x);
112     }
113  
114     void makeroot(int x)
115     {
116         access(x), splay(x), reverse(x);
117     }
118  
119     int findroot(int x)
120     {
121         access(x), splay(x);
122         while(ch[x][0]) x = ch[x][0];
123         return x;
124     }
125     void link(int x,int y)
126     {
127         makeroot(x), fa[x] = y;
128     }
129  
130     void cut(int x,int y)
131     {
132         makeroot(x), access(y), splay(y);
133         if(ch[y][0] == x)   ch[y][0] = fa[x] = 0;
134         update(y);
135     }
136  
137     int go(int op,int x,int y,int hid)
138     {
139         if(x>y) swap(x,y);
140         if(op==1)
141         {
142             makeroot(x),access(y),splay(y);
143             return mx[y];
144         }
145         if(findroot(x)!=findroot(y))
146            link(x,hid+n),link(hid+n,y);
147         else
148         {
149             makeroot(x),access(y),splay(y);
150             if(mx[y]>eg[hid].w)
151             {
152                 int pp=id[y]-n;
153                 cut(eg[pp].u,pp+n),cut(eg[pp].v,pp+n);
154                 link(x,hid+n),link(hid+n,y);
155             }
156         }
157         return 0;
158     }
159     void debug(void)
160     {
161         for(int i=1;i<=100;i++)
162             printf("%d %d %d %d %d %d %d\n",i,fa[i],ch[i][0],ch[i][1],rev[i],sz[i]);
163     }
164 }lct;
165  
166 int tid[1000007];
167 bool cmp(const int &ta,const int &tb)
168 {
169     return eg[ta].w<eg[tb].w;
170 }
171 int fd(int x)
172 {
173     return f[x]==x?x:f[x]=fd(f[x]);
174 }
175 void mintree(void)
176 {
177     for(int i=1;i<=n;i++) f[i]=i;
178     for(int i=1;i<=m;i++) tid[i]=i;
179     sort(tid+1,tid+1+m,cmp);
180     for(int i=1;i<=m;i++)
181     {
182         int p=tid[i];
183         if(vis[p]) continue;
184         int fa=fd(eg[p].u),fb=fd(eg[p].v);
185         if(fa==fb)  continue;
186         f[fa]=fb,lct.link(eg[p].u,p+n),lct.link(eg[p].v,p+n);
187     }
188 }
189 int main(void)
190 {
191     //freopen("in.acm","r",stdin);
192     read(n),read(m),read(q);
193     for(int i=0;i<=n;i++) lct.v[i]=-1;
194     for(int i=1,x,y,z;i<=m;i++)
195     {
196         read(x),read(y),read(z),eg[i]=node(x,y,z);
197         if(x>y)swap(x,y);
198         hs[make_pair(x,y)]=i;
199         lct.v[i+n]=lct.mx[i+n]=z,lct.id[i+n]=i+n;
200     }
201     for(int i=1,op,x,y;i<=q;i++)
202     {
203         read(op),read(x),read(y),qs[i]=node(op,x,y);
204         if(x>y)swap(x,y);
205         qs[i].f=hs[make_pair(x,y)];
206         if(op==2)
207             vis[qs[i].f]=1;
208     }
209     mintree();
210     for(int i=q;i;i--)
211         ans[i]=lct.go(qs[i].u,qs[i].v,qs[i].w,qs[i].f);
212     for(int i=1;i<=q;i++)
213     if(qs[i].u==1)
214         printf("%d\n",ans[i]);
215     return 0;
216 }

bzoj2594: [Wc2006]水管局長數據加強版