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

最新文章

  1. iOS controller解耦探究实现——第一次写博客
  2. mysql错误一例:ERROR 1030 (HY000): Got error 28 from storage engine
  3. opencart 引入 TWIG 模板引擎
  4. Linux下打开串口设置
  5. jQuery.serialize()函数
  6. 新建VM_Script
  7. 云服务器 ECS Linux IO 占用高问题排查方法
  8. 遍历List remove方法,雨露均沾
  9. android-support-xxxx.jar NoClassDefFoundError
  10. vssettings 的备份和导入
  11. Android之布局属性
  12. 深入理解JavaScript中的this关键字
  13. Java基础知识强化之集合框架笔记32:集合之可变参数的概述和使用
  14. C++测试利器--google test开源测试框架
  15. Project Euler:Product-sum numbers (problem 88) C++
  16. AspNet Core :创建自定义 EF Core 链接数据库
  17. vue搜索功能
  18. 面试遇到两个稍显变态的题目,mark一下
  19. C#Mvc地址栏传值
  20. hive-client heap内存大小的配置优先级

热门文章

  1. legend3---Homestead中Laravel项目502 Bad Gateway
  2. 字符串在PHP比较运算中的变化
  3. spring boot加载自定义配置
  4. Jmeter接口测试系列之测试用例变量参数化处理
  5. C#学习基础
  6. eclipse和myeclipse怎么在项目中查找指定代码?https://www.jb51.net/softjc/554889.html
  7. 【ABAP系列】SAP ABAP smartforms设备类型CNSAPWIN不支持页格式ZXXX
  8. ActiveMQ学习教程/1.简要介绍与安装
  9. Anaconda版本
  10. 决解nginx代理的django项目的admin站点无法访问,和没样式的问题。