PTA 05-树8 File Transfer

PTA 05-树8 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.

程序框架

(陈越Mooc PPT)
在这里插入图片描述
在这里插入图片描述

按秩归并

(1)按树高
在这里插入图片描述

(2)按树的规模
在这里插入图片描述

路径压缩

沿路径递归将子节点全部直接挂到父节点上。
在这里插入图片描述

#include<iostream>
#define MaxSize 10010
using namespace std;

//在S中找有没有这个值->路径压缩
int Find(int S[],int u){
    if(S[u]<0){
        return u;
    }else{
        return S[u]=Find(S,S[u]);//路径压缩->递归
    }
}

//按树的规模合并
void Union(int S[],int root1,int root2){
    if(S[root1]<S[root2]){//abs S[root1] > abs S[root2];root1为大规模
        S[root1]+=S[root2];//更新新的规模
        S[root2]=root1;
    }else{//规模一样大或root2为大规模
        S[root2]+=S[root1];
        S[root1]=root2;
    }
}
//I
void Input(int S[]){
    int u,v,root1,root2;
    cin>>u>>v;//u存储在S[u-1]中,因为S从0开始的下标
    root1=Find(S,u-1);
    root2=Find(S,v-1);
    if(root1!=root2){
        Union(S,root1,root2);
    }
}
//C
void Check(int S[]){
    int u,v,root1,root2;
    cin>>u>>v;
    root1=Find(S,u-1);
    root2=Find(S,v-1);
    if(root1==root2){
        printf("yes\n");
    }else{
        printf("no\n");
    }
}
//S
void Check_netword(int S[],int N){
    int count=0;
    for(int i=0;i<N;i++){
        if(S[i]<0){
            count++;
        }
    }
    if(count==1){
        printf("The network is connected.\n");
    }else{
        printf("There are %d components.\n",count);
    }
}
int main(){
    int N;
    cin>>N;
    int S[N];
    for(int i=0;i<N;i++){
        S[i]=-1;//初始化全部为树根
    }
    char in;
    do{
        cin>>in;
        switch(in){
            case 'I':Input(S);break;
            case 'C':Check(S);break;
            case 'S':Check_netword(S,N);break;
        }
    }while(in!='S');
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这里给出 PTA 7-8 成绩统计2 的题目描述和 Python 3 代码实现。 ## 题目描述 本题要求读入 N(1 <= N <= 1000)个成绩,统计其中的优秀(>=85)、良好([70, 85))、及格([60, 70))和不及格(<60)的人数,并输出统计结果。当某一类成绩的人数为0时,不输出该行。 ## 输入格式 输入的第一行为正整数 N,表示输入成绩的个数。 接下来 N 行,每行为一个整数,表示一个成绩。 ## 输出格式 统计结果,四行,每行输出一个整数,分别表示优秀、良好、及格和不及格的人数。 ## 输入样例 ``` 10 60 75 90 55 70 82 100 67 74 80 ``` ## 输出样例 ``` 2 5 2 1 ``` ## 代码实现 ```python n = int(input()) # 输入成绩个数 # 初始化四个计数器,分别统计优秀、良好、及格和不及格的人数 excellent = 0 good = 0 passing = 0 failing = 0 # 循环读入 n 个成绩,并统计各个类别的人数 for i in range(n): score = int(input()) # 读入一个成绩 if score >= 85: excellent += 1 elif score >= 70: good += 1 elif score >= 60: passing += 1 else: failing += 1 # 输出各个类别的人数(如果人数为 0,则不输出该行) if excellent > 0: print(excellent) else: print("N/A") if good > 0: print(good) else: print("N/A") if passing > 0: print(passing) else: print("N/A") if failing > 0: print(failing) else: print("N/A") ``` 代码说明: 1. 首先输入成绩个数 n; 2. 然后初始化四个计数器 excellent、good、passing 和 failing,分别用于统计优秀、良好、及格和不及格的人数,初始值都为 0; 3. 接着使用 for 循环,循环 n 次,每次读入一个成绩 score,并使用 if 语句判断该成绩属于哪个类别,然后累加相应的计数器; 4. 循环结束后,使用 if 语句,判断各个类别的人数是否为 0,如果不为 0,则输出该类别的人数,否则输出 "N/A"。 5. 完成以上步骤,即可得到各个类别的人数统计结果。 希望能帮助到您!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值