5-8 File Transfer (25分)
来源:互联网 发布:linux 应用层驱动 编辑:程序博客网 时间:2024/05/16 09:39
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≤104), 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:
5C 3 2I 3 2C 1 5I 4 5I 2 4C 3 5S
Sample Output 1:
nonoyesThere are 2 components.
Sample Input 2:
5C 3 2I 3 2C 1 5I 4 5I 2 4C 3 5I 1 3C 1 5S
Sample Output 2:
nonoyesyes
The network is connected.
思路:本题的关键点是按秩归并和路径压缩,关于这两点在MOOC上老师有很详细的解释。
#include<stdio.h>#include<stdlib.h>#include<algorithm>using namespace std;int S[10001];int N;int Find(int S[],int i){if(S[i] < 0){return i;}elsereturn S[i] = Find(S,S[i]);/*不断的找父结点,一直找到根结点,然后再把每个父结点指向根结点*/}void check(int S[]){int root1,root2;int a, b;scanf("%d %d",&a,&b);root1 = Find(S,a-1);root2 = Find(S,b-1);if(root1 == root2){printf("yes\n");}elseprintf("no\n");}void Union(int S[],int root1,int root2){if(S[root2] < S[root1]){S[root2] += S[root1];S[root1] = root2;}else{S[root1] += S[root2];S[root2] = root1;}}void Insert(int S[]){int root1,root2;int a, b;scanf("%d %d",&a,&b);root1 = Find(S,a-1);root2 = Find(S,b-1);if(root1 != root2){Union(S,root1,root2);}}int main(void){char ch;int count = 0;scanf("%d",&N);for(int i = 0; i < N; i++)/*初始化令每个结点都是根结点*/{S[i] = -1;}while(1){scanf("%s",&ch);if(ch == 'C'){check(S);}else if(ch == 'I'){Insert(S);}else if(ch == 'S'){break;}}for(int i = 0; i < N; i++){if(S[i] < 0)count++;}if(count == 1){printf("The network is connected.");}else{printf("There are %d components.",count);}return 0;}
在之前的Find函数中我用的是循环来返回根结点,结果犯了一个错误。int Find(int S[],int i)
{
for(;S[i] > 0; i = S[i]);
return i;
}
for循环后的那个分号我没加,结果导致i = S[i]这条语句在for循环判断后(不满足S[i] > 0),仍然被执行。
- 5-8 File Transfer (25分)
- 05-树8 File Transfer (25分)
- 05-树8 File Transfer (25分)
- 05-树8 File Transfer (25分)
- 05-树8 File Transfer (25分)
- 05-树8 File Transfer (25分)
- 05-树8 File Transfer (25分)
- 05-树8 File Transfer (25分)
- 5-1 File Transfer (25分)
- File Transfer(25 分)
- 05-树8 File Transfer (25分)---并查集
- 05-树8 File Transfer(25 分)
- 05-树8 File Transfer(25 分)
- 5-8 File Transfer (25分) (简单的并查集, 注意getchar())
- 05-树9 File Transfer (25分)
- 05-树9 File Transfer (25分)
- 05-树9 File Transfer (25分)
- 7-26 File Transfer(25 分)
- 挑战程序设计竞赛 POJ 2431 贪心+优先队列
- uchar与char
- 关于server右键后无法显示 Browse Deployment Location...的解决办法
- ElK分析tomcat的Catalina.out日志
- NestedScrollChild与NestedScrollParent的认识
- 5-8 File Transfer (25分)
- VTK修炼之道14:图像处理_创建
- codeblocks + msys2
- Activity向Fragment传递参数
- 数字字母正则表达式
- HashMap-Java集合框架之 Java HashMap 源码解析
- RecyclerView的动画及banner的用法
- 将FCKeditor文本编辑器添加到javaweb项目中
- (HDU 5775)Bubble Sort <树状数组> 多校训练4