hdu1213 disjoint-set board

Topic links: http://icpc.njust.edu.cn/Problem/Hdu/1213/

A support disjoint-set data structure to find and merge, the operation in the forest, with path compression, and combined to find the time complexity are almost constant. And check the most basic role is to establish a set of so-called "relationship" between the different points, and the query whether the two are related. And a feature set is check Vectorial transitive relationship, such as A-> B B-> C, there is A-> C, is not particularly between the image transfer vectors? In many cases the application and check the set are related to this property. Chairman and check the code segment comparable set of tree-friendly thing orz hdu1213 board problem, as follows:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef unsigned int ui;
 4 typedef long long ll;
 5 typedef unsigned long long ull;
 6 #define pf printf
 7 #define mem(a,b) memset(a,b,sizeof(a))
 8 #define prime1 1e9+7
 9 #define prime2 1e9+9
10 #define pi 3.14159265
11 #define lson l,mid,rt<<1
12 #define rson mid+1,r,rt<<1|1
13 #define scand(x) scanf("%llf",&x) 
14 #define f(i,a,b) for(int i=a;i<=b;i++)
15 #define scan(a) scanf("%d",&a)
16 #define mp(a,b) make_pair((a),(b))
17 #define P pair<int,int>
18 #define dbg(args) cout<<#args<<":"<<args<<endl;
19 #define inf 0x3f3f3f3f
20 inline int read(){
21     int ans=0,w=1;
22     char ch=getchar();
23     while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}
24     while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
25     return ans*w;
26 }
27 const int maxn=1e6+10;
28 int n,m,t;
29 int f[maxn];
30 set<int> s;
31 void init()
32 {
33     F (I, . 1 , n-) F [I] = I;
 34 is      s.clear ();
 35  }
 36  int Find ( int X)
 37 [  {
 38 is      IF (X == F [X]) return X;
 39      F [ X] = Find (F [X]); // path compressor, or may constitute an extremely uneven tree 
40      return F [X];
 41 is  } 
 42 is  void of Union ( int X, int Y)
 43 is  {
 44 is      int = FX Find (X);
 45      int FY = Find (Y);
 46 is      IF(FX == FY) return ; // has the same tree 
47      the else  
48      {
 49          F [FX] = FY;
 50          // not put a tree in the tree b is located on the root connected the root of the tree, combined to achieve 
51 is       } 
 52 is  }
 53 is  
54 is  int main ()
 55  {
 56 is      // the freopen ( "input.txt", "R & lt", stdin);
 57 is      // the freopen ( "output.txt", "W ", stdout); 
58      STD :: :: iOS sync_with_stdio ( to false );
 59      T = Read ();
 60      int A, B;
 61 is      the while (T--)
 62 is      {
 63 is          n-= Read (), m = Read ();
 64          the init ();
 65          F (I, . 1 , m) A = Read (), B = Read (), of Union (A, B);
 66          F (I, . 1 , n-) s.insert (Find (I)); // see how many independent tree 
67          PF ( " % D \ n- " , s.size ());
 68      }
 69   }

 

Guess you like

Origin www.cnblogs.com/randy-lo/p/12559969.html