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

运行结果

CaseHintResultRun TimeMemory
0sample 1 合并2个集合,最后不连通Accepted3 ms424 KB
1sample 2 最后连通Accepted2 ms384 KB
2最小N,无连通操作Accepted3 ms384 KB
3最大N,无操作Accepted2 ms540 KB
4最大N,递增链,卡不按大小union的Accepted43 ms424 KB
5最大N,递减链,卡不按大小union的Accepted49 ms424 KB
6最大N,两两合并,反复查最深结点,卡不压缩路径的Accepted49 ms412 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);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值