Source:

PAT A1118 Birds in Forest (25 分)

Description:

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

Keys:

Code:

 /*
Data: 2019-06-23 14:01:57
Problem: PAT_A1118#Birds in Forest
AC: 19:34 题目大意:
一张照片中的所有鸟都在同一棵树上,现在给出多张照片,
找出一共有多少棵树,并判断给定的两只鸟是否在同一棵树上。 基本思路:
并查集
*/
#include<cstdio>
#include<set>
using namespace std;
const int M=1e4+;
int mp[M]; int Father(int v)
{
int x=v,s;
while(mp[v] != v)
v = mp[v];
while(mp[x] != x){
s = mp[x];
mp[x] = v;
x = s;
}
return v;
} void Union(int v1, int v2)
{
int f1 = Father(v1);
int f2 = Father(v2);
mp[f2] = f1;
Father(v2);
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE for(int i=; i<M; i++)
mp[i]=i; int n,m,f,b1,b2;
set<int> tree,bird;
scanf("%d", &n);
while(n--)
{
scanf("%d", &m);
scanf("%d", &b1);
bird.insert(b1);
for(int i=; i<m; i++)
{
scanf("%d", &b2);
bird.insert(b2);
Union(b1,b2);
b1=b2;
}
}
scanf("%d", &n);
for(auto it=bird.begin(); it!=bird.end(); it++)
tree.insert(Father(*it));
printf("%d %d\n", tree.size(), bird.size());
for(int i=; i<n; i++)
{
scanf("%d%d", &b1,&b2);
b1 = Father(b1);
b2 = Father(b2);
if(b1 == b2)
printf("Yes\n");
else
printf("No\n");
} return ;
}

PAT_A1118#Birds in Forest的更多相关文章

  1. PAT1118:Birds in Forest

    1118. Birds in Forest (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Some ...

  2. 1118 Birds in Forest (25 分)

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

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

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

  4. PAT 1118 Birds in Forest [一般]

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

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

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

  6. A1118. Birds in Forest

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

  7. PAT A1118 Birds in Forest (25 分)——并查集

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

  8. 1118 Birds in Forest (25 分)

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

  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. ZooKeeper教程资源收集(简介/原理/示例/解决方案)

    菩提树下的杨过: ZooKeeper 笔记(1) 安装部署及hello world ZooKeeper 笔记(2) 监听数据变化 ZooKeeper 笔记(3) 实战应用之[统一配置管理] ZooKe ...

  2. PHP包管理工具composer简单总结

    前言 接触laravel之后,才知道有PSR,composer之类的东西,PHP已经不再是一门草根语言了.最近在尝试玩thrift,需要安装PHP thrift依赖库,使用composer insta ...

  3. 关于压缩软件gzip和xz的简单对照

    晚上因为处理磁盘报警的须要.进行了日志压缩,在此次压缩中分别使用了gzip和xz软件对文本进行了压缩.压缩的结果很令人诧异. 出于对xz好奇的原因是因为在下载内核源码时常常能够看到.xz格式的文件包. ...

  4. 编程算法 - 和为s的连续正整数序列 代码(C)

    和为s的连续正整数序列 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 输入一个正数s, 打印出全部和为s的连续正数序列(至少含有两个数). 起 ...

  5. 学习笔记——WCF

    学了一下WCF,发现怎么跟Web Service这么像! 这个WCF究竟干嘛的? 一查,原来: "Windows Communication Foundation (WCF) 是由微软发展的 ...

  6. 2018GDOI记

    今年居然是主场.就没有游了. 向死而生.发现最近生活就是印证了我blog的那句话:就算是修罗,也会被生活玩弄于股掌间 想了很久,还是决定要继续写,然后公诸于众. ------------------- ...

  7. [APIO2008]DNA

    https://zybuluo.com/ysner/note/1158123 题面 戳我 解析 我们要求出第\(r\)种方案,莫过于看其前面什么时候有\(r-1\)种方案. 于是,我们要求出每种情况的 ...

  8. nodejs--Nodejs单元测试小结

    前言 最近在写一课程的Project,用Node写了一个实时聊天小应用,其中就用到了单元测试.在写Node单元测试的时候,一方面感受到了单元测试的重要性,另一方面感受到了Node单元测试的不够成熟,尚 ...

  9. E20170911-hm

    specification n.     规格; 说明书; 详述;

  10. Aviator

    Aviator 简介¶ Aviator是一个高性能.轻量级的java语言实现的表达式求值引擎,主要用于各种表达式的动态求值.现在已经有很多开源可用的java表达式求值引擎,为什么还需要Avaitor呢 ...