05-Tree 8 File Transfer (25 min)
We have a network of computers and a list of bi-directional connections. Each of the 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 the one test case. For each test case, the first line containsN2≤ n≤1 0 ? 4 ?), the total number of computers in a network. Each computer in the network are then represented by a positive integer between 1 and n. Then in the following lines, the input was given in the format:
I c1 c2
Where I
stands for inputting a connection between c1
and c2
;
C c1 c2
Where C
stands for checking if it's possible to transfer files between c1
and c2
;
S
Where stands for stopping the this case S
.
Output Specification:
For each case, print on one line C
the word "yes" or "no" if it's possible or impossible to transfer files between c2
and, respectively. At the end of all case, print on one line ' the network is connected. ' If there is a path between any pair of computers; Or "There is a." Where is the number of k
k
connected.
Sample Input 1:
5C 3 2I 3 2C 1 5I 4 5I 2 4C 3 5S
Sample Output 1:
nonoyesThere are 2 components.
Sample Input 2:
5C 3 2I 3 2C 1 5I 4 5I 2 4C 3 5I 1 3C 1 5S
Sample Output 2:
nonoyesyesThe network is connected.
特意查了查c++的 二维数组 怎么声明 ,函数 参数是二维数组的情况怎么做,返回是一维数组怎么弄,
查到用的是指针.就那么用吧,估计就该这样吧.
c#的代码不能满分
c++的可以
usingSystem;usingSystem.Collections.Generic;usingSystem.Threading;usingSystem.Threading.Tasks;usingSystem.Diagnostics;usingSystem.Net;usingSystem.Text;usingSystem.Xml;classt{ Public classmyitem { Public intValue; Public intPindex; } Static voidMain (string[] args) {List<MyItem> list =NewList<myitem>(); varCount =int. Parse (Console.ReadLine ()); for(inti =1; I < Count +1; i++) {list. ADD (NewMyItem () {Value = i, Pindex =-1 }); } AAA:varA =Console.ReadLine (); if(A! ="S") { varItem = A.split (' '); varV1 = lookup (list, item[1]); varV2 = lookup (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; } GotoAAA; } Else { varv = list. FINDALL (ax = ax). Pindex <0); if(V.count = =1) {Console.WriteLine ("The network is connected."); } Else{Console.WriteLine ($"there is {v.count} components."); } return; } } Private StaticMyItem Lookup (list<myitem> List,stringItem) { varMyItem = list[int. Parse (item)-1]; if(MyItem! =NULL) { while(Myitem.pindex >0) {myitem= list. Find (x = X.value = =Myitem.pindex); } returnmyitem; } Else { return NULL; } }}
//C1.cpp: This file contains the "main" function. The execution of the program starts here and ends here. ////#include "pch.h"#include <iostream>using namespacestd;int* FIND (int**list,inti1);intCCC =0;intMain () {int**list; Std::cin>>CCC; List=New int*[CCC]; for(inti =0; I < CCC; i++) {List[i]=New int[2]; list[i][0] = i +1; list[i][1] = -1; }AAA:CharA ='a'; Std::cin>>A; if(A! ='S') { intI1; intI2; 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; } GotoAAA; } Else { intCI =0; for(inti =0; I < CCC; i++) { if(list[i][1] <0) {ci++; } } if(CI = =1) {cout<< ("The network is connected.\n"); } Else{cout<<"there is"<< ci <<"components.\n"; } } return 0;}int* FIND (int**list,intI1) { inti = I1-1; for(; i < CCC; i++) { if(list[i][0] ==I1) { Break; } } while(list[i][1] >0) {i= list[i][1] -1; } returnlist[i];}
05-Tree 8 File Transfer (25 min)