1118 Birds in Forest (25 分)
 

Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in the same picture belong to the same tree. You are supposed to help the scientists to count the maximum number of trees in the forest, and for any pair of birds, tell if they are on the same tree.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive number N (≤) which is the number of pictures. Then N lines follow, each describes a picture in the format:

K B​1​​ B​2​​ ... B​K​​

where K is the number of birds in this picture, and B​i​​'s are the indices of birds. It is guaranteed that the birds in all the pictures are numbered continuously from 1 to some number that is no more than 1.

After the pictures there is a positive number Q (≤) which is the number of queries. Then Q lines follow, each contains the indices of two birds.

Output Specification:

For each test case, first output in a line the maximum possible number of trees and the number of birds. Then for each query, print in a line Yes if the two birds belong to the same tree, or No if not.

Sample Input:

4
3 10 1 2
2 3 4
4 1 5 7 8
3 9 6 4
2
10 5
3 7

Sample Output:

2 10
Yes
No

题目大意: 有N张照片,每张照片里面有K只鸟,它们属于同一棵树,每只鸟从1开始连续编号。接着有Q条查询指令,每条指令包含两只鸟的编号;要求先输出树的数量和鸟的数量,再对每条查询指令判断这两只鸟是否属于同一棵树。

思路:并查集的操作没啥好说的,略过~~,定义一个大数组S作为集合,并将所有元素初始化为-1;由于鸟的编号是连续的,那么最大的那个编号就是鸟的数量,同时也是集合S的size,遍历集合S,S中小于0的元素的数量就是树的数量。

 #include <iostream>
#include <vector>
using namespace std;
int findRoot(int X);//寻找树的根
void unionSet(int root, int Y);
vector <int> S(, -);//将集合元素初始化为-1
int main()
{
int N, setSize = , Q, treeNum = ;
scanf("%d", &N);
for (int i = ; i < N; i++) {
int K, B, root;
scanf("%d%d", &K, &B);
root = findRoot(B);
if (setSize < B) setSize = B;
for (int j = ; j < K; j++) {
scanf("%d", &B);
if (setSize < B) setSize = B;
unionSet(root, B);
}
}
for (int i = ; i <= setSize; i++)
if (S[i] < )
treeNum++;
printf("%d %d\n", treeNum, setSize);
scanf("%d", &Q);
for (int i = ; i < Q; i++) {
int X, Y;
scanf("%d%d", &X, &Y);
printf(findRoot(X) == findRoot(Y) ? "Yes\n" : "No\n");
}
}
void unionSet(int root, int Y) {
int rootY = findRoot(Y);
S[rootY] = root;
}
int findRoot(int X) {
if (S[X] < ) {
return X;
}
else {
return S[X] = findRoot(S[X]);//递归地进行路径压缩
}
}

PAT甲级——1118 Birds in Forest (并查集)的更多相关文章

  1. PAT A 1118. Birds in Forest (25)【并查集】

    并查集合并 #include<iostream> using namespace std; const int MAX = 10010; int father[MAX],root[MAX] ...

  2. PAT题解-1118. Birds in Forest (25)-(并查集模板题)

    如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...

  3. PAT甲级——A1118 Birds in Forest【25】

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

  4. PAT 1118 Birds in Forest [一般]

    1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume ...

  5. [并查集] 1118. Birds in Forest (25)

    1118. Birds in Forest (25) Some scientists took pictures of thousands of birds in a forest. Assume t ...

  6. 1118 Birds in Forest (25 分)

    1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume ...

  7. PAT 1118 Birds in Forest

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

  8. 1118. Birds in Forest (25)

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

  9. 1118 Birds in Forest

    题意: 思路:并查集模板题. 代码: #include <cstdio> #include <algorithm> using namespace std; ; int fat ...

随机推荐

  1. Android Button Maker(在线生成android shape xml文件的工具),真方便!

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/scry5566/article/details/25379275        直接上地址:http ...

  2. Hadoop安全

    kerberos-hadoop配置常见问题汇总 注意事项 常见问题如下(其中前面两点最多): 各目录属主组属性修改. 对于hadoop,需要改为yarn:hadoop/mapred:hdoop/hdf ...

  3. ubuntu12.04不能记住上次编辑位置的解决方案

    1:按照网上的代码 au BufReadPost * |if line("'\"") <= line("$")|exe("norm ' ...

  4. codeforces 715c

    题目大意:给定一个有N个点的树,问其中有多少条路径满足他们的边权连成的数对M取余为0.其中gcd(M,10)=1. 题解: 很亲民的点分治题目,对每一层点分治,预处理每个点到当前根的数字并对m取余,和 ...

  5. Android 启动过程介绍【转】

    本文转载自:http://blog.csdn.net/yangwen123/article/details/8023654 一般开机过程大致可以分为三个大阶段: 1. OS级别,由bootloader ...

  6. 20171202作业1python入门

    1.简述编译型与解释型语言的区别,且分别列出你知道的哪些语言属于编译型,哪些属于解释型 编译型:需要编译器,执行前一次性翻译成机器能读懂的代码(如c,c++,执行速度快,调试麻烦) 解释型:需要解释器 ...

  7. ansible管理windows实践

    一.前言 近期打算搞搞自动部署,因为是windows服务器,一些工具和系统支持都不是太好.最后发现ansible比较火,最重要的是他支持windows.本文主要就ansible 在windows使用环 ...

  8. 跨域传输信息postMessage

    widnow.postMessage()方法允许安全的跨域传输. Syntax otherWindow.postMessage(message, targetOrigin, [transfer]); ...

  9. syslog格式

    转自:http://wly719.iteye.com/blog/1827394 1.syslog格式介绍 在Unix类操作系统上,syslog广泛 应用于系统日志.syslog日志消息既可以记录在本地 ...

  10. React.js 之hello word

    引入的js文件说明 react.js 是 React 的核心库 react-dom.js 是提供与 DOM 相关的功能 babel.min.js的作用是将 JSX 语法转为 JavaScript 语法 ...