05-树8 File Transfer(25 point(s))

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.

题意

有N台电脑,C a b 表示将这两台电脑用网线连接起来 I a b 表示检查 这两台电脑之间是否连通

最后还要输出连通块的个数

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> #define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss; const double PI = acos(-1);
const double E = exp(1);
const double eps = 1e-30; const int INF = 0x3f3f3f3f;
const int maxn = 1e4 + 5;
const int MOD = 1e9 + 7; int pre[maxn]; int find(int x)
{
int r = x;
while (r != pre[r])
r = pre[r];
int j = x, i;
while (j != r) //路径压缩
{
i = pre[j];
pre[j] = r;
j = i;
}
return r;
} void join(int x, int y)
{
int fx = find(x), fy = find(y);
if (x != fy)
pre[fx] = fy;
} void init()
{
for (int i = 0; i < maxn; i++)
pre[i] = i;
} int main()
{
init();
int n;
scanf("%d", &n);
char c;
int a, b;
while (true)
{
scanf(" %c", &c);
if (c == 'C')
{
scanf("%d%d", &a, &b);
if (find(a) == find(b))
printf("yes\n");
else
printf("no\n");
}
else if (c == 'I')
{
scanf("%d%d", &a, &b);
join(a, b);
}
else if (c == 'S')
{
map <int, int> m;
for (int i = 1; i <= n; i++)
m[find(i)] ++;
if (m.size() == 1)
printf("The network is connected.\n");
else
printf("There are %d components.\n", m.size());
break;
}
}
}

最新文章

  1. Keil RTX systick 初始化
  2. VS 2010下单元测试
  3. Codeforces Round #256 (Div. 2) Multiplication Table
  4. python的random模块
  5. requirejs实现模块化
  6. leetcode第八题--String to Integer (atoi)
  7. iOS推送服务细节回顾
  8. .net core控制台应用程序初识
  9. linux 服务器网络有关的内核参数
  10. BBS论坛(十六)
  11. EMI优化
  12. laravel的日志权限
  13. GET和POST传输方式
  14. N76E003的环境搭建
  15. [Jedis] ERR wrong number of arguments for &#39;mget&#39;
  16. 转 WiFi的STA和AP模式指什么?
  17. php file_get_contents 使用3法
  18. 浅谈 JSON 那些被转义的字符们
  19. mybatis - maven - eclipse 坑爹问题: No suitable driver found for http://maven.apache.org
  20. Linux Deploy 使用 Repository部署Linux系统

热门文章

  1. 给控件做数字签名之一:将控件打包为Web发布包 [转]
  2. 对ps4 cmask fmask的理解
  3. Mysql的时间戳转date类型
  4. Linux学习之十七-配置Linux简单的脚本文件自启动
  5. &amp;和|不等同于&amp;&amp;或||
  6. 我的IT成长史,不以物喜,不以己悲
  7. UITextField placeholder text color
  8. springboot+async异步接口实现和调用
  9. eclipse--windowBuilder
  10. 有关SQL注入的知识