"BZOJ-4668" Cold War and search set + heuristic merger + promiscuity

Source: Internet
Author: User

4668: Cold War time limit:10 Sec Memory limit:256 MB
Submit:37 solved:24
[Submit] [Status] [Discuss] Description1946 March 5, the former British prime Minister Winston Churchill in the United States, Fulton published "Iron Curtain Speech" formally opened the Cold War prologue. The United States and the Soviet Union were the world's "superpowers", and the two countries and their allies fought for decades to fight for world hegemony. During this period, despite the serious differences and conflicts, both sides have endeavoured to avoid the outbreak of large-scale war worldwide (World War III), whose confrontation is usually carried out by "cold" means of local agency warfare, science and technology and arms race, space competition, diplomatic competition, namely "mutual containment, non-force", so called "Cold War". Reddington is the admiral of the United States. Because of the tense situation in the war, he needed to keep an eye on the activities of the Soviet Union and avoid getting his country into trouble. The Soviet Union had N military plants all over the world, but because of the improper planning, there was no railroad between the plants at first, and in order to make weapons faster, the Soviets decided to build several roads that would allow some military factories to connect. Reddington was given a schedule to build the Soviet Union, and he needed to keep an eye on whether or not a certain two military plants were connected, and when the road was first built. In particular, there are now a total of M operations, the operation is divided into two categories: 0 u V, this operation of the Soviet Union will build a connection to the U-Military plant and the railway of the V plant, the railway is two-way; 1 U V, Reddington need to know the U military Plant and the V plant were added to the first few iron After the road will be unicom, if the operation is not connected, then output 0; As America's strongest scientist, Reddington needs you to help design a program that meets his requirements. Input first line two integers N, M. Next M line, each behavior 0 u v or 1 u v form. The data is encrypted, for each edge or query, the true U, V is equal to the read-in U, v XOR or the answer to the previous query. At first this value is 0. 1≤n, m≤500000, decrypted u, v satisfies 1≤u, v≤n, U is not equal to voutput for each 1 operation, output U, v the first to join which side will be unicom, if the operation is not connected, then output 0. Sample Input5 9
0 1 4
1 2 5
0 2 4
0 3 4
1 3 1
0 7 0
0 6 1
0 1 6
1 2 6Sample Output0
3
5Hintsourcesolution

To do a good job on the problem ....

Maintain connectivity, obviously consider and check the set, for the earliest time to inquire, obviously can not be naked and check set, but can also maintain

A better understanding of the idea:

Records all the points in the collection, merging the information from the two sets when merging.

Reduce complexity? Can be analogous to the splay of the heuristic merge, each time the small merge into the big inside

So each set, record the elements of the Unicom, and the corresponding time can ... Dynamic memory, you can use vector

For a query, if unicom, two minutes time can

and check the normal path compression can be connected to the state of the edge, it will be better to write

LCT can also Qaq

Code
#include <iostream>#include<algorithm>#include<cmath>#include<cstring>#include<cstdio>#include<vector>using namespaceStd;inlineintRead () {intx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; Ch=GetChar ();}  while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*F;}#defineMAXN 500010intN,m,dfn,st[maxn],top,last;structUnionfindnode {intFa,sz; vector<int>Time,data;} UF[MAXN];structedgenode{intNext,to;} edge[maxn<<2];intHead[maxn],cnt=1; inlinevoidAddedge (intUintV) {cnt++; edge[cnt].next=head[u]; head[u]=cnt; edge[cnt].to=v;} InlinevoidInsert (intUintv) {Addedge (u,v); Addedge (v,u);} InlinevoidDFS (intNowintLast ) {st[++top]=Now ;  for(intI=head[now]; I I=edge[i].next)if(edge[i].to!=Last ) DFS (Edge[i].to,now);}structunionfind{InlinevoidInit () { for(intI=1; i<=n; i++) Uf[i]. fa=i,uf[i].sz=1, Uf[i].data.push_back (i), Uf[i].time.push_back (0);} InlineintFind (intx) {if(Uf[x]. FA==X)returnXElse returnUF[X]. Fa=Find (Uf[x].    FA);} InlinevoidUnion (intXintY//split x insert y{DFS (x,0); intFa=Find (y), now;  while(top) now=st[top--],uf[now]. fa=fa,uf[fa].sz++, Uf[now].data.push_back (FA), Uf[now].time.push_back (DFN);        Insert (x, y); } InlinevoidMerge (intXinty) {intF1=find (x), f2=Find (y); DFN++; if(F1!=F2)if(UF[F1].SZ&LT;UF[F2].SZ) Union (x, y);ElseUnion (y,x); Else return; } InlineintGetTime (intXintT) {returnUf[x].time[uf[x].time.size ()-T];} InlineintGetData (intXintP) {returnUf[x].data[uf[x].data.size ()-p];} InlineintGetans (intXintYintT) {returnMax (GetTime (x,t), GetTime (y,t));}} UF;BOOLCheck (intXintYintP) {returnuf. GetData (x,p) = =uf. GetData (y,p);} InlinevoidAsk (intXinty) {    if(UF. Find (x)!=uf. Find (y)) {printf ("%d\n", last=0);return;} intL=2, r=min (uf[x].data.size (), uf[y].data.size ());  while(l<=r) {intMid= (l+r) >>1; if(Check (x,y,mid)) l=mid+1;Elser=mid-1; } printf ("%d\n", Last=uf. Getans (x,y,l-1));}intMain () {N=read (), m=read ();    Uf.init ();  for(intI=1; i<=m; i++)        {            intOpt=read (), U=read (), v=read (); U^=last; v^=Last ; if(opt==1) Ask (U,V); if(opt==0) uf.        Merge (U,V); }    return 0;}

For the history of the treatment of the science students I, these history incredibly remember!!!

"BZOJ-4668" Cold War and search set + heuristic merger + promiscuity

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.