1 并查集: 2 #include<utility> 3 #include<iostream> 4 #include<set> 5 #include<map> 6 #include<vector> 7 #include<queue> 8 #include<cmath> 9 #include<algorithm> 10 const int MAX_E = 10001; 11 const int MAX_N = 10000; 12 const int INF = 999999; 13 using namespace std; 14 //先搞并查集: 15 int FA [MAX_N]; 16 int Rank [MAX_N]; . 17 // total of n elements 18 // initializes size n disjoint-set . 19 void the init ( int n) { 20 is for ( int I = 0 ; I < n-; I ++ ) { 21 is FA [I] = I; 22 is } 23 is } 24 // root of the query tree 25 int Find ( int X) { 26 is // this is written using compression path 27 return X FA == [ ? X] X: FA [X] = Find (FA [X]); 28 // If not, then the path of the compression: 29 // return x == FA [x]? X: Find (FA [x]); 30 } 31 is // where the combined set of x and Y 32 void Unite ( int x, int Y) { 33 is // go first ancestors 34 is X = Find (X); 35 Y = Find (Y); 36 IF (X == Y) return ; // if already set, anything not done 37 [ IF (Rank [X] < Rank [Y]) { 38 is FA [X] = Y; 39 } 40 the else { 41 is FA [Y] = X; 42 is IF (Rank [X] == Rank [Y]) Rank [X] ++; // if a high a short, final height of the original must be high the height of the tree. If the two as the height will become original height + 1 43 } 44 } 45 // determines whether the x and y belonging to the same set of 46 is BOOL same ( int x, int y) { 47 return Find ( X) == Find (Y); 48 }
And check writing template set
Guess you like
Origin www.cnblogs.com/OldAtaraxi/p/12203166.html
Ranking