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 (<= 104) which is the number of pictures. Then N lines follow, each describes a picture in the format:
K B1 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 (<= 104) 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 <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int f[],n,k,a,b,c,m;
int getf(int x)
{
if(f[x] != x)f[x] = getf(f[x]);
return f[x];
}
void mer(int x,int y)
{
int xx = getf(x);
int yy = getf(y);
f[xx] = yy;
}
void init()
{
for(int i = ;i <= ;i ++)
{
f[i] = i;
}
}
int main()
{
cin>>n;
init();
for(int i = ;i < n;i ++)
{
cin>>k;
cin>>a;
if(a > m)m = a;
for(int j = ;j < k;j ++)
{
cin>>b;
if(b > m)m = b;
mer(a,b);
}
}
for(int i = ;i <= m;i ++)
{
if(getf(i) == i)c ++;
}
cout<<c<<' '<<m<<endl;
cin>>k;
for(int i = ;i < k;i ++)
{
cin>>a>>b;
if(f[a] == f[b])cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
}

1118. Birds in Forest (25)的更多相关文章

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

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

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

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

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

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

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

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

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

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

  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 [一般]

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

  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. PAT 1118 Birds in Forest

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

随机推荐

  1. group by 与 order by 一起使用的时候

    select 后面的列+order by 后面的列 必须在group by 里面 也就是说 select 和 order by 后面的列是 group by 列的子集 而 select 和 order ...

  2. RotateZoom.cpp 20180622

    20180622代码加入随意变换图像大小 批处理框架先不看:-B src3.bmp 10 1 30 2  0.1 3 tar.bmp src2.bmp 37.5 2.1 tar // RotateZo ...

  3. windows安装程序制作

    作为一个学计算机的,现在才知道那些安装软件都是用软件封装工具封装起来的. 我们写好exe以后可以下载一个Inno setup5 对其打包成可安装的软件,期间可加入图标,readme,等等一些东西.

  4. 阶段3 1.Mybatis_08.动态SQL_01.mybatis中的动态sql语句-if标签

    创建新的工程 复制到新建的项目里面 pom.xml依赖部分复制过来 dao中整理代码 只保留四个查询 映射文件也只保留四个查询方法 增加一个根据条件查询的方法. 由于用了别名,所以parpameter ...

  5. apue 在 mac 环境编译错误

    参考资料:https://unix.stackexchange.com/questions/105483/compiling-code-from-apue 笔者使用 mac 学习 apue, 在编译的 ...

  6. springmvc中获取request对象,加载biz(service)的方法

    获取request对象: 首先配置web.xml文件--> <listener> <listener-class> org.springframework.web.con ...

  7. mysql先分组,然后取每个分组中的第2大的记录

    文章参考http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/ 首先建表: ...

  8. Hive-多分隔符

    ROW FORMAT SERDE 'org.apache.hadoop.hive.contrib.serde2.MultiDelimitSerDe' WITH SERDEPROPERTIES (&qu ...

  9. [19/05/27-星期一] JavaScript_ 条件语句(if语句)和循环语句(while 、for、do-while)

    一.条件语句 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <ti ...

  10. Java - Java Mail邮件开发(2)springboot +Java Mail + Html

    1.springboot + Java Mail + Html 项目结构: pom.xml <project xmlns="http://maven.apache.org/POM/4. ...