05-树8 File Transfer (25 分)

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 (2≤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 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.

特意查了查c++的 二维数组 怎么声明 ,函数 参数是二维数组的情况怎么做,返回是一维数组怎么弄,查到用的是指针.就那么用吧,估计就该这样吧.

c#的代码不能满分c++的可以

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Net;
using System.Text;
using System.Xml;

class T
{

    public class MyItem
    {
        public int Value;
        public int PIndex;

    }
    static void Main(string[] args)
    {

        List<MyItem> list = new List<MyItem>();

        var count = int.Parse(Console.ReadLine());
        for (int i = 1; i < count + 1; i++)
        {
            list.Add(new MyItem() { Value = i, PIndex = -1 });
        }
        AAA:

        var a = Console.ReadLine();
        if (a != "S")
        {
            var item = a.Split(‘ ‘);
            var v1 = 查找(list, item[1]);
            var v2 = 查找(list, item[2]);
            switch (item[0])
            {
                case "C":

                    if (v1.Value == v2.Value)
                    {
                        Console.WriteLine("yes");
                    }
                    else
                    {
                        Console.WriteLine("no");
                    }
                    break;
                case "I":

                    if (v1.PIndex <= v2.PIndex)
                    {

                        v1.PIndex += v2.PIndex;
                        v2.PIndex = v1.Value;

                    }
                    else
                    {
                        v2.PIndex += v1.PIndex;
                        v1.PIndex = v2.Value;
                    }

                    break;

                default:
                    break;
            }
            goto AAA;
        }
        else
        {
            var v = list.FindAll(ax => ax.PIndex < 0);
            if (v.Count == 1)
            {
                Console.WriteLine("The network is connected.");
            }
            else
            {
                Console.WriteLine($"There are {v.Count} components.");
            }
            return;
        }

    }

    private static MyItem 查找(List<MyItem> list, string item)
    {
        var myItem = list[int.Parse(item)-1];

        if (myItem != null)
        {
            while (myItem.PIndex > 0)
            {
                myItem = list.Find(x => x.Value == myItem.PIndex);
            }
            return myItem;
        }
        else
        {
            return null;
        }
    }
}
 
// C1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

//#include "pch.h"
#include <iostream>
using namespace std;

int* find(int **list, int i1);

int ccc = 0;
int main()
{

    int **list;

    std::cin >> ccc;

    list = new int*[ccc];

    for (int i = 0; i < ccc; i++)
    {
        list[i] = new int[2];
        list[i][0] = i + 1;
        list[i][1] = -1;
    }

AAA:

    char a = ‘a‘;
    std::cin >> a;

    if (a != ‘S‘)
    {
        int i1;
        int i2;
        std::cin >> i1;
        std::cin >> i2;

        int *v1 = find(list, i1);
        int *v2 = find(list, i2);
        switch (a)
        {
        case ‘C‘:

            if (v1[0] == v2[0])
            {
                cout << ("yes\n");
            }
            else
            {
                cout << ("no\n");
            }
            break;
        case ‘I‘:

            if (v1[1] <= v2[1])
            {

                v1[1] += v2[1];
                v2[1] = v1[0];

            }
            else
            {
                v2[1] += v1[1];
                v1[1] = v2[0];
            }

            break;

        default:
            break;
        }
        goto AAA;
    }
    else
    {
        int ci = 0;
        for (int i = 0; i < ccc; i++)
        {
            if (list[i][1] < 0)
            {
                ci++;
            }

        }
        if (ci == 1)
        {
            cout << ("The network is connected.\n");
        }
        else
        {

            cout << "There are " << ci << " components.\n";
        }

    }

    return 0;

}
int * find(int **list, int i1)
{
    int i = i1 - 1;
    for (; i < ccc; i++)
    {
        if (list[i][0] == i1)
        {
            break;
        }

    }
    while (list[i][1] > 0)
    {
        i = list[i][1] - 1;
    }

    return  list[i];
}
 

原文地址:https://www.cnblogs.com/interim/p/9744026.html

时间: 2024-11-08 13:11:37

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

PAT 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:

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 Specificat

04-树5. File Transfer (25)

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

04-2. File Transfer (25)并查集

04-2. File Transfer (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 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

pat04-树5. File Transfer (25)

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

PTA 树的同构(25 分)

7-1 树的同构(25 分) 给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是"同构"的.例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A.B.G的左右孩子互换后,就得到另外一棵树.而图2就不是同构的. 图1 图2 现给定两棵树,请你判断它们是否是同构的. 输入格式: 输入给出2棵二叉树树的信息.对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N?1编号):随后N行,第i行对应编号第i个结点,给出

PTA 5-8(English) File Transfer (25) - 并查集 - 数组实现

题目:http://pta.patest.cn/pta/test/16/exam/4/question/670 PTA - Data Structures and Algorithms (English) - 5-8 We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer t

7-1 树的同构 (25 分)

题目 : 给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是"同构"的.例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A.B.G的左右孩子互换后,就得到另外一棵树.而图2就不是同构的. 图一: 图二: 现给定两棵树,请你判断它们是否是同构的. 输入格式: 输入给出2棵二叉树树的信息.对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N?1编号):随后N行,第i行对应编号第i个结点,给出该结点中存储的1

File Transfer (25)

这是一道考察“并查集”的题目 并查集只有并和查这两种操作 值得注意的是,因为查的操作是O(height)的,所以我们可以依靠一些小技巧降低树的高度,并且不增加时间复杂度 #include <iostream> using namespace std; bool check(int x, int y); void connect(int x, int y); int father(int x); int *a; int main() { int n; cin >> n; a = (i

7-3 树的同构 (25 分)

题目地址: https://pintia.cn/problem-sets/15/problems/711 解决方法: 要判断树是否是同构,判定存储相同信息的节点的孩子(或父节点)是否一致即可; 推荐用结构体数组存储树  : 输入的节点  下标依次为 0 ==> n-1 根节点的判断:根据题目输入信息为 节点信息  左孩子 右孩子 :因此 孩子信息内没出现过的点即为根节点 样例分析 :以判断节点的父节点进行说明 根据两棵树对应的输入信息得出下表  LEFT A B C D E G G H Inde