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.
c++代码
#include<iostream>
using namespace std;
int parent[100000];
int find(int x)
{
return parent[x] < 0 ? x : parent[x] = find(parent[x]);
}
void init()
{
for(int i = 0; i < 100000; i++) parent[i] = -1;
}
void Union(int x1,int x2)
{
int p1 = find(x1);
int p2 = find(x2);
if(p1 != p2) //少了这句可能会出错
if(S[p1] > S[p2]) {
parent[p2] += parent[p1];
parent[p1] = p2;
} //p2个数多
else{
parent[p1] += parent[p2];
parent[p2] = p1;
}
}
int main()
{
int n;
cin>>n;
char c;
int c1,c2;
init();
while(true){
cin>>c;
if(c == 'S'){
break;
}
cin>>c1>>c2;
if(c == 'C'){ //C查询两台电脑有没有连接
if(find(c1) != find(c2)){
cout<<"no"<<endl;
}else{
cout<<"yes"<<endl;
}
}else if(c == 'I'){
Union(c1,c2);
}
}
int count = 0;
for(int i = 1; i <= n; i++){
if(parent[i] < 0){
count++;
}
}
if(count == 1){ //全连通
cout<<"The network is connected.";
}else{
cout<<"There are "<<count<<" components.";
}
}
java代码 超时了过不去
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
static BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
static final int N = 10001;
static int S[] = new int[N]; //存储集合
static void Init() { //初始化集合为本身 每一个节点用1~N表示
for(int i = 1; i < N; i++) S[i] = -1;
}
static int find(int x) { //寻找元素所在集合,返回根结点
if(S[x] < 0) return x;
else return S[x] = find(S[x]);
}
static void Union(int x1, int x2) { //合并
int p1 = find(x1);
int p2 = find(x2);
//按元素个数
if(p1 != p2)
if(S[p1] > S[p2]) { //p2高
S[p2] += S[p1];
S[p1] = p2; //p1根结点的父节点指向p2
}else {
// if(p1 == p2) S[p1] --; //按高度
S[p1] += S[p2];
S[p2] = p1;
}
}
public static void main(String[] args) throws IOException {
int n = Integer.parseInt(sc.readLine());
Init();
while(true) {
String[] s = sc.readLine().split(" ");
char c = s[0].charAt(0);
if(c == 'S') break;
int c1 = Integer.parseInt(s[1]);
int c2 = Integer.parseInt(s[2]);
if(c == 'C') {
if(find(c1) == find(c2)) {
System.out.println("yes");
}else {
System.out.println("no");
}
}else if(c == 'I') {
Union(c1,c2);
}
}
int cnt = 0;
for(int i = 1; i <= n; i++) {
if(S[i] < 0) { //判断有几个根结点
cnt++;
}
}
if(cnt == 1) { //全连通
System.out.println("The network is connected.");
}else {
System.out.println("There are " + cnt + " components.");
}
}
}