PTA 5-8 (English) File Transfer (25)-and set-array implementation

Source: Internet
Author: User

Title: http://pta.patest.cn/pta/test/16/exam/4/question/670

Pta-data structures and Algorithms (中文版)-5-8

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≤10?4??) 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 c2 and;

C c1 c2

Where C stands for checking if it's possible to transfer files between c1 c2 and;

S

Where S stands for stoppingthe this case.

Output Specification:

For each case, print on one line C the word "yes" or "no" if it is possible or impossible to Tra Nsfer files between c1 c2 and, respectively. At the end of all case, print on one line ' the network is connected. ' if there is a path between any pair of COM Puters; Or "There is a k ." where is the number of k connected.

Sample Input 1:
5C 3 2I 3 2C 1 5I 4 5I 2 4C 3 5S
Sample Output 1:
nonoyesThere are 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 solution turns from: http://www.cnblogs.com/clevercong/p/4192953.html

Topic Analysis:

The first input is an integer number that represents how many computer (nodes) are in the network.

Then enter a number of lines, each consisting of 1 characters and two integers. I represents an increase of the connection, and C represents the check whether the connection, S represents the end input.

C Check the connection, to output yes or no to indicate whether connectivity.

The final output of the entire network, if all the general clauses output the net is connected, otherwise the output has how many connected components.

Algorithm Example:

Note: On the left, figure 1.  2.3.  4.5. The result of the corresponding operation in the middle diagram

Procedure: Input: The corresponding tree:

1. Initialization

int *s;   global variable definition//dynamic application in main function SS = new int[num+1];   Num+1 convenient starting from 1 to check for (int i=0; i<=num; i++)    s[i] = i;   Array subscript is data, array value is parent

2. "I" and "C" operation

if (choose = = ' I ')    Union (c1, C2), if (choose = = ' C ') {    if (find (c1) = = Find (c2))//root is the same, in a collection        cout << " Yes "<< Endl;    else        cout << "no" << Endl;}

3. Check

int find (ElementType x)//Find the root node {    if (s[x]==x)        return x of the collection where x is located;    Return S[x]=find (S[x]); Always find root, assign root to s[x] that is, connect the node directly to the root to make the current tree high to 1}

4. and

void Union (ElementType X1, ElementType X2)//combine X1 and X2 into one and concentrate (increase connection) {    int Root1, Root2;    ROOT1 = Find (X1);    Root2 = Find (X2);    if (Root1! = Root2)  //! root is not equal when merging    {        //! number large to small upper        if (S[ROOT1] < S[root2])            S[root2] = Root1 ;        else            s[root1] = Root2;    }}

5. To find the number of connected components

for (int i=1; i<=num; i++) {    if (s[i] = = i)   //If the parent is the data itself, it is the root node    icount++;}
Full code:
#include <iostream>using namespace Std;int *s;//! Find the root node of the collection where X is located int find (int X) {if (s[x]==x) return X; Return S[x]=find (S[x]);    Look up the root, and connect the current node directly to the root, the height of the tree to 1}void Union (int X1, int X2) {int Root1, Root2;    ROOT1 = Find (X1);        Root2 = Find (X2); if (Root1! = Root2)//! root is not equal when merging {//! number large to small upper if (S[root1] < S[root2]) S[root2] = Root1        ;    else s[root1] = Root2;    }}int Main () {int num;    CIN >> Num;    Char choose;    int C1, C2; S = new Int[num+1];    ! Starting from 1, easy to find, so altogether num+1//! initialization: Array angle label is data, value is parent for (int i=0; i<=num; i++) s[i] = i;        while (1) {cin >> choose;        if (choose = = ' S ') break;        CIN >> C1 >> C2;        if (choose = = ' I ') Union (c1, C2);            if (choose = = ' C ') {if (find (c1) = = Find (C2)) cout << "yes" << Endl; else cout << "No" << endL }} int icount=0;    Record how many connected components for (int i=1; i<=num; i++) {if (s[i] = = i)//! If the parent is the data itself, it proves to be the root node icount++;    if (icount = = 1) cout << "The network is connected." << Endl;    else cout << "There is" << icount << "Components" << Endl; return 0;}

PTA 5-8 (English) File Transfer (25)-and check-array implementation

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.