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. matlab修改文件名和删除某类文件

    matlab修改多级文件夹路径下的文件名: % %%%%%%%%%%%%%%批量修改文件名一级文件夹 \路径下直接为文件 % close all;clear all;clc; % path='G:\1 ...

  2. Linux学习之十一-Linux字符集及乱码处理

    Linux字符集及乱码处理 1.字符(Character)是各种文字和符号的总称,包括各国家文字.标点符号.图形符号.数字等.字符集(Character set)是多个字符的集合,字符集种类较多,每个 ...

  3. PropertyGrid—隐藏某些Public属性

    1.定义一个继承ControlDesigner 的类 public class MyControlDesigner:System.Windows.Forms.Design.ControlDesigne ...

  4. hdu2955 Robberies (01背包)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=2955">http://acm.hdu.edu.cn/showproblem.php ...

  5. Android开发之短信验证码示例

    在说Android中的短信验证码这个知识点前,我们首先来了解下聚合数据 聚合数据介绍 聚合数据是一家国内最大的基础数据API提供商,专业从事互联网数据服务.免费提供从天气查询.空气质量.地图坐标到金融 ...

  6. xshell中配置linux密钥登陆

    参见 http://www.aiezu.com/system/linux/xshell_ssh_public-key_login.html 配置只能秘钥登陆 http://blog.csdn.net/ ...

  7. vue2.X v-model 指令

    1.v-model指令 <!DOCTYPE html> <html> <head> <title></title> <script s ...

  8. 【Python】写入文件

    1.1写入空文件 若将文本写入文件,在调用open()时候需要提供另外一个实参,告诉Python你要写入打开的文件 file_path = 'txt\MyFavoriteFruit.txt' with ...

  9. mysql 时间函数date_format

    http://toptree.iteye.com/blog/812642今天,在开发邮件系统的时候发现有很多的邮件没有发送成功,想手动把数据修改.找了mysql 的日期函数 获得的pubtime为St ...

  10. EMC机理------串扰

    转:电子工程师不得不知道的EMC机理------串扰(韬略科技EMC) 串扰是信号完整性中最基本的现象之一,在板上走线密度很高时串扰的影响尤其严重.我们知道,线性无缘系统满足叠加定理,如果受害线上有信 ...