04-2. File Transfer (25) time limit MS

Memory Limit 65536 KB

Code length limit 8000 B

Procedures for the award of questions 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<=10^{4) 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

#include <iostream> #include <algorithm> #include <string> #include <cstdio> #include <cstr ing> #include <cstdlib> #include <cmath> #include <vector> #include <queue> #include <sta ck> #include <map> #include <set> using namespace std; #define Lson rt<<1,l,mid #define Rson rt<<1|1,mid+1,r//#define LSON root<<1//#define Rson root<& Lt;1|1 #define MID ((l+r) >>1) typedef long Long LL; typedef pair<int,int> P; const int maxn=50005; const int base=1000; const int inf=999999; const double eps=1e-5; An int a[maxn];//represents the collection of tree int r[maxn];//that represents the height of the tree void build (int n)//Initialize {for (int i=0;i<=n;i++) {a[i]=i ;//father is himself r[i]=0;//height of 0}} int find (int x)//Find node Father {if (a[x]==x) return x; Return A[x]=find (A[x]); } void Add (int x,int y)//node Merge {X=find (a[x]); Y=find (A[y]); if (x==y) return; ElsE if (r[x]<r[y]) a[x]=y; else {a[y]=x; if (R[x]==r[y]) r[x]++; }} bool Same (int x,int y)//judgment is not a set {return find (x) ==find (y); } int main () {int n,m,i,j,k,t; cin>>n; Build (n); while (1) {char op[2]; cin>>op; if (op[0]== ' I ') {cin>>i>>j; Add (I,J); } else if (op[0]== ' C ') {cin>>i>>j; if (Same (I,J)) puts ("yes"); Else puts ("no"); } else if (op[0]== ' S ') {int cnt=0; for (i=1;i<=n;i++) if (i==a[i]) cnt++; if (cnt<=1) puts ("The network is connected."); else printf ("There is%d components.\n", CNT); Break }} return 0; }

04-2. File Transfer (25) and check set