您的位置:首页 > Web前端

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≤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 transferfiles 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 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.

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: