题目地址

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≤10​4​​), 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开课之外的题库里面不带提示,找了一张别人做题的截图,发现最后一个点是卡不压缩的,容易超时

问题是我有一直合并路线,而且时间也不长。不知道遇上什么问题了。

PTA 05-树8 File Transfer (25分)的更多相关文章

  1. PAT 5-8 File Transfer (25分)

    We have a network of computers and a list of bi-directional connections. Each of these connections a ...

  2. 05-树8 File Transfer (25 分)

    We have a network of computers and a list of bi-directional connections. Each of these connections a ...

  3. PTA甲级1094 The Largest Generation (25分)

    PTA甲级1094 The Largest Generation (25分) A family hierarchy is usually presented by a pedigree tree wh ...

  4. L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...

  5. pat04-树5. File Transfer (25)

    04-树5. File Transfer (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue We have ...

  6. PTA 04-树5 Root of AVL Tree (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/668 5-6 Root of AVL Tree   (25分) An AVL tree ...

  7. PTA 10-排序5 PAT Judge (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/677 5-15 PAT Judge   (25分) The ranklist of PA ...

  8. PTA 05-树7 堆中的路径 (25分)

    题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/713 5-5 堆中的路径   (25分) 将一系列给定数字插入一个初始为空的小顶堆H[] ...

  9. pta 编程题13 File Transfer

    其它pta数据结构编程题请参见:pta 这道题考察的是union-find并查集. 开始把数组中每个元素初始化为-1,代表没有父节点.为了使树更加平衡,可以让每一个连通分量的树根的负值代表这个连通分量 ...

随机推荐

  1. 139 Word Break 单词拆分

    给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,确定 s 是否可以被空格分割为一个或多个在字典里出现的单词.你可以假设字典中无重复的单词.例如,给出s = "leet ...

  2. 【转】log4j的日志

    一.Log4j配置 第一步:加入log4j-1.2.8.jar到lib下. 第二步:在CLASSPATH下建立log4j.properties.内容如下: 放在src下的话就不用配置 否则得去web. ...

  3. logging模块基础

    很多程序都有记录日志的需求,日志不仅可以保存访问记录,也可以有错误,警告等信息输出. python的logging模块提供了标准的日志接口,可以通过logging存储各种格式的日志.logging模块 ...

  4. CF779C(round 402 div.2 C) Dishonest Sellers

    题意: Igor found out discounts in a shop and decided to buy n items. Discounts at the store will last ...

  5. P3372 【模板】线段树 1 区间查询与区间修改

    题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个 ...

  6. Arduino Ethernet W5100扩展板的指示灯含义

    Arduino Ethernet W5100扩展板是继承WIZnet W5100网络芯片的扩展板.将扩展板连接到Arduino后,可使Arduino具有网络功能.此扩展板上有多个指示灯,由于轻易查不到 ...

  7. 在idea启动tomcat出现The JAVA_HOME environment variable is not defined correctly的解决

    情况:某套代码是用jdk 1.6编译,然后电脑的JAVA_HOME系统变量配的是jdk1.7的,在tomcat启动时报错 The JAVA_HOME environment variable is n ...

  8. phpstorm 格式化代码

    MAC 安装phpcs.phpcbf composer global require 'squizlabs/php_codesniffer=*' Changed current directory t ...

  9. UVA 11020 Efficient Solutions (BST,Splay树)

    题意:给n个坐标.一个坐标(x,y)若有无存在的坐标满足x1<x && y1<=y  或  x1<=x && y1<y 时,此坐标(x,y)是就 ...

  10. HDU 5416 CRB and Tree (技巧)

    题意:给一棵n个节点的树(无向边),有q个询问,每个询问有一个值s,问有多少点对(u,v)的xor和为s? 注意:(u,v)和(v,u)只算一次.而且u=v也是合法的. 思路:任意点对之间的路径肯定经 ...