题目地址
https://pta.patest.cn/pta/test/16/exam/4/question/670
5-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 NN (2\le N\le 10^42≤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 NN. 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.
这题是考并查集
/*
运行详情
评测结果
时间 结果 得分 题目 编译器 用时(ms) 内存(MB) 用户
2017-06-29 18:12 部分正确 24 5-8 gcc 27 1
测试点结果
测试点 结果 得分/满分 用时(ms) 内存(MB)
测试点1 答案正确 7/7 18 1
测试点2 答案正确 7/7 1 1
测试点3 答案正确 1/1 2 1
测试点4 答案正确 1/1 2 1
测试点5 答案正确 4/4 14 1
测试点6 答案正确 4/4 15 1
测试点7 答案错误 0/1 27 1
没有ac,不知道最后一个测试点用了什么数据
*/
#include<stdio.h>
#define MAXLEN 15000
#define TOPLEVEL -1
int gComputers[MAXLEN];
void InitComputers(int n)
{
int i;
for(i=1;i<=n;i++)
{
gComputers[i]=TOPLEVEL;
}
}
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void Connect(int a,int b)
{
/* if(gComputers[a]==TOPLEVEL)
{
gComputers[b]=a;
return;
}
*/
int temp;
temp=a;
while(gComputers[temp]!=TOPLEVEL)
{
temp=gComputers[temp];
}
// gComputers[a]=temp;
gComputers[b]=temp;
return;
}
/*
int GetTop(int a)
{
int temp=a;
if(gComputers[a]==TOPLEVEL)
return a;
while(gComputers[temp]!=TOPLEVEL)
temp=gComputers[temp];
gComputers[a]=temp; //查找完了合并父节点;
return temp;
}
*/
int GetTop(int k)
{
if(gComputers[k]<0)
{
return k;
}
return gComputers[k] = GetTop(gComputers[k]);
}
void Check(int a,int b)
{
if(GetTop(a)==GetTop(b))
{
printf("yes\n");
}
else
{
printf("no\n");
}
}
void ShowSummry(int n)
{
int i,count=0;
for(i=1;i<=n;i++)//此处扫描应从1号开始,电脑从1开始编号,0号没操作始终是-1;
{
if(gComputers[i]==TOPLEVEL)
{
count++;
}
}
if(count==1)
{
printf("The network is connected.");
}
else
printf("There are %d components.",count);
}
void OutputStatus(int n)
{
int i;
for(i=0;i<=n;i++)
{
printf("--%d:%d\n",i,gComputers[i]);
}
}
int main()
{
int N;
int a,b;
char command;
scanf("%d",&N);
InitComputers(N);
while(1)
{
scanf("%c",&command);
if(command=='S')
{
ShowSummry(N);
// OutputStatus(N);
break;
}
scanf("%d %d",&a,&b);
if(command=='I')
Connect(a,b);
if(command=='C')
Check(a,b);
}
}
不知道最后一个测试点什么情况,mooc开课之外的题库里面不带提示,找了一张别人做题的截图,发现最后一个点是卡不压缩的,容易超时
问题是我有一直合并路线,而且时间也不长。不知道遇上什么问题了。