PAT_A1118#Birds in Forest
Source:
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 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 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, orNo
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的更多相关文章
- PAT1118:Birds in Forest
1118. Birds in Forest (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Some ...
- 1118 Birds in Forest (25 分)
1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume ...
- [并查集] 1118. Birds in Forest (25)
1118. Birds in Forest (25) Some scientists took pictures of thousands of birds in a forest. Assume t ...
- PAT 1118 Birds in Forest [一般]
1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume ...
- PAT甲级——1118 Birds in Forest (并查集)
此文章 同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/89819984 1118 Birds in Forest ...
- A1118. Birds in Forest
Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...
- PAT A1118 Birds in Forest (25 分)——并查集
Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...
- 1118 Birds in Forest (25 分)
Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...
- PAT 1118 Birds in Forest
Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...
随机推荐
- S - Best Reward 扩展KMP
After an uphill battle, General Li won a great victory. Now the head of state decide to reward him w ...
- HDU 5392 BC #51
就是求最大公倍数,但要用分解质因子求. 自己写的WA到爆.... #include<iostream> #include<stdio.h> #include<math.h ...
- Test for Job (poj 3249 记忆化搜索)
Language: Default Test for Job Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 9733 A ...
- UNIX环境高级编程之第3章:文件I/O
3.1 引言 文件I/O函数:打开文件,读文件,写文件 经常使用到五个函数:open, read, write, lseek, close. 本章描写叙述的函数都是:不带缓冲的I/O(unbuffer ...
- codeforces #262 DIV2 B题 Little Dima and Equation
题目地址:http://codeforces.com/contest/460/problem/B 这题乍一看没思路.可是细致分析下会发现,s(x)是一个从1到81的数,不管x是多少.所以能够枚举1到8 ...
- QQ是怎样实现好友桌面快捷方式的?
QQ是怎样实现好友桌面快捷方式的? 不知道什么时候,QQ推出了好友桌面快捷方式.方便用户和最常保持联系的好友一键联系.核心功能一:若QQ启动了.则双击快捷方式直接打开好友聊天界面:核心功能二:若QQ未 ...
- 二分图染色模板(P1330 封锁阳光大学)
二分图染色模板(P1330 封锁阳光大学) 题目描述 曹是一只爱刷街的老曹,暑假期间,他每天都欢快地在阳光大学的校园里刷街.河蟹看到欢快的曹,感到不爽.河蟹决定封锁阳光大学,不让曹刷街. 阳光大学的校 ...
- B1192 [HNOI2006]超级英雄Hero 二分图匹配
先检讨一下,前一段时间开学,做题懒得发博客,也不总结...现在捡起来. 这个题一看是裸的二分图匹配,但是仔细一看还有一些区别,就是必须要连续的连接,否则直接退出.因为前一道题答不出来的话后面的题就没有 ...
- c++ 数据预处理(数据去噪,归一化)
正态分布3σ原则,把3倍方差之外的点设想为噪声数据来排除. 归一化,将数据经过处理之后限定到一定的范围内,一般都会将数据限定到[0,1]. #include <iostream>#incl ...
- js开发规范,在php上也适用
本篇主要介绍JS的命名规范.注释规范以及框架开发的一些问题. 目录 1. 命名规范:介绍变量.函数.常量.构造函数.类的成员等等的命名规范 2. 注释规范:介绍单行注释.多行注释以及函数注释 3. 框 ...