04-Tree 5. File transfer--and check set

Source: Internet
Author: User

Common operations for a collection are: Judging whether an element belongs to a collection, merging two collections, and so on. and The disjoint is a useful tool to deal with the merging and querying problems of some disjoint sets (sets) .

  and the collection is realized by using tree structure . A collection is represented by a tree, and multiple collections are forests. And the centralized "and" is to merge two sets of two trees into a tree; "Check" is to find out which collection an element belongs to, that is, find out which tree the node belongs to. Ideas are as follows:

    • Search: Find the parent node through the node, always up to the root node, return to the root node, and the root node represents the only tree;
    • And: First find the tree of two nodes, if in the same tree (that is, the root node found the same), then directly return; otherwise, a tree is attached to another tree as a subtree, and a root node will be the son of another root node.

So the question is, which root node should be the son? The simple idea is whatever, so in a programming implementation, either keep the previous root node as the son, or keep the latter root node as the son. The advantage of this implementation is that it is simple to implement and short in code. But it is not necessarily efficient to know that the relatively short code is not. In this case, it is easy for the tree to lose its balance, as the larger tree may be used as a subtree of smaller trees, making the depth of the tree larger. In fact, it is more natural to think of smaller trees as subtrees of larger trees , so that merging does not increase the depth of the tree (if the two trees are equally large, of course), making the tree relatively balanced.

  A collection is not defined precisely, and the whole that is usually formed by putting together something that is not the same is called a set. As for why they are put together, they are based on the need for specific problems, such as placing a road-connected town in a set (connectivity problem). certainty, reciprocity, and disorder are the three major properties of a set. Because of the nature of the set, it can usually correspond to the natural number one by one . Therefore, it is very convenient and ingenious to implement and check the set with an array: array subscript (starting from 1) as the tree node, and the value of the array subscript as the parent node. because the root node does not have a parent node, it is advisable to make the corresponding array's value non-positive. And how to compare two trees who are big or small, a very ingenious trick is to let the root node correspond to the value of the array (non-positive) of the absolute values as the depth of this tree . This makes the code not only concise, but also space-saving. The specific implementation code is as follows:

intFather[n] = {0};//Initialize, all nodes are root node, depth is 0;n to maximum possible number of nodesintN//N is the actual number of nodes/*Find function*/intFind (intFather[],intNintx) {    intRoot;  for(root = x; Father[root] >0; root = Father[root]);//traverse up until the root node exits the loop    returnRoot//returns the root node}/*Merging Functions*/voidUnion (intFather[],intNintAintb) {    intAroot, Broot; Aroot= Find (father, N, a);//find the root node of the tree where Node A is locatedBroot = Find (father, N, b);//Locate the root node of the tree where the b node is located    if(Aroot = =broot)return; Else if(Father[aroot] > Father[broot])//B-Tree depth greater than a-tree    {        /*the depth of the tree does not change*/Father[aroot]= Broot;//point A tree to B-Tree    }    Else    {        if(Father[aroot] = = Father[broot])//A, b Two trees are the same depthfather[aroot]--;//The depth of the tree is plus 1, and the absolute value of the root node corresponding to the array (non-positive) is the depth of the tree.Father[broot] =Aroot; }}

Here is an examination and review of the exercise, the source of the topic: http://www.patest.cn/contests/mooc-ds/04-%E6%A0%915

We have a network of computers and a list of bi-directional connections. Each of the these connections allows a file transfer from one computer to another. Is it possible to send a file from any computer on the network to any other?

Input Specification:

Each input file contains the one test case. For each test case, the first line contains N (2<=n<=104) and the total number of computers in a network. Each of the computer in the network are then represented by a positive integer between 1 and N. Then in the following lines, the input was given in the format:

I C1 C2  

Where I stands for inputting a connection between C1 and C2; Or

C C1 C2    

Where C stands for checking if it's possible to transfer files between C1 and C2; Or

S

Where S stands for stopping the this case.

Output Specification:

For each C case, print in one line the word "yes" or "no" if it's possible or impossible to transfer files Betwe En C1 and C2, respectively. At the end of all case, print on one line ' the network is connected. ' If there is a path between any pair of computers; Or "there is K." Where K is the number of connected the.

Sample Input 1:

5C 3 2I 3 2C 1 5I 4 5I 2 4C 3 5S

Sample Output 1:

Nonoyesthere is 2 components.

Sample Input 2:

5C 3 2I 3 2C 1 5I 4 5I 2 4C 3 5I 1 3C 1 5S

Sample Output 2:

Nonoyesyesthe Network is connected.

The code is as follows:
#include <cstdio>#include<cstring>#defineN 10010/*Lookup function: Finds the root node of its tree by the value of the node*/intFind (intFather[],intNintx);/*Merging functions: Merging two nodes*/voidUnion (intFather[],intNintAintb);intMain () {Charoperation[Ten]; intFather[n] = {0};//Initialize, all nodes are root nodes, with a depth of 0    intN; intA, B; scanf ("%d", &N);  while(SCANF ("%s", operation) = =1)    {        if(strcmp (Operation,"S") ==0) Break; if(strcmp (Operation,"C") ==0) {scanf ("%d%d", &a, &b); if(Find (Father, N, a) = = Find (father, N, b))//The same Joghen node is on the same treeprintf"yes\n"); Elseprintf ("no\n"); }        Else{scanf ("%d%d", &a, &b);        Union (father, N, a, b); }    }    intK =0;//Count the number of root nodes (number of trees)     for(inti =1; I <= N; i++)    {        if(Father[i] <=0) K++; }    if(k = =1)//There is only one tree, which is all connectedprintf"The network is connected.\n"); Elseprintf ("there is%d components.\n", K); return 0;}voidUnion (intFather[],intNintAintb) {    intAroot, Broot; Aroot= Find (father, N, a);//find the root node of the tree where Node A is locatedBroot = Find (father, N, b);//Locate the root node of the tree where the b node is located    if(Aroot = =broot)return; Else if(Father[aroot] > Father[broot])//B-Tree depth greater than a-tree    {        /*the depth of the tree does not change*/Father[aroot]= Broot;//point A tree to B-Tree    }    Else    {        if(Father[aroot] = = Father[broot])//Two tree depth equalfather[aroot]--;//The depth of the tree is plus 1, and the absolute value of the root node corresponding to the array (non-positive) is the depth of the tree.Father[broot] =Aroot; }}intFind (intFather[],intNintx) {    intRoot;  for(root = x; Father[root] >0; root = Father[root]);//traverse up until the root node exits the loop    returnRoot//returns the root node}

The schematic example 2 is as follows:








04-Tree 5. File transfer--and check set

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.