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