案例4-1.7 文件传输--我的踩坑

文章目录

题目

当两台计算机双向连通的时候,文件是可以在两台机器间传输的。给定一套计算机网络,请你判断任意两台指定的计算机之间能否传输文件?

输入格式:
首先在第一行给出网络中计算机的总数 N (2≤N≤104),于是我们假设这些计算机从 1 到 N 编号。随后每行输入按以下格式给出:

I c1 c2

其中I表示在计算机c1和c2之间加入连线,使它们连通;或者是

C c1 c2

其中C表示查询计算机c1和c2之间能否传输文件;又或者是

S

这里S表示输入终止。

输出格式:
对每个C开头的查询,如果c1和c2之间可以传输文件,就在一行中输出"yes",否则输出"no"。当读到终止符时,在一行中输出"The network is connected.“如果网络中所有计算机之间都能传输文件;或者输出"There are k components.”,其中k是网络中连通集的个数。

输入样例 1:

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

输出样例 1:

no
no
yes
There are 2 components.

输入样例 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

输出样例 2:

no
no
yes
yes
The network is connected.

这道题的测试用例会卡住不适用按秩归并和路径压缩的代码。
此外,在检查网络是否连通的代码中,我没有给用来计数的变量setCount显示初始化为0,导致在pta这里一直输出个负值,如果在vscode和devcpp的话就不会有这个问题。
还有一个个人的疏忽大意:
root2 = findRoot(Set, c2 - 1);里的c2-1顺着c2写成了c2-2,好久才发现这个问题。

代码

#include<stdio.h>
void connect(int Set[]);
void isConnected(int Set[]);
void checkNetwork(int Set[], int length);
void initSet(int Set[], int length);
int findRoot(int Set[], int data);
// 按秩合并
void unionSet(int Set[], int root1, int root2);
int main() {
    char operation;
    int computerCount;
    scanf("%d\n", &computerCount);
    int Set[computerCount];
    initSet(Set, computerCount);
    do {
        scanf("%c", &operation);
        switch (operation) {
        case 'I':
            connect(Set);
            break;
        case 'C':
            isConnected(Set);
            break;
        case 'S':
            checkNetwork(Set, computerCount);
        default:
            break;
        }
    } while (operation != 'S');
    return 0;
}

void initSet(int Set[], int length) {
    for (int i = 0;i < length;i++)
        Set[i] = -1;
}
int findRoot(int Set[], int data) {
    if (Set[data] < 0)
        return data;
    else
        return Set[data] = findRoot(Set, Set[data]);
}
void unionSet(int Set[], int root1, int root2) {
    if (Set[root1] > Set[root2]) {
        Set[root2] += Set[root1];
        Set[root1] = root2;
    } else {
        Set[root1] += Set[root2];
        Set[root2] = root1;
    }
}
void connect(int Set[]) {
    int c1, c2;
    int root1, root2;
    scanf("%d %d\n", &c1, &c2);
    // 这里的元素从1~N编号,需要减一
    root1 = findRoot(Set, c1 - 1);
    root2 = findRoot(Set, c2 - 1);
    if (root1 != root2)
        unionSet(Set, root1, root2);
}
void isConnected(int Set[]) {
    int c1, c2;
    scanf("%d %d\n", &c1, &c2);
    findRoot(Set, c1 - 1) == findRoot(Set, c2 - 1) ? printf("yes\n") : printf("no\n");
}
void checkNetwork(int Set[], int length) {
    int setCount=0;
    for (int i = 0;i < length;i++)
        if (Set[i] < 0)
            setCount++;
    setCount == 1 ? printf("The network is connected.\n") : printf("There are %d components.\n", setCount);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值