#### 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  (), the total number of computers in a network. Each computer in the network is then represented by a positive integer between 1 and . 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.

#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>

#define mod 1000000007
const int INF = 0x7f7f7f7f;
typedef long long ll;
const int maxn = 10050;
using namespace std;

int vec[maxn] = {0};
int t;

int Find(int x)
{
int i;
if(vec[x] < 0)
return x;
for(i = vec[x]; vec[i]>=0; i = vec[i]);
return i;
}
void Union( int x, int y)
{
int root1, root2;
root1 = Find(x);
root2 = Find(y);
if(root1!=root2)
{
if(vec[root1]<vec[root2])
{
vec[root1] += vec[root2];
vec[root2] = root1;
t--;
}
else
{
vec[root2] += vec[root1];
vec[root1] = root2;
t--;
}
}
}
int main()
{

scanf("%d", &t);
getchar();
for(int i = 1; i <= t; i++)
{
vec[i] = -1;
}
while(1)
{
char temp;
scanf("%c", &temp);
if(temp == 'C')
{
int m, n;
scanf("%d%d", &m, &n);
getchar();
if(Find(m)==Find(n))
{
printf("yes\n");
}
else
{
printf("no\n");
//Union(m, n);
}
}
else
{
if(temp == 'I')
{
int m, n;
scanf("%d%d", &m, &n);
getchar();
Union(m,n);
}
else
{
if(t>1)
{
printf("There are %d components.\n", t);
break;
}
else
{
printf("The network is connected.\n");
break;
}
}
}
}

return 0;
}


#### 并查集-05-树8 File Transfer

2017-05-12 12:34:52

#### 05-树9 File Transfer (25分)

2016-10-03 21:07:30

#### 05-树8 File Transfer (25分)

2016-04-02 15:16:51

#### 05-树8 File Transfer (25分)---并查集

2017-05-26 19:13:23

#### MOOC浙大数据结构 — 05-树9 File Transfer (25分)

2016-11-21 10:42:49

#### 05-树8 File Transfer（25 point(s)） 【并查集】

2018-04-06 09:08:04

#### 05-树8 File Transfer（25 分）

2017-10-21 22:40:51

#### PAT 数据结构 04-树5. File Transfer (25)

2015-07-12 19:30:04

#### 05-树8 File Transfer【并查集】

2017-04-02 21:57:52

#### 05-树8 File Transfer   (25分)

2017-05-30 15:28:10

## 不良信息举报

05-树8 File Transfer