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
Yesif the two birds belong to the same tree, orNoif 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 ...
随机推荐
- java 调用 库文件错误查找方法
第一步首先找到 backtrace:keyword,然后找到都应的库文件 出错的地方 pc 0000088b /system/lib/libNDK_04.so (SayHello+98). 08-1 ...
- BC ROUND 43# 03 HDU 5266
弱啊弱啊,我用了扩展指令,然后大牛告诉我,只对VC++有用,对G++没用的..shit,三题就这样没了. 方法是使用ST在线算法,O(1)查询,然后用线段树维护..呃感觉这个好慢.看了大斌神的是用LC ...
- 1.7-BGP⑤
BGP Attributes/BGP属性 (通过BGP的属性,实现对BGP路由的选择/操纵) BGP Route Selection/BGP的选路原则: 1: The BGP forwarding t ...
- 用sp_executesql执行动态SQL语句及获得返回值
过去我执行拼凑出来的动态SQL语句,都直接使用EXEC @sql 的方式.有好几次,都看到有资料说,应该尽量使用 sp_executesql. 究其原因,是因为仅仅参数不同的情况下,sp_execut ...
- oc61--block
// // main.m // Block基本使用:一种数据类型,应用在动画,多线程,集合遍历,网络请求回调. // 用来保存一段代码,在恰当的时候拿出来调用.功能类似于函数.函数不能嵌套定义,blo ...
- bzoj1895
fhqtreap 其实fhqtreap只有可持久化之后才用新建节点... reverse和splay一样. //#include<bits/stdc++.h> #include<cs ...
- C语言程序创建文件夹
#include <stdio.h> #include <process.h> #include <dir.h> int main(void) { int stat ...
- sublime的ctags安装
首先,是ctags的下载.在这里:http://pan.baidu.com/s/1gdAMFab 我们用sublime几乎都会首先安装这个插件,这个插件是管理插件的功能,先安装它,再安装其他插件就方便 ...
- Django day05 视图层之 (HttpRequest) \ (HttpResponse) \ (JsonResponse) 对象
一:视图层之HttpRequest对象 # 前台Post传过来的数据,包装到POST字典中 # request.POST # 前台浏览器窗口里携带的数据,包装到GET字典中 # request.GET ...
- JavaScript是按值传递还是按引用传递?
JavaScript是按值传递的,但是要分情况才知道传递之后原来的值会不会变,不然会出现你想都想不出来的bug 一.按值传递--元类型输入tip:元类型( number, string, boolea ...