Bzoj 4668: The Cold War

Source: Internet
Author: User

Description

On a diagram, connect one edge between two points and ask when the two points first unicom.

Sol

and search set + heuristic merge.

Combined by rank and check set ... I do not know what is combined by rank, anyway, similar to the heuristic merge, when the merger will be small to large satowa, because each growth is a small set multiples of more than twice times, so the number of layers not more than \ (log n\)

And then even when the edge of the record is the number of connections, statistical depth, the final violence to find LCA ...

Code
/************************************************************** problem:4668 User:beiyu language:c++ Result : Accepted time:1256 ms memory:9104 kb****************************************************************/#include &lt ; bits/stdc++.h>using namespace Std; const int N = 5E5+50;    int n,lst;struct uniontable {int f[n],s[n],v[n],d[n];    void init (int n) {for (int i=1;i<=n;i++) f[i]=i,v[i]=0,s[i]=1;            } int find (int x) {if (f[x]^x) {int fa=find (f[x]);            d[x]=d[f[x]]+1;        return FA;    }else return x;        } void Union (int x,int y,int c) {int f1=find (x), F2=find (y);        if (F1==F2) return;        if (S[f1]>s[f2]) F[F2]=F1,V[F2]=C,S[F1]+=S[F2];    else F[F1]=F2,V[F1]=C,S[F2]+=S[F1];        } int Query (int x,int y) {int f1=find (x), F2=find (y), r=0;        if (F1!=F2) return 0; for (; x^y;)            {if (D[x]<d[y]) swap (x, y);        R=max (R,v[x]), x=f[x];    }return R; }}un;    inline int in (int x=0,char Ch=getchar ()) {while (ch> ' 9 ' | | ch< ' 0 ') Ch=getchar (); while (ch>= ' 0 ' && ch<= ' 9 ') x=x*10+ch-' 0 ', Ch=getchar (); return x;    }int Main () {n=in ();    Un.init (n); for (int m=in (), c=0;m--;)        {int opt=in (), U=in () ^lst,v=in () ^lst; if (!opt) un.        Union (U,V,++C); else printf ("%d\n", Lst=un.    Query (u,v)); }return 0;}

Bzoj 4668: The Cold War

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.