1118 Birds in Forest (25 分)
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 (≤104) 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 104.
After the pictures there is a positive number Q (≤104) 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
分析:并查集,水题。
/** * Copyright(c) * All rights reserved. * Author : Mered1th * Date : 2019-02-27-20.04.59 * Description : A1118 */ #include<cstdio> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> #include<string> #include<unordered_set> #include<map> #include<vector> #include<set> using namespace std; ; int father[maxn]; int findFather(int x){ int a=x; while(x!=father[x]){ x=father[x]; } while(a!=father[a]){ int z=a; a=father[a]; father[z]=x; } return x; } void Union(int a,int b){ int faA=findFather(a); int faB=findFather(b); if(faA!=faB){ father[faB]=faA; } } int main(){ #ifdef ONLINE_JUDGE #else freopen("1.txt", "r", stdin); #endif ; scanf("%d",&n); ;i<maxn;i++) father[i]=i; ;i<n;i++){ scanf("%d%d",&k,&t); maxin=max(maxin,t); ;j<k-;j++){ scanf("%d",&t1); maxin=max(maxin,t1); Union(t,t1); } } set<int> tree; ;i<=maxin;i++){ tree.insert(findFather(i)); } printf("%d %d\n",tree.size(),maxin); int query,u,v; scanf("%d",&query); ;i<query;i++){ scanf("%d%d",&u,&v); if(findFather(u)==findFather(v)) printf("Yes\n"); else printf("No\n"); } ; }
1118 Birds in Forest (25 分)的更多相关文章
- 【PAT甲级】1118 Birds in Forest (25分)(并查集)
题意: 输入一个正整数N(<=10000),接着输入N行数字每行包括一个正整数K和K个正整数,表示这K只鸟是同一棵树上的.输出最多可能有几棵树以及一共有多少只鸟.接着输入一个正整数Q,接着输入Q ...
- [并查集] 1118. Birds in Forest (25)
1118. Birds in Forest (25) Some scientists took pictures of thousands of birds in a forest. Assume t ...
- 1118. Birds in Forest (25)
Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...
- PAT A 1118. Birds in Forest (25)【并查集】
并查集合并 #include<iostream> using namespace std; const int MAX = 10010; int father[MAX],root[MAX] ...
- PAT题解-1118. Birds in Forest (25)-(并查集模板题)
如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...
- PAT甲级——1118 Birds in Forest (并查集)
此文章 同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/89819984 1118 Birds in Forest ...
- PAT 1118 Birds in Forest [一般]
1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume ...
- 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 ...
随机推荐
- python筛选特定文件的信息按照格式输出到txt
最近搞数据库,为了把图片文件的信息导入数据库表中,我开始研究python列出图片文件,其中发现因为IE临时文件里有非常多的不需要的图片,就需要筛选掉一些文件. 最终用python输出了所有需要的图片文 ...
- CH4401 蒲公英
题意 4401 蒲公英 0x40「数据结构进阶」例题 描述 题目PDF 样例输入 6 3 1 2 3 2 1 2 1 5 3 6 1 5 样例输出 1 2 1 来源 石家庄二中Violet 6杯省选模 ...
- mongodb启动很慢
故障现象mongodb重启后,等了几十分钟还一直没启动完成,单节点副本集,状态一直处于startup 原因分析查看mongod的错误日志,发现一直处于building index,但根据之前的经验,只 ...
- rsync入门使用
rsync是用来同步文件的,但是是基于增量同步的,也就是说每次同步时不需要把全部文件数据都传输过去,只需要将不相同的部分(也就是说增量差异内容)传输过去. 其基本命令格式为rsync [option] ...
- 取消svn关联文件夹
svn没有自带取消svn关联功能,所以我们需要以下脚本 Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Classe ...
- 使用btrace需要注意的几个问题
1. @ProbeClassName String clazz 此处String不能写为java.lang.String 2. location=@Location(Kind.RETURN) publ ...
- ViewBag & ViewData
ViewBag 和ViewData 是ASP.NET MVC 开发当中大家使用很多的传递数据的方法 VB可以称为VD的一块语法糖, VB是使用C# 4.0动态特征, 使得VD也具有动态特性. 下面就是 ...
- 枚举 Java Enumeration接口
Enumation 定义了一些方法,通过这些方法可以枚举对象集合中的元素 如: boolean hasMoreElements() 测试此枚举是否包含更多的元素 object nextElement( ...
- Eclipse之父、《设计模式》作者、Junit作者之Erich Gamma
Erich Gamma拥有多重权威身份.他是Jazz项目的主要领导人:是Eclipse的项目管理委员会成员,被业界称为“Eclipse之父”: 是经典书<设计模式>的作者四人帮之一,199 ...
- 在浏览器中输入url回车之后会发生什么
在浏览器中输入URL之后,浏览器会经历以下5个步骤: 1.解析URL 2.DNS域名解析 3.浏览器与网站建立TCP链接(三次握手) 4.请求和传输数据 5.浏览器渲染页面 一.解析URL 什么是ur ...