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. Coder-Strike 2014 - Round 2

    t题目链接:Coder-Strike 2014 - Round 2 A题:简单水题,注意能加入反复的数字.因此仅仅要推断是否能把Min和Max加入好.就能够了 B题:开一个sum计算每一个聊天总和,和 ...

  2. JSP页面的编码设置(转载)

    1. pageEncoding:<%@ page pageEncoding="UTF-8"%> jsp页面编码: jsp文件本身的编码 2. contentType: ...

  3. Arrays.sort(a) 自定义排序

     Arrays.sort(a) 自定义排序,(需实现接口:Comparable) package com.hd; import java.util.Arrays; class Person imple ...

  4. 用 ERD 盘解决 Win8 自己主动更新后不能启动的问题

    用 ERD 盘解决 Win8 自己主动更新后不能启动的问题 有安装了 Win8 x64 系统的台式机,在自己主动更新后无法启动了.在黑屏的情况下.没有反应了. 安全模式也无法进入系统. 几经周折,发现 ...

  5. hdu 3932 Groundhog Build Home

    Groundhog Build Home Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...

  6. 修改live555支持mpeg2ts RTSP拉流,附代码

    在很早之前的博客<用EasyDarwin进行IPTV rtsp mpeg-ts smil流的转发和分发直播服务>中,我们介绍到如何将live555支持mpeg2ts拉流,这个在IPTV领域 ...

  7. photoswipe 实现图片的单击放大

    1.项目结构 2.HTML 代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind=&qu ...

  8. [IR课程笔记]概率检索模型

    几个符号意义: R:相关文档集 NR:不相关文档集 q:用户查询 dj:文档j 1/0风险情况 PRP(probability ranking principle):概率排序原理,利用概率模型来估计每 ...

  9. appium(7)-Automating mobile gestures

    While the Selenium WebDriver spec has support for certain kinds of mobile interaction, its parameter ...

  10. CoreData使用

    1.如果想创建一个带有coreData的程序,要在项目初始化的时候勾选中 2.创建完成之后,会发现在AppDelegate里多出了几个属性,和2个方法 <span style="fon ...