您的位置:首页 > Web前端

PAT 5-8 File Transfer (25分)

2016-02-22 19:37 459 查看
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-1;
while (a[root1]>=0)
root1=a[root1];
int root2 = x2-1;
while (a[root2]>=0)
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-1;
int root2 = x2-1;
while (a[root1]>=0)
root1 = a[root1];
while (a[root2]>=0)
root2 = a[root2];
if ( root1 == root2 )
printf("yes\n");
else
printf("no\n");
}

int main()
{
int N=0;
cin >> N;
a = new int
;

for (int i=0;i<N;i++)
{
a[i] = -1;
}

char operation='a';
int c1=0,c2=0;

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=0;
for (int i=0;i<N;i++)
if (a[i] < 0)
++component;

if (component == 1)
cout << "The network is connected." << endl;
else
cout << "There are " << component << " components." << endl;

return 0;
}
#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-1].parent >=0; x=a[x-1].parent+1);
return x;
}

void unite(int x1,int x2)
{
int root1 = find(x1);
int root2 = find(x2);
if (-(a[root1-1].parent) >= -(a[root2-1].parent)) //root1的集合较大
{
a[root1-1].parent += a[root2-1].parent;
while (!a[root2-1].children.empty())
{
a[root1-1].children.push_back(a[root2-1].children.back());
a[ a[root2-1].children.back()-1 ].parent = root1-1;
a[root2-1].children.pop_back();
}
}
else
{
a[root2-1].parent += a[root1-1].parent;
while (!a[root1-1].children.empty())
{
a[root2-1].children.push_back(a[root1-1].children.back());
a[ a[root1-1].children.back()-1 ].parent = root2-1;
a[root1-1].children.pop_back();
}
}
}

int main()
{
int N=0;
scanf("%d",&N);
a = new PC
;
for (int i=0;i<N;i++)
{
a[i].data = i+1;
a[i].parent = -1;
a[i].children.push_back(i+1);
}

char operation='a',temp;
int c1=0,c2=0;

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=0;
for (int i=0;i<N;i++)
{
if (a[i].parent < 0)
++component;
}
if (component == 1)
{
cout << "The network is connected." << endl;
}
else
{
cout << "There are " << component << " components." << endl;
}
return 0;
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: