05-树8 File Transfer
2015-10-28 21:35
211 查看
这个其实是利用集合的概念,利用一个一维数组来表示。数组的下标和元素对应,其中存储的值为各自的父亲的下标(正数)。若为负数,则表示这个节点是根,根所在节点的值的绝对值表示这棵树的总节点数。因此,连接时,找到两个元素各自的根节点,若相同就不管了,否则,将较小的树连到较大的树上(小树根节点的值等于大树的根节点的下标) 。 查找是否属于同一棵树,找到两个元素的根节点,比较即可。另外注意不在这个总的集合里面的情况。
#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 c2where
Istands for inputting a connection between
c1and
c2;or
C c1 c2where
Cstands for checking if it is possible to transferfiles between
c1and
c2;or
Swhere
Sstands for stopping this case.
Output Specification:
For eachCcase, print in one line the word "yes"or "no" if it is possible or impossible to transfer files between
c1and
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
kcomponents."where
kis the number of connected components in thisnetwork.
Sample Input 1:
5C 3 2I 3 2C 1 5I 4 5I 2 4C 3 5S
Sample Output 1:
no no yes There 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:
no no yes yes The network is connected.
相关文章推荐
- JSP的学习(6)——九大隐式对象及其out对象
- JS 代码调试经验总结(菜鸟必读)
- JS的bug调试与 常见bug
- 字体图标 iconfont cssfont
- JQ数组=jQuery对象数组
- css定位
- 判断js中的数据类型
- 杭电-1015Safecracker(DFS)
- 通过SharedPreferences存储和显示信息
- HTML5 中的可缩放矢量图形(SVG)
- JS高级程序设计读书笔记(第十三章 事件)
- node.js_in_practice(intermediate)
- JS图片放大查看的3种方案
- 深入理解javascript之IIFE
- 常见的2种HTML5错误语法
- JS完美运动框架
- 8、第八次课jquery第一节20151006
- html-表单的应用
- 京东首页很炫的js效果、图片自动滑动,悬停效果,鼠标离开效果
- javascript基础之confirm