I. and the concept of a collection
And check set is an algorithm can be used to determine the correlation (the same part of a set) of the elements belong to several sets, can also be used to determine whether the two points in the graph structure is connected. And the design of the collection of ideas is this:
Any element must be lost in the following three states during program execution
1. F[i]=i, the element in this state may not be merged (initial state), or it may be merged but the selected parent node is the node
2. There is a parent node and is the true parent node in the current state (in fact the most normal state, the second state also contains the first case).
3. There is a parent node, but not the current state of the real parent node, possibly the previous parent node, as can be seen by the merge function, when merging two nodes is only a two node of the parent node, when the node's parent node is not itself, the parent node of this node will not be changed to a new parent node. This state is dangerous when counting the number of sets, because back makes the number of collections increase, but actually do not worry about this problem, carefully examine the GETF function (look for the parent node) when you will find F[A]=GETF (F[a]) Such a piece of code, the original GETF function not only returned the father of each node , and by modifying the f[] value of each node so that it is consistent with his father's f[] value, so that whenever the node or the node's child nodes are merged again, the node's f[] will be modified to the correct value, but the problem comes, in case the node is not merged again what to do? A little insurance should be done after all the nodes in the merge are GETF and then all the nodes below, so that all nodes are correct.
Two. Application of the check set
1. Used to merge the collection elements and determine the number of combinations that the lookup element belongs to.
2. In the graph structure (the point in the diagram is the element), to determine whether two points are in the state of Unicom, application examples:
(1)Kruskal method minimum Spanning tree
Idea: All sides in accordance with the length of the size of the order, in turn from small to large row to choose Ze, each selected side, will be both ends of the point with and search set merge, if the selected edge through and check set query has been connected, then skip this side
ProblemGiven a graph of the right value, find the minimum path for all nodes in the Unicom map. Data
6 9
2 4 11
3 5 13
4 6 3
5 6 4
2 3 6
4 5 7
1 2 1
3 4 9
Code
The Kruskal algorithm of the minimum spanning tree of main.cpp//graph////Created by Zhang Jiaji on 16/3/18.//Copyright? 2016 Zhang Jiaji. All rights reserved.//#include <iostream> #include <cstring> #include <algorithm>using namespace std; int f[50];struct line{int u; int V; int W;} Lines[50];int cmp (const void *a,const void *b) {return (* (line *) a) .w> (* (line *) b). W?1:-1;} void init (int n) {for (int i=1;i<=n;i++) f[i]=i;} int getf (int a) {if (f[a]==a) return A; else {F[A]=GETF (f[a]); return F[a]; }}int merge (int a,int b) {int t1,t2; T1=GETF (a); T2=GETF (b); if (t1!=t2) {f[t2]=t1; return 1; } else return 0;} int main (int argc, const char * argv[]) {freopen ("/users/zhangjiatao/desktop/input.txt", "R", stdin); int n,m,sum; sum=0; cin>>n>>m; Init (n); for (int i=0;i<=m-1;i++) {cin>>lines[i].u>>lines[i].v>>lines[i].w; } qsort (Lines,m,sizeof (lines[0]), CMP); for (int i=0;i<=m-1;i++) cout<<lines[i].w<< ""; cout<<endl; for (int i=0;i<=m-1;i++) {if (merge (LINES[I].U,LINES[I].V) ==1) {SUM+=LINES[I].W; cout<<i+1<< ""; }} cout<<endl; cout<<sum<<endl; return 0;}
Three. And look up the set of templates 1. QuestionsGiven the relationship between the n elements of the M group, ask these elements to belong to a collection2. Enter
#include <iostream> #include <cstring>using namespace std;int f[50];void init (int n) {for (int i=1;i< =n;i++) f[i]=i;} int getf (int a) { if (f[a]==a) return A; else { f[a]=getf (f[a]); return f[a];} } void merge (int a,int b) { int t1,t2; T1=GETF (a); T2=GETF (b); if (t1!=t2) { f[t2]=t1; }} void print (int n) {for (int i=1;i<=n;i++) cout<< "(" <<i<< ")" <<f[i]<< ""; Cout<<endl;} int main (int argc, const char * argv[]) { freopen ("/users/zhangjiatao/desktop/input.txt", "R", stdin); int n,m,a,b,sum; sum=0; cin>>n>>m; Init (n); for (int i=1;i<=m;i++) { cin>>a>>b; Merge (A, b); print (n); } for (int i=1;i<=n;i++) { f[i]=getf (i);} for (int i=1;i<=n;i++) if (f[i]==i) sum++; cout<<sum<<endl; return 0;}
10 8
1 2
3 4
5 2
4 6
2 6
: U
9 7
1 6
2 4
Summary of the application of the check set