数据结构基础 — File Transfer

这个按照老师的自己码一遍就好了,按秩合并+路径压缩,然后最后的K是指在整个网络里有多少个集合。不说理论部分,不相关集是我本科数据结构学的代码最简单的部分了,哈哈。

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 Specification:

Each input file contains one test case. For each test case, the first line contains N (2N104), 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.

/*--------------------------------------------------------------
 如果 ElementType Data是个整数的话,那么就直接用数组好了
 ---------------------------------------------------------------*/
#include <stdio.h>
#define MaxSize 100000
typedef int ElementType;
typedef int SetName;
typedef ElementType SetType[MaxSize];
SetName Find(SetType S, ElementType X);
void Union (SetType S, SetName Root1, SetName Root2);
void Input_connections(SetType S);

/* 路径压缩,这个可以通过第四个测试点*/
SetName Find(SetType S, ElementType X){
    if(S[X] < 0)
        return X;
    else
        return S[X] = Find(S, S[X]);
}

void Check_network(SetType S, int n){
    int i, counter = 0;
    for(i = 0; i < n; i++){
        if(S[i] < 0)
            counter++;
    }
    if(counter == 1)
        printf("The network is connected.\n");
    else
        printf("There are %d components.\n",counter);
}

void Input_connections(SetType S){
    ElementType u,v;
    SetName Root1, Root2;
    scanf("%d %d\n",&u, &v);
    Root1 = Find(S, u-1);
    Root2 = Find(S, v-1);
    if(Root1 != Root2)
        Union(S, Root1, Root2);
}

void Check_connections(SetType S){
    ElementType u,v;
    SetName Root1,Root2;
    scanf("%d %d\n",&u,&v);
    Root1 = Find(S, u-1);
    Root2 = Find(S, u-1);
    if(Root1 == Root2)
        printf("yes\n");
    else
        printf("no\n");
}
/* 按秩归并, 如果随意归并的话很可能会超时,
 就是把矮树贴到高的树上,用根节点表示树高,这样可以past一个测试点 */
void Union (SetType S, SetName Root1, SetName Root2){
    if(S[Root2] < S[Root1])
        S[Root1] = Root2;  //小的连在大的,二者的高度都不会变;
    else{
        if(S[Root2] == Root1)
            S[Root1] --;  //相等的话,选择其中一个改变,这里选择root1;
        S[Root2] = Root1;
    }
}

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

int main(int argc, const char * argv[]) {
    SetType S;
    int N;
    scanf("%d",&N);
    Initialization(S,N);
    char in;
    do{
        scanf("%c",&in);
        switch (in) {
            case 'I':
                Input_connections(S);
                break;
            case 'C':
                Check_connections(S);
                break;
            case 'S':
                Check_network(S,N);
                break;
        }
    }while(in != 'S');
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_25175067/article/details/79822955