码迷,mamicode.com
首页 > 其他好文 > 详细

PAT File Transfer

时间:2015-05-26 23:02:25      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

File Transfer

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.



题意很简单 连我的英语水平都看懂了 我就不赘述了 考察并查集

设计一个数组 A[N]=M 表示N这个数的父节点是M

这就很容易了

两个核心函数就能解决 getfather() 和 union() 详细看mooc 何老师的课

最近几天的题都很基础

下面给出AC代码 非原创来自网络加以整理

 1 #include <stdio.h>
 2 
 3 #define N 10010
 4 int father[N];
 5 int ans[N]={0};
 6 int num=0;
 7 
 8 int getfa(int index)
 9 {
10     if(father[index]==index)                    
11             return index;
12     else 
13         return father[index]=getfa(father[index]);
14 }
15 
16 
17 void Union(int x,int y)
18 {
19   father[getfa(y)]=getfa(x);
20 }
21 
22 
23 void Process()
24 {
25     char cmd;
26     int i,x,y,a=0;
27      while(1)
28     {
29            
30            cmd=getchar();
31            if(cmd==S)
32             break;
33         scanf("%d%d",&x,&y);
34           getchar();
35            if(cmd==I)
36              Union(x,y);
37            if(cmd==C)
38             if(getfa(x)==getfa(y))
39                    printf("yes\n");
40            else
41                    printf("no\n");
42      }
43     for(i=1;i<=num;i++)
44         ans[getfa(i)]=1;
45 
46     for(i=1;i<=num;i++)
47         if(ans[i])
48            a++;
49     if(a==1)
50          printf("The network is connected.");
51     else
52         printf("There are %d components.",a);
53 
54 }
55 
56 int main()
57 {
58     int i;
59     scanf("%d",&num);
60     getchar();    
61     for(int i=0;i<=num;i++)
62            father[i]=i;
63     Process();    
64     return 0;    
65 }

 








PAT File Transfer

标签:

原文地址:http://www.cnblogs.com/threezj/p/4531718.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!