数据结构-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 Specification:

Each input file contains one test case. For each test case, the first line contains N (2≤N≤10​4​​), 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." where k 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.

运行结果

Case Hint Result Run Time Memory
0 sample 1 合并2个集合,最后不连通 Accepted 3 ms 424 KB
1 sample 2 最后连通 Accepted 2 ms 384 KB
2 最小N,无连通操作 Accepted 3 ms 384 KB
3 最大N,无操作 Accepted 2 ms 540 KB
4 最大N,递增链,卡不按大小union的 Accepted 43 ms 424 KB
5 最大N,递减链,卡不按大小union的 Accepted 49 ms 424 KB
6 最大N,两两合并,反复查最深结点,卡不压缩路径的 Accepted 49 ms 412 KB

程序


#include<iostream>
using namespace std;

#define MaxSize 10000
typedef int SetType[MaxSize];

void initializeSet(SetType S, int N);
void Check(SetType S);
int Find(SetType S, int X);
void Connect(SetType S);
void Union(SetType S, int Root1, int Root2);
void Stop(SetType S, int N);

/*测试数据1
//输入操作
char Operation[7] = {'C','I','C','I','I','C','S'};
//输入结点的下标
int InputIndex[7][2] = \
{ {3,2},
  {3,2},
  {1,5},
  {4,5},
  {2,4},
  {3,5},
  {0,0}};

int i = 0;
*/
/*测试数据2
//输入操作
char Operation[9] = {'C','I','C','I','I','C','I','C','S'};
//输入结点的下标
int InputIndex[9][2] = \
{ {3,2},
  {3,2},
  {1,5},
  {4,5},
  {2,4},
  {3,5},
  {1,3},
  {1,5},
  {0,0}};

int i = 0;
*/

int main()
{
    /*测试程序
    SetType S;
    char Ope;
    //输入总结点个数
    static int N = 5;
    //初始化集合数组
    initializeSet(S, N);

    do{
        Ope = Operation[i];
        switch(Ope){
            case 'C': Check(S); break;
            case 'I': Connect(S); break;
            case 'S': Stop(S, N); break;
        }
        ++i;
    }while(Ope != 'S');
    */
    //正式应用
    SetType S;
    char Ope;
    int N;
    //输入总结点个数
    cin >> N;
    //初始化集合数组
    initializeSet(S, N);

    do{
        cin >> Ope;
        switch(Ope){
            case 'C': Check(S); break;
            case 'I': Connect(S); break;
            case 'S': Stop(S, N); break;
        }
    }while(Ope != 'S');

    return 0;
}

void initializeSet(SetType S, int N)
{
    for(int i=0; i<N; i++){
        S[i] = -1;
    }
}

void Check(SetType S)
{
    int u, v;
    int Root1, Root2;

    //测试程序
    //u = InputIndex[i][0];
    //v = InputIndex[i][1];

    cin >> u;
    cin >> v;
    //由于数组第一下标为0,而输入第一下标为1
    Root1 = Find(S, u-1);
    Root2 = Find(S, v-1);
    if(Root1 == Root2){
        cout << "yes" <<endl;
    }
    else
        cout << "no" <<endl;
}

int Find(SetType S, int X)
{
    //递归出口条件:找到根结点
    if(S[X] < 0){
        return X;
    }
    //递归逻辑:(路径压缩)最初的输入X到集合根结点路径上的结点,把这些结点
    //的父结点都设为根结点
    else
        return S[X] = Find(S, S[X]);
}

void Connect(SetType S)
{
    //比规模连接:规模小的依附在规模大的上面
    int u, v;
    int Root1, Root2;

    //测试程序
    //u = InputIndex[i][0];
    //v = InputIndex[i][1];

    cin >> u;
    cin >> v;
    //由于数组第一下标为0,而输入第一下标为1
    Root1 = Find(S, u-1);
    Root2 = Find(S, v-1);
    //两个根相等的集合不需要再连接
    if(Root1 != Root2){
        Union(S, Root1, Root2);
    }
}

void Union(SetType S, int Root1, int Root2)
{
    //由于根结点保存的父结点值为负数,
    //其绝对值表示该集合的元素个数,
    //因此规模大的集合,其值小
    if(S[Root1] < S[Root2]){
        //Root1集合的元素更多,规模更大
        //更新新集合的元素个数
        S[Root1] = S[Root1] + S[Root2];
        S[Root2] = Root1;
    }
    else{
        S[Root2] = S[Root1] + S[Root2];
        S[Root1] = Root2;
    }
}

void Stop(SetType S, int N)
{
    int components = 0;
    for(int n=0; n<N; n++){
        if(S[n] < 0) ++components;
    }
    //判断是否全连接
    if(components == 1)
        cout << "The network is connected." <<endl;
    else
        printf("There are %d components.\n", components);
}

猜你喜欢

转载自blog.csdn.net/Brianone/article/details/89373819