日常,满分
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 betweenc1
andc2
; orC c1 c2
where
C
stands for checking if it is possible to transfer files betweenc1
andc2
; orS
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
andc2
, 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 arek
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 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.
起初我是老实运用,但很快超时了
struct UnionFind{ int data; int Parent; };
4 最大N,递增链,卡不按大小union的 376 150 运行超时
0 / 4 5 最大N,递减链,卡不按大小union的 376 150 运行超时
0 / 4 6 最大N,两两合并,反复查最深结点,卡不压缩路径的 364 150 运行超时
0 / 1
#include<stdio.h>
#include<stdlib.h>
typedef struct UnionFind *UF;
struct UnionFind{
int data;
int Parent;
};
UF Build_UnionFindTree(int num);
UF Init_UnionFindTree(int num);
int Find_Value(UF T, int num, int data);
void Check_Tree(UF T, int num, int n1, int n2);
void Union_Tree(UF T, int num, int n1, int n2);
int Component_num(UF T, int num);
int main()
{
UF T;
int num, count;
scanf("%d", &num);
T = Build_UnionFindTree(num);
count = Component_num(T, num);
if(count == 1){
printf("The network is connected.\n");
}else{
printf("There are %d components.\n", count);
}
return 0;
}
UF Build_UnionFindTree(int num)
{
UF T;
char ch;
int n1, n2;
T = Init_UnionFindTree(num);
while(scanf(" %c", &ch), ch != 'S'){
switch(ch){
case 'C':
scanf("%d%d", &n1, &n2);
Check_Tree(T, num, n1, n2);
break;
case 'I':
scanf("%d%d", &n1, &n2);
Union_Tree(T, num, n1, n2);
break;
default:
printf("出现不合规定字符!\n");
while((ch = getchar()) != '\n' && ch != EOF);
break;
}
}
return T;
}
UF Init_UnionFindTree(int num)
{
UF T;
T = (UF)malloc(sizeof(struct UnionFind) * num);
for(int i = 0; i < num; i++){
T[i].data = i + 1;
T[i].Parent = -1;
}
return T;
}
int Find_Value(UF T, int num, int data)
{
int i;
for(i = 0; i < num && T[i].data != data; i++){
if(i >= num){
return -1;
}
}
for(; T[i].Parent >= 0; i = T[i].data);
return i;
}
void Check_Tree(UF T, int num, int n1, int n2)
{
if(Find_Value(T, num, n1) != Find_Value(T, num, n2)){
printf("no\n");
}else{
printf("yes\n");
}
}
void Union_Tree(UF T, int num, int n1, int n2)
{
int r1, r2;
r1 = Find_Value(T, num, n1);
r2 = Find_Value(T, num, n2);
if(T[r1].Parent < T[r2].Parent){
T[r1].Parent += T[r2].Parent;
T[r2].Parent = r1;
}else{
T[r2].Parent += T[r1].Parent;
T[r1].Parent = r2;
}
}
int Component_num(UF T, int num)
{
int count = 0;
for(int i = 0; i< num; i++){
if(T[i].Parent < 0){
count++;
}
}
return count;
}
接下来便运用数组了解题,这个是满分
其实都一样,唯一的区别便是
int Find(int *BF, int data) { if(BF[data] < 0){ return data; } return Find(BF, BF[data]); } int Find_Value(UF T, int num, int data) { int i; for(i = 0; i < num && T[i].data != data; i++){ if(i >= num){ return -1; } } for(; T[i].Parent >= 0; i = T[i].data); return i; }
奇怪的是,我的上一个改变Find函数,便会出现问题,不该就超时
#include<stdio.h>
#include<stdlib.h>
int Find(int *BF, int data);
void Union(int *BF, int n1, int n2);
void Check(int *BF, int n1, int n2);
int Component_num(int *BF, int num);
int main()
{
int *BF;
int num, count, n1, n2;
char ch;
scanf("%d", &num);
BF = (int*)malloc(sizeof(int) * (num+1));
for(int i = 0; i <= num; i++){
BF[i] = -1;
}
while(scanf(" %c", &ch), ch != 'S'){
switch(ch){
case 'C':
scanf("%d%d", &n1, &n2);
Check(BF, n1, n2);
break;
case 'I':
scanf("%d%d", &n1, &n2);
Union(BF, n1, n2);
break;
default:
printf("出现不合规定字符!\n");
while((ch = getchar()) != '\n' && ch != EOF);
break;
}
}
count = Component_num(BF, num);
if(count == 1){
printf("The network is connected.\n");
}else{
printf("There are %d components.\n", count);
}
return 0;
}
int Find(int *BF, int data)
{
if(BF[data] < 0){
return data;
}
return Find(BF, BF[data]);
}
void Union(int *BF, int n1, int n2)
{
int r1 = Find(BF, n1);
int r2 = Find(BF, n2);
if(BF[r1] < BF[r2]){
BF[r1] += BF[r2];
BF[r2] = r1;
}else{
BF[r2] += BF[r1];
BF[r1] = r2;
}
return ;
}
void Check(int *BF, int n1, int n2)
{
if(Find(BF, n1) != Find(BF, n2)){
printf("no\n");
}else{
printf("yes\n");
}
}
int Component_num(int *BF, int num)
{
int count = 0;
for(int i = 1; i<= num; i++){
if(BF[i] < 0){
count++;
}
}
return count;
}