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