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​​ B​2​​ ... B​K​​

where K is the number of birds in this picture, and B​i​​'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 10​4​​.

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 <bits/stdc++.h>
using namespace std;
int p[];
int visit[];
int a[];
int n,m,k;
int found(int a)
{
if(a==p[a]){
return a;
}
return p[a]=found(p[a]);
}
void unite(int a,int b)
{
int x = found(a);
int y = found(b);
if(x!=y){
p[x] = y;
}
return ;
}
bool istrue(int a,int b){
return found(a) == found(b);
}
int main()
{
int x,y;
set<int> s; for(int i=;i<=;i++){
p[i] = i;
}
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&m);
for(int j=;j<=m;j++)
{
scanf("%d",&a[j]);
s.insert(a[j]);
}
for(int j=;j<=m;j++)
{
unite(a[j-],a[j]);
}
}
int sum = s.size();
set<int> ss;
for(int i=;i<=sum;i++)
{
ss.insert(found(i));
}
cout<<ss.size()<<" "<<sum<<endl;
scanf("%d",&k);
while(k--)
{
scanf("%d%d",&x,&y);
if(istrue(x,y)){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
}
return ;
}

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

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

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

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

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

  3. 1118. Birds in Forest (25)

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

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

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

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

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

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

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

  7. 1118 Birds in Forest (25 分)

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

  8. PAT 1118 Birds in Forest [一般]

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

  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. 关于移动端文字无法垂直居中(或line-height不起作用)的问题的解决方案(网摘)

    最近开发移动端APP,发现安卓端对于文字垂直居中的问题兼容性不好,网上也搜了很多方法,但是都比较麻烦,自己摸索出来了最终的解决方案: 1.首先在html头部把我们常用的lang="en&qu ...

  2. php源码zend_do_begin_namespace函数详解

    version:5.6.21 file:Zend/zend_compile.c line:7055-7152 void zend_do_begin_namespace(const znode *nam ...

  3. MySQL 存储过程 (3)

    以下介绍下像数据库循环插入数据操作 第一步:建立存储过程用到的信息表

  4. wcf系列(一)--- 寄宿方式

    一.自我寄宿(self-hosting) 1.wcf采用基于终结点(Endpoint)的通信手段:终结点由:地址(Address)+绑定(Binding)+契约(Contract)组成:  Enpoi ...

  5. Connection Phase Packets

    Connection Phase Packets https://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet ...

  6. Qt笔记之使用设计器自定义窗口标题栏

    1.在窗口显示之前,设置WindowFlags为FramelessWindowHint,以产生一个没有边界的窗口 例如 Widget::Widget(QWidget *parent) : QWidge ...

  7. python自动化运维八:Ansible

    Ansible是新出现的自动化运维工具,基于Python研发.糅合了众多老牌运维工具的优点实现了批量操作系统配置.批量程序的部署.批量运行命令等功能.仅需在管理工作站上安装ansible程序配置被管控 ...

  8. ABAP DEMO-2018

    sap Program DEMO 介绍 Program Description BALVBT01 Example SAP program for displying multiple ALV repo ...

  9. Python使用setuptools打包源文件(精简)

    目录结构: F:\capt>tree ├─abc_test │  ├─__init__.py │  ├─comman_line.py ├─setup.py #__init.py__ def he ...

  10. [coci2011]友好数对 容斥

    无趣的小x在玩一个很无趣的数字游戏.他要在n个数字中找他喜欢友好数对.他对友好数对的定义是:如果有两个数中包含某一个以上相同的数位(单个数字),这两个数就是友好数对.比如:123和345 就是友好数对 ...