Disjoint-set minimum spanning tree elaborate &

Disjoint-set minimum spanning tree &

Disjoint-set Disjoint Sets

What is the disjoint-set ?

    Disjoint-set , in some N elements of a collection of application problems, we usually make at the beginning of each element of the collection constitute a single element, then a certain order will belong to the set of elements of the same group where the merger , during which you want to repeatedly to find an element in which the collection. In recent years this type of problem recurring theme in domestic and international competition in informatics, which is characterized does not seem complicated, but the amount of data is great, if your normal data structure to describe it, often in the space is too large , the computer can not afford; even in the space of barely passed, the time complexity of the operation is also very high, simply can not calculate the results of questions need to run the game within the stipulated time (1 to 3 seconds), and can only be used to check set to describe.
    Disjoint-set (Disjoint Sets) is a tree data structure, for processing and merging a number of disjoint sets of inquiries. Often use to forest to represent. (Quoted from Baidu Encyclopedia "disjoint-set")

In simple terms, the main operation and check collection are:

   1- merge two disjoint sets
   2- queries two elements belong to the same set

The old way, the first cases of lead -

NKOJ 1205 relatives

    Maybe you do not know, one of your friends are your relatives. He could be cousin's niece's son's grandfather your great-grandfather's grandson. If you can get the complete family tree, to determine whether the relatives of two people should be feasible, but if two people's common ancestor with them for several generations apart, making the family tree is very large, so the real test nonhuman relatives whatever. In this case, the computer is the best helper.
  In order to simplify the problem, you will get some information about relatives, as Tom Marry and relatives, Tom and Ben are relatives, and so on. From this information, you can launch Marry and Ben relatives. Please write a program, for our questions about the relatives of the fastest answer.
Input format :
Input consists of two parts.
The first portion begins with N, M. N is the number of issues involved persons (1 ≤ N ≤ 20000). These people are
numbered 1,2,3, ..., N. Below the line M (1 ≤ M ≤ 100000), each line has two numbers ai, bi, ai and bi represents the known relatives.
The second section begins with Q. The following Q row has asked the Q (1 ≤ Q ≤ 1 000 000 ), each behavior ci, di, di and ci represents ask whether relatives.
Output format :
For each inquiry ci, di, di, and if ci for the relatives, the output yes, otherwise output no.
Sample input :
10. 7
2. 4
. 5. 7
. 1. 3
. 8. 9
. 1 2
. 5. 6
2. 3
. 3
. 3. 4
. 7 10
89
sample output :
Yes
NO
Yes
Portal : http://oi.nks.edu.cn/zh/Problem/Details?id=1205

    From the title we can get some tips, it is to make a set of relationships we build out, and then quickly find the two elements are on the same collection, which obviously will be very consistent with the utility and check set.

What is the process of merging (illustration)?





And investigation works set




This algorithm is based on such a high time complexity, we use some special means to optimize it, which is the core of the investigation and collection - path compression

Given below disjoint-set of core functions:

Meanwhile queries path compression
int GetFather(int v) {  //查询元素v所在集合的根节点  
    if (Father[v] == v)return v;    //v本身为根  
    else {  
        Father[v] = GetFather(Father[v]);   //只对v到根这条路径上的节点进行路径压缩  
        return Father[v];  
    }  
}  
Merge two sets
void Merge(int x, int y) {  //合并元素x和元素y所在集合  
    int fx, fy;  
    fx = GetFather(x);  //先找出x和y所在集合的根  
    fy = GetFather(y);  //两根不相同,说明x和y位于不同集合  
    if (fx != fy)Father[fx] = fy;   //将fy设为fx的父亲,合并两个集合  
}  

We returned cited cases, we can now easily solve this problem (pseudo-code) -

for (i = 1; i <= n; ++ i)Father[i] = i; //初始化  
for (i = 1; i <= m; ++ i) { //读入关系  
    cin >> x >> y;  
    Merge(x, y);  
}  
for (i = 1; i <= q; ++ i) { //回答询问  
    cin >> x >> y;  
    if (GetFather(x) == GetFather(y))  
        cout << "Yes";  
    else cout << "No";  
}  (O(m))

It offers a few simple exercises and check the set:

NKOJ 3197 islands
NKOJ 1046 imprisoned criminals

### disjoint-set heuristic merge (destined supplemented)

Minimum Spanning Tree Minnimum Spanning Tree (MST)

What is the minimum spanning tree ?

    A n nodes in communication with FIG spanning tree is the original minimal connected subgraph , and comprises in the artwork all n nodes , and holding FIG communication minimal side . Minimum Spanning Tree algorithm Kruskal (Kruskal) algorithm or Prim (Primm) is obtained. (Quoted from Baidu Encyclopedia "minimum spanning tree")

Briefly, a minimum spanning tree is generated in FIG communicating a tree, all nodes communicating just the number (or the minimum sum of edge weights) contained side.

Guess you like

Origin www.cnblogs.com/Limbo-To-Heaven/p/11704580.html