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. 【机器学习速成宝典】模型篇04k近邻法【kNN】(Python版)

    目录 什么是k近邻算法 模型的三个基本要素 构造kd树 kd树的最近邻搜索 kd树的k近邻搜索 Python代码(sklearn库) 什么是K近邻算法(k-Nearest Neighbor,kNN) ...

  2. Android NDK的生命周期JNI_OnLoad与JNI_OnUnload(转)

    摘要 NDK的生命周期 //当动态库被加载时这个函数被系统调用 JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { LOGI ...

  3. C#调用C++的库 P/Invoke工具集

    p/Invoke可以使用工具辅助自动生成,以减少混淆 1.官方的支持 http://visualstudiogallery.msdn.microsoft.com/site/search?query=p ...

  4. MySQL 常用报错注入原理分析

    简介 这段时间学习SQL盲注中的报错注入,发现语句就是那么两句,但是一直不知道报错原因,所以看着别人的帖子学习一番,小本本记下来 (1) count() , rand() , group by 1.报 ...

  5. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'jeewx.weixin_account_user_relation' doesn't exist

    [INFO] Scanning for projects...[INFO] [INFO] ------------------------------------------------------- ...

  6. 【Linux 应用编程】进程管理 - 进程、线程和程序

    基本概念 程序和进程的区别 程序是平台相关的二进制文件,只占用磁盘空间.编写完程序代码后,编译为可执行的二进制文件即可. 进程是运行中的程序,占用 CPU.内存等系统资源. 通过 Shell 命令,可 ...

  7. java正则匹配正则表达式

    1.简单匹配小案例 public static void main( String[] args ){ // 按指定模式在字符串查找 String line = "This order wa ...

  8. 深入理解java:1. JVM虚拟机的构成

    1.JVM虚拟机的构成 什么是Java虚拟机? 作为一个Java程序员,我们每天都在写Java代码,我们写的代码都是在一个叫做Java虚拟机的东西上执行的. 但是如果要问什么是虚拟机,恐怕很多人就会模 ...

  9. 【C语言--数据结构】线性表链式存储结构

    直接贴代码 头文件 #ifndef __LINKLIST_H__ #define __LINKLIST_H__ typedef void LinkList; typedef struct _tag_L ...

  10. [转帖]socat使用笔记

    socat使用笔记 https://blog.csdn.net/yangbingzhou/article/details/49783235 进行简单学习 centos 下面安装 yum install ...