05-树8 File Transfer (25分)

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 NNN (2≤N≤1042\le N\le 10^42N104), the total number of computers in a network. Each computer in the network is then represented by a positive integer between 1 and NNN. 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.
#include 
#define maxn 10005

int s[maxn];

//对find改进, 在搜索中改变每个节点父母值,使其为根节点
int find2(int x) {
    if(s[x] < 0) return x;
    else
        return  s[x] = find2(s[x]);
}//找到跟后把每个节点的父母改为根节点,为下次查找缩短时间;

int find(int x) {
    for(; s[x] > 0; x = s[x]);
    return x;
}

//改进connect, 按秩插入1, 按高度插入;
void connect(int a, int b) {
    int r1, r2;
    r1 = find(a);
    r2 = find(b);
    if(r1 != r2) {
        if(s[r1] > s[r2]) s[r1] = r2;
        else if(s[r1] < s[r2]) s[r2] = r1;
        else { s[r1] = r2; s[r2]--; }
    }
}
//按秩插入2, 按大小插入;
void connect2(int a, int b) {
    int r1, r2;
    r1 = find(a);
    r2 = find(b);
    if(r1 != r2){
        if(s[r1] > s[r2]) {
            s[r2] += s[r1];
            s[r1] = r2;//先改变s[r2];
        }
        else {
            s[r1] += s[r2];
            s[r2] = r1;
        }
    }
}

void check(int a, int b) {
    if(find(a) != find(b)) printf("no\n");
    else printf("yes\n");
}

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

int main() {
    int N, a, b, i;
    char str[10];
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    scanf("%d", &N);
    for(i = 1; i <= N; i++) s[i] = -1;
    scanf("%s", str);
    while(str[0] != 'S') {
        scanf("%d%d", &a, &b);
        if(str[0] == 'C') check(a, b);
        if(str[0] == 'I') connect2(a, b);
        scanf("%s", str);
    }
    judge(N);
    return 0;
}

//昨天感觉状态不太对,这题看了3个小时也没搞明白,今天终于解决了,心情好多了;

你可能感兴趣的:(PTA)