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. linux cut sort wc sed>vi awk (文本处理)

    cut: 显示切割的行数据 -f: 选择显示的列 (1: 显示第一列; 1,3: 显示第一列.第三列; 1-3: 显示第一列到第三列) -s: 不显示没有分隔符的行 -d: 自定义分隔符(' '空格 ...

  2. Mysql锁表问题解决过程

    开发中难免会遇到数据库操作锁表问题,这里说下解决过程,算是记录了. show OPEN TABLES where In_use > 0; 查看哪些表被锁了 show processlist 查看 ...

  3. @RequestMapping 和@ResponseBody 和 @RequestBody和@PathVariable 注解 注解用法

    接下来讲解一下 @RequestMapping  和@ResponseBody 和 @RequestBody和@PathVariable 注解 注解用法 @RequestMapping 为url映射路 ...

  4. ERROR:imshow、Mat、waitkey找不到标识符(opencv)

    可以发现imshow.Mat.waitkey这三个都是opencv相关的. 在添加了相关库文件后还是有问题. #include "stdafx.h" #include <st ...

  5. Django学习之序列化和信号

    一.序列化 1.serializers 2.json.dumps 二.信号 1.Django内置信号 2.自定义信号 一.序列化 关于Django中的序列化主要应用在将数据库中检索的数据返回给客户端用 ...

  6. 关于ajax请求controller返回中文乱码的解决方法!

    问题描述:前台ajax请求 Spring框架的 controller 返回的本应该是一段中文字符串 ,结果返回了一串 ?????? 解决办法:在RequestMapping的属性中添加返回数据类型属性 ...

  7. SSTap | ProxyCap

    SSTap  SSTap 全称 SOCKSTap, 是一款利用虚拟网卡技术在网络层实现的代理工具.SSTap 能在网络层拦截所有连接并转发给 HTTP, SOCKS4/5, SHADOWSOCKS(R ...

  8. 打印一个浮点数组,会输出字符串"Hello, world“ & 浮点数的二进制表示(IEEE 754标准)

    #include <stdio.h> #include<stdlib.h> int main() { float a[3] = { 1143139122437582505939 ...

  9. vue中的$EventBus.$emit、$on的应用

    今天在项目中遇到的一个需求: 在一个选项卡功能的页面,出现的问题是,当点击选项卡的某个选项时,会同时加载整个选项卡的数据,本身产品就很大,数据很多,所以这个问题无法忽略: 仔细研究下发现,当刚进入页面 ...

  10. mysql自动生成my.cnf文件

     http://imysql.com/my-cnf-wizard.html