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;
}
}
}

05-树8 File Transfer(25 point(s)) 【并查集】的更多相关文章

  1. pat04-树5. File Transfer (25)

    04-树5. File Transfer (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue We have ...

  2. PTA 05-树8 File Transfer (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/670 5-8 File Transfer   (25分) We have a netwo ...

  3. PAT 5-8 File Transfer (25分)

    We have a network of computers and a list of bi-directional connections. Each of these connections a ...

  4. 05-树8 File Transfer (25 分)

    We have a network of computers and a list of bi-directional connections. Each of these connections a ...

  5. 1013 Battle Over Cities (25分) DFS | 并查集

    1013 Battle Over Cities (25分)   It is vitally important to have all the cities connected by highways ...

  6. PAT A1118 Birds in Forest (25 分)——并查集

    Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...

  7. BZOJ4025: 二分图【线段树分治】【带撤销的并查集】

    Description 神犇有一个n个节点的图.因为神犇是神犇,所以在T时间内一些边会出现后消失.神犇要求出每一时间段内这个图是否是二分图.这么简单的问题神犇当然会做了,于是他想考考你. Input ...

  8. 线段树区间离散化维护按秩合并并查集(可撤销)——牛客多校第八场E

    模板题..去网上学了可撤销的并查集.. /* 给定一个无向图,边的属性为(u,v,l,r),表示<u,v>可以通过的size为[l,r] 求出有多少不同的size可以从1->n 把每 ...

  9. pat 1013 Battle Over Cities(25 分) (并查集)

    1013 Battle Over Cities(25 分) It is vitally important to have all the cities connected by highways i ...

  10. 【PAT甲级】1118 Birds in Forest (25分)(并查集)

    题意: 输入一个正整数N(<=10000),接着输入N行数字每行包括一个正整数K和K个正整数,表示这K只鸟是同一棵树上的.输出最多可能有几棵树以及一共有多少只鸟.接着输入一个正整数Q,接着输入Q ...

随机推荐

  1. nginx 配置静态目录 访问bootstrap

    location /static/ { alias /Users/wangziqiang/djangoprojects/bpmTest/static/; } 注意  /static/   中 /的完整 ...

  2. Usage of API documented as @since1.6+

    Usage of API documented as @since1.6+ File ->Project Structure->Project Settings -> Modules ...

  3. Unity3D开发基础组件提取总结

    在游戏开发过程中,除了逻辑功能的开发之外,还有非常多基础的模块.这些模块,对大部分手机网络游戏来说都是一样的.所以,在上个游戏已经上线运营大半年之际,我认为有必要将这些模块整理出来.让后面其它游戏的开 ...

  4. 常见CSS两栏式布局

    代码下载:https://files.cnblogs.com/files/xiandedanteng/TwoColumnLayout.rar 效果展示: 代码: <!DOCTYPE html&g ...

  5. AutoCAD 样条曲线如何结束

    如下所示,走了四个点之后曲线绘制结束想要闭合了   鼠标右击选择确认   然后变成下面这个样子,鼠标再右击就可以结束(然后又回从下面伸出来东西,还是右击)总之就是想要结束的时候:右击确认,不断右击   ...

  6. C++必知必会(1)

    条款1数据抽象 抽象数据类型的用途在于将变成语言扩展到一个特定的问题领域.一般对抽象数据类型的定义须要准训下面步骤: 1.     为类型取一个描写叙述性的名字 2.     列出类型所能运行的操作 ...

  7. 微信小程序 - 考试状态不同显示

    未开考 .已交卷. 考试中 .考试结束 #ddd      #f00     #ff0    默认禁用色 禁用的button仅有style起作用,四个状态,通过wx:if ... elif ... e ...

  8. #測试相关#Getting “junit.framework.AssertionFailedError: Forked Java VM exited abnormally” Exception

    编写Ant脚本进行持续測试的时候.出现了junit.framework.AssertionFailedError: Forked Java VM exited abnormally的报错,以此为key ...

  9. css:颜色名和十六进制数值

    http://www.w3school.com.cn/cssref/css_colornames.asp

  10. 面向对象程序的设计原则--Head First 设计模式笔记

    一.找出应用中可能需要变化的地方,把它们独立出来,不要和那些不需要变化的代码混在一起. 把会变化的部分取出并“封装”起来,好让其他部分不会受到影响.这样,代码变化引起的不经意后果变少,系统变得更有弹性 ...