04-2. 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 andc2; or
C c1 c2
where C stands for checking if it is possible to transfer files betweenc1 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 betweenc1 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." wherek 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 SSample 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 SSample Output 2:
no no yes yes The network is connected.
解题:以下程序代码基本是按照陈越老师上课讲解来写的。具体解法见:中国慕课—数据结构—第五讲——小白专场[陈越]——File Transfer
import java.util.Scanner;
public class FileTransfer {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int N=sc.nextInt();
int[] arr=new int[N+1]; //数组的index代表元素,内容代表根的index;
reset(arr,N);
String str=sc.next();
char key=str.charAt(0);
while(key!='S'){
switch (key){
case 'C':
checking(arr,sc.nextInt(),sc.nextInt());
break;
case 'I':
connect(arr,sc.nextInt(),sc.nextInt());
break;
case 'S':
print(arr,N);
break;
}
str=sc.next();
key=str.charAt(0);
}
print(arr,N);
sc.close();
/**
*校验两个数是否属于同一个集合
* 即根是否相同
*/
}
public static void checking(int[] arr,int num1,int num2){
int root1=findRoot(arr,num1);
int root2=findRoot(arr,num2);
if(root1==root2)
System.out.println("yes");
else
System.out.println("no");
}
/**
*将两个数连接起来
* 先判断这两个数的根是否相同
*/
public static void connect(int[] arr,int num1,int num2){
int root1=findRoot(arr,num1);
int root2=findRoot(arr,num2);
if(root1==root2)
return;
//根的元素为-(集合元素个数),所以越小,代表该集合元素个数越多
if(root1<root2)
union(arr,root2,root1);
else
union(arr,root1,root2);
}
public static void union(int[] arr,int root1,int root2){
arr[root2]=arr[root1]+arr[root2];
arr[root1]=root2;
}
/**
*寻找根节点
*根节点所对应数组元素是负数
*/
public static int findRoot(int[] arr,int num){
while(arr[num]>0){
num=arr[num];
}
return num;
}
/**
*操作结束,判断最终有几个集合
*/
public static void print(int[] arr,int N){
int count=0;
for(int i=1;i<=N;i++){
if(arr[i]<0)
count++;
}
if(count==1)
System.out.println("The network is connected");
else
System.out.println("There are "+count+" components");
}
/**
*将数组重置
* 即将数组内容都设置为-1
*/
public static void reset(int[] arr,int N){
for(int i=0;i<=N;i++){
arr[i]=-1;
}
}
}