Focus5679的博客

记录个人代码用

05-树8 File Transfer

05-树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 N (2N104), 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." 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.

题解:

#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>

#define mod 1000000007
const int INF = 0x7f7f7f7f;
typedef long long ll;
const int maxn = 10050;
using namespace std;

int vec[maxn] = {0};
int t;

int Find(int x)
{
    int i;
    if(vec[x] < 0)
        return x;
    for(i = vec[x]; vec[i]>=0; i = vec[i]);
    return i;
}
void Union( int x, int y)
{
    int root1, root2;
    root1 = Find(x);
    root2 = Find(y);
    if(root1!=root2)
    {
        if(vec[root1]<vec[root2])
        {
            vec[root1] += vec[root2];
            vec[root2] = root1;
            t--;
        }
        else
        {
            vec[root2] += vec[root1];
            vec[root1] = root2;
            t--;
        }
    }
}
int main()
{

    scanf("%d", &t);
    getchar();
    for(int i = 1; i <= t; i++)
    {
        vec[i] = -1;
    }
    while(1)
    {
        char temp;
        scanf("%c", &temp);
        if(temp == 'C')
        {
            int m, n;
            scanf("%d%d", &m, &n);
            getchar();
            if(Find(m)==Find(n))
            {
                printf("yes\n");
            }
            else
            {
                printf("no\n");
                //Union(m, n);
            }
        }
        else
        {
            if(temp == 'I')
            {
                int m, n;
                scanf("%d%d", &m, &n);
                getchar();
                Union(m,n);
            }
            else
            {
                if(t>1)
                {
                    printf("There are %d components.\n", t);
                    break;
                }
                else
                {
                    printf("The network is connected.\n");
                    break;
                }
            }
        }
    }

    return 0;
}



阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40758751/article/details/80355045
个人分类: PTA
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

05-树8 File Transfer

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭