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. Caused by: java.lang.NullPointerException: Attempt to write to field 'int android.app.Fragment.mNextAnim' on a null object reference

    原因fragment必须先add(),才能remove(),故remove前先做判空操作 参考:http://www.cnblogs.com/hixin/p/4427276.html

  2. 我的Android进阶之旅------>Android中MediaRecorder.stop()报错 java.lang.RuntimeException: stop failed.【转】

    本文转载自:http://blog.csdn.net/ouyang_peng/article/details/48048975 今天在调用MediaRecorder.stop(),报错了,java.l ...

  3. 第一篇 Windows 8 开发Windows Metro style app环境配置

    半   饱问 题 到 我 这 里 为 止! 第一篇 Windows 8 开发Windows Metro style app环境配置 2012-09-24 08:24 by 半饱, 1289 阅读, 3 ...

  4. eclipse高亮选中属性以及更改颜色

    1.显示:      1.1.工具栏里有个黄色小笔的图标,点一下就好了      1.2.打开对话框windows->preference,在左上角输入mark Occurrencs 把右边都选 ...

  5. openstack instance resize to rebuild

  6. 特征变化--->标签到向量的转换(OneHotEncoder)

    一.One-Hot Encoding     One-Hot编码,又称为一位有效编码,主要是采用位状态寄存器来对个状态进行编码,每个状态都由他独立的寄存器位,并且在任意时候只有一位有效.     在实 ...

  7. SQL数据库还原的二种方式和区别

    1.数据库还原 在SQL中,直接选择选择“还原数据库”:选中.bak 文件即可. 2.生成脚本 新建同样的DB名字,在SQL打开脚本,执行脚本语言.数据库里面就会自动填充内容.

  8. wxwidgets安装环境配置

    一:安装VS2012 wxWidgets-2.9.5( 2.95版本为最稳定版本) 二:打开wxWidgets-2.9.5的安装目录,找到build-msw-wx_vc10.sln打开(等待) 三:打 ...

  9. windows下关闭进程树

    关闭进程需要特定权限,如果你程序权限不够也会导致关闭进程失败.关闭进程树,需要遍历给定进程下的所有子进程,这个过程可以用并查集来做. 1.编写获取进程父进程的代码 #define ProcessBas ...

  10. VMWare 安装Ubuntu 16.04

    1.新建虚拟机 (1)点击文件-->新建虚拟机 (2)选择 自定义(高级)--> 下一步 (3)选择Workstation 12.0 --> 下一步 (4)选择 稍后安装操作系统 - ...