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. java.lang.NoClassDefFoundError: org/apache/commons/lang/xwork/StringUtils

    java.lang.NoClassDefFoundError: org/apache/commons/lang/xwork/StringUtils Struts2框架下使用JSON插件时.程序保存找不 ...

  2. Modular_exponentiation模幂运算

    https://en.wikipedia.org/wiki/Modular_exponentiation 蒙哥马利(Montgomery)幂模运算是快速计算a^b%k的一种算法,是RSA加密算法的核心 ...

  3. JSP-Runoob:JSP 页面重定向

    ylbtech-JSP-Runoob:JSP 页面重定向 1.返回顶部 1. JSP 页面重定向 当需要将文档移动到一个新的位置时,就需要使用JSP重定向了. 最简单的重定向方式就是使用respons ...

  4. XMLHttpRequest 对象-回调函数

    回调函数是一种以参数形式传递给另一个函数的函数. 如果您的网站上存在多个 AJAX 任务,那么您应该为创建 XMLHttpRequest 对象编写一个标准的函数,并为每个 AJAX 任务调用该函数. ...

  5. Linux文件属性相关补充及软硬连接

    第1章 文件属性相关 1.1 文件的属性 1.1.1 扩展名 windows  通过扩展名区分不同的类型的文件 linux 扩展名是给人类看的 方便我们区分不同类型文件 .conf      配置文件 ...

  6. Linux运维人员-服务器组成硬件基础

    第1章 1.1关于运维人员 1.1.1 运维的职责 数据不能丢 网站7*24小时运行 保证用户体验(用户体验要好) 1.1.2 运维原则 简单.易用.高效  === 简单.粗暴 1.2 服务器 1.2 ...

  7. [Apple开发者帐户帮助]七、注册设备(1)注册一个设备

    您需要已注册的设备来创建开发或临时配置文件.要使用开发人员帐户注册设备,您需要拥有设备名称和设备ID. 注意:如果您使用自动签名,Xcode会为您注册连接的设备.Xcode Server也可以配置为注 ...

  8. [Swift通天遁地]九、拔剑吧-(6)使用开源类库快速搭建强大的侧边栏项目

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  9. redis+php实现秒杀

    使用redis队列,因为pop操作是原子的,即使有很多用户同时到达,也是依次执行,推荐使用(mysql事务在高并发下性能下降很厉害,文件锁的方式也是) 先将商品库存如队列 1 2 3 4 5 6 7 ...

  10. printf的实型

    参  数 说  明 %f 按实数格式输出,整数部分按实际位数输出,6位小数 %m.nf 总位数m(含小数点),其中有n位小数 %-m.nf 同上,左对齐 %0.xf 输出小数点后x位   %f 后面如 ...