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 ...
随机推荐
- hdu1907 John 博弈
Little John is playing very funny game with his younger brother. There is one big box filled with M& ...
- Python IDLE theme
#转自 http://www.2cto.com/os/201507/418532.html #win10+python3.5.2 #保护视力 .idlerc 目录下新建名为 config-highli ...
- 【TCP/IP】二、协议的概念
一.概念 1.tcp/ip是通信协议的统称. 2.协议就是计算机与计算机之间通过网络实现通信时,事先达成的一种约定.这种约定,使那些不同厂商的设备,不同的CPU以及不同的操作系统组成的计算机之间,只要 ...
- Spring boot Mybatis 整合
PS: 参考博客 PS: spring boot配置mybatis和事务管理 PS: Spring boot Mybatis 整合(完整版) 这篇博客里用到了怎样 生成 mybatis 插件来写程 ...
- ie6,ie7下a标签无法点击(转载)
前几天在做一个网站的引导页面,因为都是用图片,所以按钮需要用空a标签来做,发现a标签在IE6与IE7中点击无效中点击不了,其他浏览器都正常.一开始以为是z-index的问题,但不论z-index设置多 ...
- mysql自增主键
MariaDB [test]> create table test1(id int primary key auto_increment,name varchar(20))auto_increm ...
- Gravitee.io api gateway 试用
以前写过几篇关于整体介绍的以及 使用docker 运行的简单说明,有了docker-compose 环境我们可以 方便的进行测试使用了. 环境准备 docker-compose 文件 versio ...
- Visual Studio不显示智能提示代码,快捷键Alt+→也不出现
最近安装了Dev Express的控件,我的vs2017 Enterprise版的环境,智能提示补全代码的快捷键功能,好像被修改了,不能使用了. 我原来的时候,比如在代码中输入如下代码: Consol ...
- out, ref 和 params 的区别和用法
1. out 参数. 如果你在一个方法中,返回多个相同类型的值,可以考虑返回一个数组. 但是,如果返回多个不同类型的值,返回数组就不可取.这个时候可以考虑使用out参数. out参数就侧重于在一个方法 ...
- mysql之 重建GTID下主从关系
主库:mysqldump -uroot -pmysql -S /tmp/mysql.sock1 --single-transaction --add-drop-database --master-da ...