04-2. File Transfer (PAT) - 集合问题

We have a network of computers and a list of bi-directional connections. Each of 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 one test case. For each test case, the first line contains N (2<=N<=104), the total number of computers in a network. Each computer in the network is then represented by a positive integer between 1 and N. Then in the following lines, the input is 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 is possible to transfer files between c1 and c2; or

S

where S stands for stopping this case.

Output Specification:

For each C case, print in one line the word "yes" or "no" if it is possible or impossible to transfer files between c1 and c2, respectively. At the end of each case, print in one line "The network is connected." if there is a path between any pair of computers; or "There are k components." wherek is the number of connected components in this network.

Sample Input 1:

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

Sample Output 1:

no
no
yes
There are 2 components.

Sample Input 2:

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

Sample Output 2:

no
no
yes
yes
The network is connected.

题意:根据给出的数字,建立这些数字的集合关系解题思路:使用数组存储,数组下标对应给出的元素,数组元素值对应该下标值的parent]如数组a[3] = 8表示,元素3的parent为8注意:对于大规模的集合,可能产生某个集合层级很多的情况,可在进行查找操作时减少集合的层级。
#include <iostream>
#include <string>
using namespace std;

#define MaxNum 10001

int Find( int arrSet[], int X );
void ConnectSet( int arrSet[], int c1, int c2 );
string CheckConnection( int arrSet[], int c1, int c2 );
int CountComponentNum( int arrSet[], int num );

//使用数组保存集合元素,数组下标对应元素值,数组元素对应parent值

int main()
{
    int nodeNum;
    int *arrSet;
    cin >> nodeNum;
    nodeNum += 1;    //由于数组下标0用于标记根结点,因此使用数组下标从1开始
    arrSet = new int[ nodeNum ];    //parent == 0 -> root, parent == -1 -> NULL
    int i;
    for ( i = 0; i < nodeNum; i++ )
    {
        arrSet[i] = -1;
    }

    int c1;
    int c2;
    string operationName;
    string checkResult = "";
    int componentNum;
    while( true )
    {
        cin >> operationName;
        if ( operationName == "S" )
        {
            break;
        }
        cin >> c1 >> c2;
        if ( operationName == "I" )
        {
            ConnectSet( arrSet, c1, c2 );
        }
        else if ( operationName == "C" )
        {
            checkResult += CheckConnection( arrSet, c1, c2 );
        }
    }
    cout << checkResult;
    componentNum = CountComponentNum( arrSet, nodeNum );
    if ( componentNum == 1 )
    {
        cout << "The network is connected." << endl;
    }
    else
    {
        cout << "There are " << componentNum << " components." << endl;
    }
}

int Find( int arrSet[], int X )    //在查找过程中,减少树的高度
{
    if ( X > MaxNum || arrSet[X] == -1 )
    {
        return -1;
    }
    int temp = X;
    int exchange;
    while( arrSet[X] > 0 )
    {
        X = arrSet[X];
    }
    while ( arrSet[arrSet[temp]] > 0 )    //将搜索路径中的所有元素都直接指向根结点
    {
        exchange = temp;
        temp = arrSet[temp];
        arrSet[exchange] = X;
    }
    return X;
}

void ConnectSet( int arrSet[], int c1, int c2 )
{
    int root1, root2;
    if ( arrSet[c1] == -1 )
    {
        arrSet[c1] = 0;
        root1 = c1;
    }
    else
    {
        root1 = Find( arrSet, c1 );
    }
    if ( arrSet[c2] == -1 )
    {
        arrSet[c2] = 0;
        root2 = c2;
    }
    else
    {
        root2 = Find( arrSet, c2 );
    }
    if ( root1 != root2 )
    {
        arrSet[root2] = root1;
    }
}

string CheckConnection( int arrSet[], int c1, int c2 )
{
    int root1, root2;
    root1 = Find( arrSet, c1 );
    root2 = Find( arrSet, c2 );
    if ( root1 == root2 && root1 != -1 && root2 != -1 )
    {
        return "yes\n";
    }
    else
    {
        return "no\n";
    }
}

//计算数组中的集合个数
int CountComponentNum( int arrSet[], int num )
{
    int i;
    int sum = 0;
    int nodeNum = 0;
    for( i = 0; i < num; i++ )
    {
        if ( arrSet[i] >= 0 )    //统计所有结点数量
        {
            nodeNum++;
        }
        if ( arrSet[i] == 0 )    //根结点的数量
        {
            sum++;
        }
    }
    return sum + num - 1 - nodeNum;    //给出的结点数中,可能存在未包含进任何集合的结点                                       //这里-1是由于在一开始将nodeNum加了1,这个处理需要改进
}
				
时间: 2024-11-06 07:08:23

04-2. File Transfer (PAT) - 集合问题的相关文章

PAT 05-树7 File Transfer

这次的题让我对选择不同数据结构所产生的结果惊呆了,一开始用的是结构来存储集合,课件上有现成的,而且我也是实在不太会,150ms的时间限制过不去,不得已,看到这题刚好可以用数组,结果7ms最多,有意思!什么是时间复杂度,终于有了一次感性的认识.这题还有个check的要求,我用了一个链表来存储,感觉挺麻烦的,不知是否有更好的方法,题设要求及代码实现如下 1 /* 2 Name: 3 Copyright: 4 Author: 5 Date: 06/04/15 09:46 6 Description:

PAT 5-8 File Transfer (25分)

We have a network of computers and a list of bi-directional connections. Each of 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:

PAT File Transfer

File Transfer We have a network of computers and a list of bi-directional connections. Each of 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 S

PTA 5-8(English) File Transfer (25) - 并查集 - 数组实现

题目:http://pta.patest.cn/pta/test/16/exam/4/question/670 PTA - Data Structures and Algorithms (English) - 5-8 We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer t

04-树5. File Transfer (25)

04-树5. File Transfer (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer to another. Is it

04-2. File Transfer (25)并查集

04-2. File Transfer (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer to another. Is it

05-树8 File Transfer (25分)

题目描述 We have a network of computers and a list of bi-directional connections. Each of 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 Specificat

FTP(File Transfer Protocol)是什么?

文件传输协议 FTP(File Transfer Protocol),是文件传输协议的简称.用于Internet上的控制文件的双向传输.同时,它也是一个应用程序(Application).用户可以通过它把自己的PC机与世界各地所有运行FTP协议的服务器相连,访问服务器上的大量程序和信息.[编辑本段]FTP的作用 正如其名所示:FTP的主要作用,就是让用户连接上一个远程计算机(这些计算机上运行着FTP服务器程序)察看远程计算机有哪些文件,然后把文件从远程计算机上拷到本地计算机,或把本地计算机的文件

SSH Secure File Transfer Client传送文件

SSH Secure File Transfer Client是连接Linux的主要客户端工具之一,其特点就是传送文件方便.虽然SSH Secure File Transfer Client显示中文时有时会乱码,但仍然挡人们对它的喜爱 http://jingyan.baidu.com/article/19192ad815fd0ee53e570719.html