Secretary enhanced data pipes: lct, back in time, minimum spanning tree, side of the point

Description:

SC MY province city has a huge network of underground pipes, water pipes Secretary MY toot City (water pipe is the pipe anyway), is toot work as director of water: water company every day may want to send a certain amount of water from at x to y, the toot need to find a path from a to B in the water supply pipe company, then notifies the pipe water supply path into the preparation state by the control center of the information on each path until the water are ready, water the company can start a bottled water. Toot can only handle one task bottled water, bottled water until the current task is completed, in order to deal with the next one.
Prior to processing each task and bottled water, the water on the path to be a series of preparatory operations, such as cleaning, disinfecting and so on. Beep at the control center order, these pipe preparation operation started simultaneously, but since the length of the pieces of pipe, different internal diameters, the time required to perform preparatory operations may differ. Water companies always want to toot water to find such a path, all the pipes on the path all the time needed to ready as short as possible. Dudu hope you can help him to complete such a system a choice of paths to meet the water requirements of the company. In addition, due to the city's water pipes MY age, some of the water from time to time lead to failure can not be used, your program must take this into account.
City water pipe may wish to MY network as a simple undirected graph (i.e., not from edge ring or weight): water is the edges in the graph, the pipe is connected to the node of FIG.

Portal .
 
Still did not they want to come out, LCT question really is not general.
The third did the title back in time, have not thought of, but also do not have to write a solution to a problem, it is necessary to accumulate some ideas.
 
Back in time is that when a certain kind of positive operating well be (and often is inserted into the deletion of the data structure can not be deleted)
Read in reverse, reverse inquiry process, the skills become delete insert operation.
 
To this question, for example, we want to maintain a minimum spanning tree, the process which will remove some edges.
We will find it impossible to find alternatives to one side in the complexity of the acceptable, deletion of complexity is not guaranteed.
 
So we consider the clock back, to deal with the last one to ask, this time all the sides are not deleted in the minimum spanning tree inside.
So we ran to the edge of the inquiry does not appear in a minimum spanning tree is the answer.
However Border Erase operation becomes to add back the deleted edge is unlocked, or adding an edge.
Adding words like that, and we just inquire about the maximum edge weight between two endpoints in the current Spanning Tree Lane.
If the query result is greater than the right side to join, then delete the most useless piece of edges in a spanning tree where the new can be added to this edge.
Otherwise, this new edge is useless without action.
 
Embodied words: even edge, plus side, the right side of the largest chain of investigation and the corresponding number.
With lct achieve on the line. Details have yet to take the edge ah hash_map mapping what is relatively simple.
 1 #include<cstdio>
 2 #include<queue>
 3 using namespace std;
 4 #define lc c[p][0]
 5 #define rc c[p][1]
 6 int f[1200005],c[1200005][2],n,m,q,lz[1200005],mx[1200005],mxp[1200005],v[1200005];
 7 int sta[1200005],A[1000005],B[1000005],V[1000005],opt[100005],x[100005],y[100005];
 8 int lck[1000005],ans[1000005];
 9 struct hashmap{
10     #define mod 2000003
11     int fir[2000005],l[2000005],x[2000005],y[2000005],v[2000005],cnt;
12     int &operator[](pair<int,int>p){
13         int xx=p.first,yy=p.second;
14         if(xx<yy)xx^=yy^=xx^=yy;
15         long long hsh=(xx*19733737ll+yy)%mod;
16         for(int i=fir[hsh];i;i=l[i])if(x[i]==xx&&y[i]==yy)return v[i];
17         l[++cnt]=fir[hsh];fir[hsh]=cnt;x[cnt]=xx;y[cnt]=yy;return v[cnt];
18     }
19 }mm;
20 void read(int &p,register char ch=getchar()){
21     while(ch>'9'||ch<'0')ch=getchar();
22     while(ch<='9'&&ch>='0')p=(p<<3)+(p<<1)+ch-48,ch=getchar();
23 }
24 bool not_root(int p){return c[f[p]][0]==p||c[f[p]][1]==p;}
25 void rev(int p){lc^=rc^=lc^=rc,lz[p]^=1;}
26 void up(int p){
27     if(mx[lc]>mx[rc])mx[p]=mx[lc],mxp[p]=mxp[lc];
28     else mx[p]=mx[rc],mxp[p]=mxp[rc];
29     if(v[p]>mx[p])mx[p]=v[p],mxp[p]=p;
30 }
31 void down(int p){if(lz[p])rev(lc),rev(rc),lz[p]=0;}
32 void rotate(int p){
33     int fa=f[p],gr=f[fa],k=c[fa][1]==p,br=c[p][!k];
34     if(not_root(fa))c[gr][c[gr][1]==fa]=p; c[p][!k]=fa; c[fa][k]=br;
35     f[p]=gr; f[fa]=p; f[br]=fa; up(fa);
36 }
37 void splay(int p){
38     int res=p,top=0;sta[++top]=p;
39     while(not_root(res))sta[++top]=res=f[res];
40     while(top)down(sta[top--]);
41     while(not_root(p)){
42         int fa=f[p],gr=f[fa];
43         if(not_root(fa))rotate((c[fa][1]==p)^(c[gr][1]==fa)?fa:p);
44         rotate(p);
45     }
46     up(p);
47 }
48 void access(int p){for(int y=0;p;p=f[y=p])splay(p),rc=y,up(p);}
49 void make_root(int p){access(p);splay(p);rev(p);}
50 void split(int x,int y){make_root(x);access(y);splay(y);}
51 void link(int x,int y){make_root(x);f[x]=y;up(y);}
52 void cut(int x,int y){split(x,y);f[x]=c[y][0]=0;up(y);}
53 struct edge{
54     int a,b,l,num;
55     friend bool operator<(edge a,edge b){return a.l>b.l;}
56 }e[1000005];
57 priority_queue<edge>qu;
58 int fa[100005];
59 int find(int k){return fa[k]==k?k:fa[k]=find(fa[k]);}
60 void Kruscal(){
61     for(int i=1;i<=n;++i)fa[i]=i;
62     for(int i=1;i<=m;++i)if(!lck[n+i])qu.push((edge){A[i],B[i],V[i],n+i});
63     while(!qu.empty()){
64         int a=qu.top().a,b=qu.top().b,v=qu.top().l,num=qu.top().num;qu.pop();
65         if(find(a)!=find(b))fa[fa[a]]=fa[b],link(a,num),link(num,b);
66     }
67 }
68 int main(){
69     scanf("%d%d%d",&n,&m,&q);
70     for(int i=1;i<=m;++i)read(A[i]),read(B[i]),read(V[i]),mm[make_pair(A[i],B[i])]=n+i,v[n+i]=V[i];
71     for(int i=1;i<=q;++i)read(opt[i]),read(x[i]),read(y[i]),opt[i]--;
72     for(int i=1;i<=q;++i)if(opt[i])lck[mm[make_pair(x[i],y[i])]]=1;
73     Kruscal();
74     for(int i=q;i;--i)
75         if(opt[i]){
76             split(x[i],y[i]);int nmx=mx[y[i]],tbc=mxp[y[i]],obj=mm[make_pair(x[i],y[i])];
77             if(nmx<=V[obj-n])continue;
78             cut(tbc,A[tbc-n]);cut(tbc,B[tbc-n]);
79             link(obj,x[i]);link(obj,y[i]);
80         }else split(x[i],y[i]),ans[i]=mx[y[i]];
81     for(int i=1;i<=q;++i)if(!opt[i])printf("%d\n",ans[i]);
82 }
View Code

 

 
 
 
 
 
 
 

Guess you like

Origin www.cnblogs.com/hzoi-DeepinC/p/11567261.html