05-树8 File Transfer
来源:互联网 发布:网络教育美术学 编辑:程序博客网 时间:2024/06/05 18:45
这个其实是利用集合的概念,利用一个一维数组来表示。数组的下标和元素对应,其中存储的值为各自的父亲的下标(正数)。若为负数,则表示这个节点是根,根所在节点的值的绝对值表示这棵树的总节点数。因此,连接时,找到两个元素各自的根节点,若相同就不管了,否则,将较小的树连到较大的树上(小树根节点的值等于大树的根节点的下标) 。 查找是否属于同一棵树,找到两个元素的根节点,比较即可。
另外注意不在这个总的集合里面的情况。
#include <stdio.h>#include <stdlib.h>void con_set(int set[],int a1,int a2);int find_set(int set[],int a1);void check_set(int set[],int a1,int a2);int Maxsize;int main(){int size;scanf("%d",&size);int set[size];int i;for(i=0;i<size+1;i++){set[i]=-1; //根初始用-1来表示 ,每多一个元素,自减1 }Maxsize=size;int a1;int a2;char cmd;getchar();scanf("%c",&cmd);while(cmd!='S'){scanf("%d %d",&a1,&a2);if(cmd=='C'){check_set(set,a1,a2);}else{con_set(set,a1,a2);}getchar();scanf("%c",&cmd);}int record=0;for(i=1;i<size+1;i++){if(set[i]<0){record++;}}if(record==1){ //只有一个根,表示所有的节点都连着了 printf("The network is connected.");}else{printf("There are %d components.",record);}return 0;}void check_set(int set[],int a1,int a2){ int root1=find_set(set,a1);int root2=find_set(set,a2);if((root1!=-1) && (root2 !=-1)){if(root1!=root2){ //根不同 printf("no\n");}else{printf("yes\n"); //根相同 }}else{printf("no\n"); //不存在 }}int find_set(int set[],int a1){ //找到根 if(a1>Maxsize || a1<=0){return -1;}else{int i=0;i=a1; while(set[i]>0){i=set[i];}return i; //返回的是根的下标 }}void con_set(int set[],int a1,int a2){int b1=find_set(set,a1);int b2=find_set(set,a2);if(b1!=-1 && b2!=-1){ //两者的根都存在 if(set[b1]<=set[b2]){ //根用负数表示,负的越多,表示节点越多。合并时,小的树合到大的树上。 set[b2]=b1;set[b1]-=1;}else{set[b1]=b2;set[b2]-=1;}}}
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 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:
nonoyesyesThe network is connected.
- 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-树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-树7. File Transfer
- 05-树8 File Transfer【并查集】
- 并查集-05-树8 File Transfer
- 05-树8 File Transfer (25分)---并查集
- 05-树8 File Transfer(25 分)
- android常用基础类库
- 洛谷1007 独木桥
- IOS开发笔记21-ARC与分类
- Hibernate教程 class1
- hdu 4632 Palindrome subsequence(dp)
- 05-树8 File Transfer
- Scala函数
- 数据库命名规则
- c++ 程序输入参数
- C-符号与运算符
- java Io 全解
- Mysql 测试数据类型
- get和post的区别
- Java的一个小程序