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." 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. 这题典型的是一道并查树的题。思路也非常简单,课件上也有现成。但是这道题直接用课本上的unite方法的话会超时。改进的办法有两个:1.unite时判断两个集合哪个集合的元素多,然后把元素少的那个集合并到大的里面,即直接把元素少的集合的根挂到另一个集合的根上。2.unite时,也是先判断哪个元素多,然后把元素少的那个集合的每一个元素都挂到元素多的那个集合的根上,这样做的话可以保证每个集合的高度只能是2,减少了find操作的耗时,但是会增加unite操作调整元素的时间。亲测用两种方法都能AC,下面给出两种方法的AC代码。
 #include<iostream>
#include"stdio.h"
using namespace std; int* a; void unite(int x1,int x2)
{
int root1 = x1-;
while (a[root1]>=)
root1=a[root1];
int root2 = x2-;
while (a[root2]>=)
root2=a[root2]; if ((a[root1]) <= (a[root2])) //root1的集合较大
{
a[root1] += a[root2];
a[root2] = root1;
}
else
{
a[root2] += a[root1];
a[root1] = root2;
}
} void judge(int x1,int x2)
{
int root1 = x1-;
int root2 = x2-;
while (a[root1]>=)
root1 = a[root1];
while (a[root2]>=)
root2 = a[root2];
if ( root1 == root2 )
printf("yes\n");
else
printf("no\n");
} int main()
{
int N=;
cin >> N;
a = new int [N]; for (int i=;i<N;i++)
{
a[i] = -;
} char operation='a';
int c1=,c2=; cin >> operation;
while (operation != 'S')
{
cin >> c1 >> c2;
if (operation == 'I')
{
unite(c1,c2);
}
else if (operation == 'C')
{
judge(c1,c2);
}
cin >> operation;
}
int component=;
for (int i=;i<N;i++)
if (a[i] < )
++component; if (component == )
cout << "The network is connected." << endl;
else
cout << "There are " << component << " components." << endl; return ;
}
 #include<iostream>
#include<vector>
#include"stdio.h"
using namespace std; struct PC
{
int data;
int parent;
vector<int> children;//若为根节点,则此容器放的是整个集合的元素值
};
PC* a; int find(int x) //查找x属于哪个集合,返回根节点的下标
{
vector<int> vec;
for (;a[x-].parent >=; x=a[x-].parent+);
return x;
} void unite(int x1,int x2)
{
int root1 = find(x1);
int root2 = find(x2);
if (-(a[root1-].parent) >= -(a[root2-].parent)) //root1的集合较大
{
a[root1-].parent += a[root2-].parent;
while (!a[root2-].children.empty())
{
a[root1-].children.push_back(a[root2-].children.back());
a[ a[root2-].children.back()- ].parent = root1-;
a[root2-].children.pop_back();
}
}
else
{
a[root2-].parent += a[root1-].parent;
while (!a[root1-].children.empty())
{
a[root2-].children.push_back(a[root1-].children.back());
a[ a[root1-].children.back()- ].parent = root2-;
a[root1-].children.pop_back();
}
}
} int main()
{
int N=;
scanf("%d",&N);
a = new PC[N];
for (int i=;i<N;i++)
{
a[i].data = i+;
a[i].parent = -;
a[i].children.push_back(i+);
} char operation='a',temp;
int c1=,c2=; scanf(" %c",&operation);
while (operation != 'S')
{
scanf("%d %d",&c1,&c2);
if (operation == 'C')
{
if ( find(c1) == find(c2) )
printf("yes\n");
else
printf("no\n");
}
if (operation == 'I')
{
unite(c1,c2);
}
scanf(" %c",&operation);
}
int component=;
for (int i=;i<N;i++)
{
if (a[i].parent < )
++component;
}
if (component == )
{
cout << "The network is connected." << endl;
}
else
{
cout << "There are " << component << " components." << endl;
}
return ;
}
												

PAT 5-8 File Transfer (25分)的更多相关文章

  1. PTA 05-树8 File Transfer (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/670 5-8 File Transfer   (25分) We have a netwo ...

  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. pat04-树5. File Transfer (25)

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

  4. PAT 1009 Product of Polynomials (25分) 指数做数组下标,系数做值

    题目 This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: E ...

  5. PAT A1122 Hamiltonian Cycle (25 分)——图遍历

    The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...

  6. PAT A1142 Maximal Clique (25 分)——图

    A clique is a subset of vertices of an undirected graph such that every two distinct vertices in the ...

  7. [PAT] 1142 Maximal Clique(25 分)

    1142 Maximal Clique(25 分) A clique is a subset of vertices of an undirected graph such that every tw ...

  8. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  9. PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)

    1146 Topological Order (25 分)   This is a problem given in the Graduate Entrance Exam in 2018: Which ...

随机推荐

  1. eclipse中将项目发布到tomcat的root目录

    在eclipse中,将项目直接部署在tomcat的root目录中,这样便可以直接ip:port访问项目: 项目右键->属性->web project settings

  2. GbkToUtf8 Utf8ToGbk PackHttp

    void CFunc::GbkToUtf8(CString &strGBK) { , (LPCTSTR)strGBK, -, NULL, ); unsigned ]; memset(wszUt ...

  3. VHDL学习之模块调用

    http://wenku.baidu.com/link?url=SsRPUVQAOKDR8yWfDhQlceCwfZQkI-KQMLFKTDGAh3KAPr2NwEgvj0d_EZjdnsB99Upp ...

  4. php知识案列1

    用PHP,在1-20间随机产生5个不重复的值,如何做 复制代码 代码如下: <?php function NoRand($begin=0,$end=20,$limit=5){ $rand_arr ...

  5. ios基础篇(二十三)—— 定时器NSTimer与图片的自动切换

    一.NSTimer NSTimer是一个能在从现在开始到后面的某一个时刻或者周期性的执行我们指定的方法的对象.可以按照一定的时间间隔,将制定的信息发送给目标对象.并更新某个对象的行为.你可以选择在未来 ...

  6. linux 硬盘速度测试

    [root@iZ25oat874uZ data]# time dd if=/dev/zero of=/var/test bs=8k count=1000000 1000000+0 records in ...

  7. Linux教程:SSH免密码登录的方法

    公司里有N台服务器需要经常登录,每次ssh的时候都要输入密码实在太不爽了,今天有空一口气全部改为公钥/私钥认证,登录再也不用任何密码了. 实现步骤: 1.在你的自己的机器下面使用ssh-keygen命 ...

  8. Making the Grade(POJ3666)

    题目大意: 给出长度为n的整数数列,每次可以将一个数加1或者减1,最少要多少次可以将其变成单调增或者单调减(不严格). 题解: 1.一开始我有一个猜想,就是不管怎么改变,最终的所有数都是原来的某个数. ...

  9. number 数据类型的分析。

    在js中,number数据类型可能算最令人关注的的类型之一了. number类型分为整数和浮点数. 一,整型数,整型又分为十进制,八进制,十六进制. 十进制即是生活中接触到的:而八进制数的首位必须是零 ...

  10. JSP基础语法---九九乘法表-java jsp

    <%@ page language="java" import="java.util.*" contentType="text/html; ch ...