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 ;
}

最新文章

  1. oracle 存储过程访问其他用户的表
  2. bootstrap制作搜索框及添加回车搜索事件
  3. linux线程同步(3)-读写锁
  4. Atitit.木马病毒的免杀原理---sikuli&#160;的使用
  5. 2016年11月1日 星期二 --出埃及记 Exodus 19:17
  6. keyboard
  7. SQL - 添加外键
  8. [LeetCode]题解(python):011-Container With Most Water
  9. 表白程序源代码,android
  10. 利用jquery对ajax操作,详解原理(附代码)
  11. 【WebApi系列】浅谈HTTP
  12. 201621123031 《Java程序设计》第8周学习总结
  13. Ubuntu16.04下安装elasticsearch+kibana实现php客户端的中文分词
  14. Hortonworks官网文档怎么找?
  15. SublimeText3追踪函数工具CTags设置及使用
  16. [转]Spring中property-placeholder的使用与解析
  17. 常用的第三方模块 requests url
  18. 使用Java线程并发库实现两个线程交替打印的线程题
  19. LVS(Linus Virtual Server):三种负载均衡方式比较+另三种负载均衡方式
  20. PlanetTogether APS安装与配置

热门文章

  1. Android Studio 工程的 .gitignore
  2. CSS常识
  3. Cocos2d-x3.1FileUtilsTest使用
  4. EasyRTMP实现的rtmp推流的基本协议流程
  5. Eclipse内存错误java heap space
  6. How to deploy a Delphi OSX project from the command line
  7. Delphi的类方法不是静态方法
  8. SQL的分页算法
  9. 【Windows核心编程】一个使用内存映射文件进行进程间通信的例子
  10. protoc: error while loading shared libraries: libprotoc.so.9: cannot open shared object