04-Tree 5. File Transfer (25) time limit MS Memory limit 65536 KB code length limit 8000 B Program StandardAuthor Chen, Yue
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 contains N (2<=n<=104) and the total number of computers in a network. Each of the 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; Or
C C1 C2
Where C stands for checking if it's possible to transfer files between C1 and C2; Or
S
Where S stands for stopping the this case.
Output Specification:
For each C case, print in one line the word "yes" or "no" if it's possible or impossible to transfer files Betwe En C1 and C2, 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 K." Where K is the number of connected the.
Sample Input 1:
5C 3 2I 3 2C 1 5I 4 5I 2 4C 3 5S
Sample Output 1:
Nonoyesthere is 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.
Submit Code
and check Set
1#include <cstdio>2#include <algorithm>3#include <iostream>4#include <cstring>5#include <queue>6#include <vector>7 using namespacestd;8 intf[10005];9 intFINDFA (inta) {Ten if(f[a]!=a) { Onef[a]=Findfa (F[a]); A } - returnF[a]; - } the intMain () { - //freopen ("D:\\input.txt", "R", stdin); - intN; - //memset (l,-1,sizeof (L)); + intI,A,B,FA,FB; - CharC; +scanf"%d",&n); A for(i=1; i<=n;i++){ atf[i]=i; - } - for(i=1;; i++){ -Cin>>C; - if(c=='S'){ - Break; in } -Cin>>a>>b; toFa=Findfa (a); +fb=Findfa (b); - if(c=='I'){ the if(fa!=FB) { * if(fa>FB) { $f[fb]=FA;Panax Notoginseng } - Else{ thef[fa]=FB; + } A } the } + Else{ - if(fa==FB) { $printf"yes\n"); $ } - Else{ -printf"no\n"); the } - }Wuyi } the intnum=0; - for(i=1; i<=n;i++){ Wu if(f[i]==i) { -num++; About } $ } - if(num==1){ -printf"The network is connected.\n"); - } A Else{ +printf"there is%d components.\n", num); the } - return 0; $}
pat04-Tree 5. File Transfer (25)