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. [poj3974]Palindrome_Manacher

    Palindrome poj-3974 题目大意:求字符串的最长回文子串. 注释:$1\le strlen(s) \le 10^6$. 想法:介绍一种字符串算法——Manacher.求以每一个字符和字 ...

  2. Spring MVC-集成(Integration)-生成XML示例(转载实践)

    以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_xml.htm 说明:示例基于Spring MVC 4.1.6. 以下示例说明如何 ...

  3. 查看OS 各项参数

    查看CPU 在linux下 cat /proc/cpuinfo 可以得到CPU信息. 要注意的是CPU型号有不同的种类比如AMD Intel.可能在这个文件中显示的信息也不同.但终归是存在这个文件中的 ...

  4. 将XML文件转化成NSData对象

    NSData *xmlData = [[NSData alloc]initWithContentsOfFile:[NSString stringWithFormat:@"%@/People. ...

  5. POJ 3653 &amp; ZOJ 2935 &amp; HDU 2722 Here We Go(relians) Again(最短路dijstra)

    题目链接: PKU:http://poj.org/problem? id=3653 ZJU:problemId=1934" target="_blank">http ...

  6. UpMarqueeTextView-模仿淘宝client向上滚动的广告条

    UpMarqueeTextView一个简单的向上滚动的相似跑马灯效果,项目中用到的时候是接受到推送过来的消息向上滚动一次.没有做动态的gif效果,所以都是一些纯文字的简单记录. UpMarqueeTe ...

  7. cocos2d js ClippingNode 制作标题闪亮特效

    1.效果图: 之前在<Android 高仿 IOS7 IPhone 解锁 Slide To Unlock>中制作了文字上闪亮移动的效果,这次我们来看下怎样在cocos2d js 中做出类似 ...

  8. 详略。。设计模式1——单例。。。。studying

    设计模式1--单例 解决:保证了一个类在内存中仅仅能有一个对象. 怎么做才干保证这个对象是唯一的呢? 思路: 1.假设其它程序可以任意用new创建该类对象,那么就无法控制个数.因此,不让其它程序用ne ...

  9. C++高精度性能測试函数

    在实际software开发工作中.我们常常会測试某个module或者function的执行效率.或者是某个算法的时间复杂度(尽管时间复杂度一定程度上依赖于机器性能.但在同一台computer上,经过算 ...

  10. hdu1116 Play on Words--并查集

    原题链接: pid=1116">http://acm.hdu.edu.cn/showproblem.php? pid=1116 一:原题内容 Problem Description S ...