05-树7. File Transfer
来源:互联网 发布:mac自带词典词库下载 编辑:程序博客网 时间:2024/05/17 01:34
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: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<stdio.h>#include<stdlib.h>typedef struct { int data; int parent;}SetType;int cmp(SetType p[],int size,int x1,int x2);int find(SetType p[],int size,int x);void Union(SetType p[],int size,int x1,int x2);int main(){ int N,i,m,n,sum = 0; SetType setType[10001]; scanf("%d",&N); for(i = 0;i<N;i++){ setType[i].data = i+1; setType[i].parent = -1; } char key; scanf("%c",&key); while(key!='S'){ if(key == 'C'){ scanf("%d %d",&m,&n); if(cmp(setType,N,m,n)) printf("yes\n"); else printf("no\n"); getchar(); } if(key == 'I'){ scanf("%d %d",&m,&n); Union(setType,N,m,n); } scanf("%c",&key); } for(i = 0;i<N;i++){ if(setType[i].parent < 0) sum++; } if(1 == sum) printf("The network is connected."); else printf("There are %d components.",sum); return 0;}int cmp(SetType p[],int size,int x1,int x2){ int root1,root2; if(x1 == x2) return 1; root1 = find(p,size,x1); root2 = find(p,size,x2); if(root1 == root2) return 1; else return 0;}// 查 int find(SetType p[],int size,int x){ int i = x-1; if(i>=size) return -1; for(;p[i].parent>=0;i = p[i].parent); return i;}// 并 void Union(SetType p[],int size,int x1,int x2){ int root1,root2; root1 = find(p,size,x1); root2 = find(p,size,x2); if(root1!=root2){ if(p[root1].parent<p[root2].parent){ p[root1].parent += p[root2].parent; p[root2].parent = root1; } else{ p[root2].parent += p[root1].parent; p[root1].parent = root2; } }}
- 05-树7. File Transfer
- 05-树7. File Transfer (25)
- 05-树7. File Transfer (25)
- PAT 05-树7. File Transfer (25)
- 05-树8 File Transfer
- 05-树8 File Transfer
- 05-树8 File Transfer
- 05-树8 File Transfer
- 05-树8 File Transfer
- 05-树8 File Transfer
- 05-树8 File Transfer (25分)
- 05-树8 File Transfer (25分)
- 05-树9 File Transfer (25分)
- 05-树9 File Transfer (25分)
- 05-树9 File Transfer
- 05-树9 File Transfer (25分)
- 05-树8 File Transfer (25分)
- 05-树8 File Transfer (25分)
- Unity3D与Android项目交互中无法监听back键返回的解决办法
- 乔姆斯基的文法分类
- Linux 文件按大小排序
- Tomcat: Tomcat系列之组件详解及服务安装配置
- eclipse远程连接hbase遇到的问题
- 05-树7. File Transfer
- PHP定时执行任务
- Terms in English
- Javascript日期时间表现形式互转
- 卸载使用“python setup.py install”安装的包
- 装Arcgis好难!!
- gdareqghfdsahfs
- 文章标题
- apache日志分析简介