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 (≤) 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 1.

After the pictures there is a positive number Q (≤) 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>
using namespace std;
int a, b, n, k, q, father[], birdNums = , treeNums = ;
int findFather(int x)
{
if (father[x] == x)
return x;
int temp = findFather(father[x]);
father[x] = temp;
return temp;
}
void Unit(int a, int b)
{
int ax = findFather(a), bx = findFather(b);
if (ax != bx)
father[ax] = bx;
}
int main()
{
cin >> n;
for (int i = ; i < ; ++i)
father[i] = i;
while(n--)
{
cin >> k;
if (k--)
{
cin >> a;
birdNums = birdNums > a ? birdNums : a;//因为鸟的序号是连续的,故数量即使最大序列号
}
while (k--)
{
cin >> b;
Unit(a,b);//合并合集
birdNums = birdNums > b ? birdNums : b;
}
}
for (int i = ; i <= birdNums; ++i)
if (findFather(i) == i)
++treeNums;
printf("%d %d\n", treeNums, birdNums);
cin >> q;
while (q--)
{
cin >> a >> b;
if (findFather(a) == findFather(b))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return ;
}

最新文章

  1. 去除inline-block之间的间隙
  2. 屏幕 Dynpro
  3. oc-17-description
  4. Spark installation for windows
  5. PHP+ MongoDB
  6. 使用C#模拟ASP.NET页面中按钮点击
  7. HW4.33
  8. Foundation 框架 NSArray、NSMutableArray排序
  9. Python之路:常用算法与设计模式
  10. Apache开启压缩功能
  11. 在Intellij IDEA 中clean报错:-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variable and mvn script match.
  12. 火狐浏览器导出EXCEL 表格,文件名乱码问题
  13. Tapestry: Obtained resource by @Inject is NULL
  14. 记录一次Oracle注入绕waf
  15. 分布式文档存储数据库 MongoDB
  16. Hibernate入门(十二)离线条件检索
  17. Linux平台中使用PHP让word转pdf
  18. 重读《深入理解Java虚拟机》一、Java虚拟机内存区域的划分
  19. Vue学习记录(一)
  20. Python基础--数据类型

热门文章

  1. leetcode-159周赛-5231-删除子文件夹
  2. Js数据类型和运算符
  3. CSS:CSS 实例
  4. ionic:创建 APP
  5. docker实用参数
  6. nginx启停脚本
  7. hexo next主题深度优化(八),微加速
  8. 剑指offer——34之字打印二叉树
  9. nodejs中命令行和node交互模式的区分
  10. 漏洞:会话固定攻击(session fixation attack)