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 (≤10^​4​​ ) which is the number of pictures. Then N lines follow, each describes a picture in the format:

K B​1 B2 ... BK

​​

where K is the number of birds in this picture, and Bi '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 104.

After the pictures there is a positive number Q (≤10^4 ) 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

分析

参考并查集简析

#include<iostream> //并查集
#include<math.h>
#include<vector>
using namespace std;
vector<int> birds(10010, 0), check(10010, 0);
int findfather(int b){
int t=b;
while(birds[b]!=b)
b=birds[b];
while(birds[t]!=b){
int temp=t;
t=birds[t];
birds[temp]=b;
}
return b;
}
void Union(int a, int b){
int m= findfather(a);
int n= findfather(b);
if(m!=n) birds[m]=n;
}
int main(){
int n, k, b, num=0, cnt=0, qn;
cin>>n;
vector<int> picture(n, 0);
for(int i=0; i<10010; i++)
birds[i]=i;
for(int i=0; i<n; i++){
cin>>k;
for(int j=0; j<k; j++){
cin>>b;
num=max(num, b);
if(picture[i]==0)
picture[i]=b;
Union(b, picture[i]);
}
}
for(int i=1; i<=num; i++){
b=findfather(i);
if(check[b]==0){
cnt++;
check[b]=1;
}
}
cout<<cnt<<" "<<num<<endl;
cin>>qn;
for(int i=0; i<qn; i++){
int a, b;
cin>>a>>b;
if(findfather(a)==findfather(b))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}

PAT 1118 Birds in Forest的更多相关文章

  1. PAT 1118 Birds in Forest [一般]

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

  2. PAT甲级——1118 Birds in Forest (并查集)

    此文章 同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/89819984   1118 Birds in Forest  ...

  3. 1118 Birds in Forest (25 分)

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

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

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

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

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

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

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

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

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

  8. PAT A1118 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 (25 分)

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

随机推荐

  1. bzoj3894: 文理分科(还是那道最小割)

    3894: 文理分科 题目:传送门 感谢波老师没有来D飞我,让我做出了这题... 题解: 这题其实和我做的上一题(bzoj2132)很像,所以就不写题意了. 依然是那最小割... 这题给出了四个利益矩 ...

  2. ubuntu安装go语言

    1.下载安装包 2.解压 sudo tar -zvxf go1.10.linux-amd64.tar.gz -C /usr/local 3.配置 sudo vim /etc/profile 添加 #s ...

  3. IDEA Spark Streaming Flume数据源 --解决无法转化为实际输入数据,及中文乱码(Scala)

    需要三步: 1.shell:往 1234 端口写数据 nc localhost 1234 2.shell: 启动flume服务 cd /usr/local2/flume/bin ./flume-ng ...

  4. js的时间展示

    <script type="text/javascript">$(function() { //方法调用    showtime();        //默认加载首页  ...

  5. 9.23 NOIP模拟题(数学专练)

    数论基础 专题测试  命题人:清华大学 王赢绪 /* 水题 答案为C(n-k,m-1) 预处理阶乘和逆元,O(1)算答案 开始读错题了!!!朱一乐!!! */ #include<iostream ...

  6. P3128 [USACO15DEC]最大流Max Flow(LCA+树上差分)

    P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of  pipes to transport mil ...

  7. 快速搭建Hadoop及HBase分布式环境

    本文旨在快速搭建一套Hadoop及HBase的分布式环境,自己测试玩玩的话ok,如果真的要搭一套集群建议还是参考下ambari吧,目前正在摸索该项目中.下面先来看看怎么快速搭建一套分布式环境. 准备 ...

  8. Java继承体系中this的表示关系

    在继承关系下,父类中的this关键字并不总是表示父类中的变量和方法.this关键字的四种用法如前文所述,列举如下. 1) this(paras…); 访问其他的构造方法 2) this.xxx; 访问 ...

  9. vue项目杂记

    vue项目杂记 文件目录结构 src main.js app.vue package.json webpack_config_dev.js 需要安装的包 1. vue cnpm i vue --sav ...

  10. python框架之虚拟环境的配置

    在开发过程中,往往同一台电脑要开发不同的项目,不同的项目可能需要不同版本的包,为了解决这个问题就引出了虚拟环境. 配置虚拟环境: 1.安装虚拟环境: sudo pip3 install virtual ...