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. leetcode-easy-array-48. Rotate Image-NO

    mycode 思路:第m行要变到 - 1- m 列 ,但是没有再想一步即列变为行,这样每一个位置的变换方式就出来了 难点:如何不使用额外空间呢? 参考: 思路:找到矩阵旋转和转置之间的联系,转置是可以 ...

  2. Docker部署MySQL8并实现远程连接

    1. 拉取镜像 docker pull mysql 2. 运行镜像启动容器 docker run --name mysql -p 33306:3306 -e MYSQL_ROOT_PASSWORD=y ...

  3. centOS7杀死进程命令

    查看当前所有正在运行的进程,可以看到80端口被httpd占用了(80端口希望分配给nginx使用,而不是httpd) netstat -tpnul 这里以杀死httpd进程为例: 先查看 httpd ...

  4. 阶段3 1.Mybatis_03.自定义Mybatis框架_3.自定义mybatis的编码-根据测试类中缺少的创建接口和类

    先认识一下这几个类.Resources是一个class SqlSessionFactoryBuilder 创建新项目 复制相关的依赖 复制之前的代码 复制到当前项目的src下 把Mybits的依赖删除 ...

  5. 数据科学家人才危机现象,是FOMO还是Silver?

    数据科学家人才危机现象,是FOMO还是Silver? 数据科学家的人才短缺和薪水高涨已经达到了顶板,未来还会持续下去吗? 在过去几年中,高级分析(#大数据#分析)空间一直经历着严重的FOMO(害怕错过 ...

  6. 中国MOOC_零基础学Java语言_第3周 循环_2数字特征值

    2 数字特征值(5分) 题目内容: 对数字求特征值是常用的编码算法,奇偶特征是一种简单的特征值.对于一个整数,从个位开始对每一位数字编号,个位是1号,十位是2号,以此类推.这个整数在第n位上的数字记作 ...

  7. c# AES128 加解密算法

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  8. ARTS-2

    ARTS的初衷 Algorithm:主要是为了编程训练和学习.每周至少做一个 leetcode 的算法题(先从Easy开始,然后再Medium,最后才Hard).进行编程训练,如果不训练你看再多的算法 ...

  9. Matlab——表达式 阵列与矩阵的创建

    表达式 指令过长: 如果一个指令过长可以在结尾加上... 下一行继续写指令即可 若不想每次都显示运算结果,只需在运算式最後加上分号(:)即可 注释 基本的算术运算有: 加 (+).减 (-).乘 (* ...

  10. 各种CNN模型

    Resnet: model_urls = { 'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth', 'res ...