PAT A1118 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
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
#include <queue>
#include <set>
using namespace std;
int n,k,m;
const int maxn=;
int father[maxn];
set<int> bird,tree;
void init(){
for(int i=;i<maxn;i++){
father[i]=i;
}
}
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 munion(int a,int b){
int fa=findfather(a);
int fb=findfather(b);
if(fa!=fb){
father[fa]=fb;
}
}
int main(){
scanf("%d",&n);
init();
for(int i=;i<=n;i++){
scanf("%d",&k);
int fi;
scanf("%d",&fi);
bird.insert(fi);
for(int j=;j<k;j++){
int tmp;
scanf("%d",&tmp);
munion(fi,tmp);
bird.insert(tmp);
}
}
for(auto it=bird.begin();it!=bird.end();it++){
tree.insert(findfather(*it));
}
printf("%d %d\n",tree.size(),bird.size());
scanf("%d",&k);
for(int i=;i<k;i++){
int b1,b2;
scanf("%d %d",&b1,&b2);
if(findfather(b1)==findfather(b2))printf("Yes\n");
else printf("No\n");
}
}
注意点:第一次做到并查集的题目,看过的都已经忘记了,照着答案敲了一遍,还是要多加复习。
PAT A1118 Birds in Forest (25 分)——并查集的更多相关文章
- 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 ...
- L2-007 家庭房产 (25分) 并查集
题目链接 题解:并查集把一个家的并在一起,特殊的一点是编号大的并到小的去.这个题有个坑编号可能为0000,会错数据3和5. 1 #include<bits/stdc++.h> 2 usin ...
- L2-013 红色警报 (25分) 并查集复杂度
代码: 1 /* 2 这道题也是简单并查集,并查集复杂度: 3 空间复杂度为O(N),建立一个集合的时间复杂度为O(1),N次合并M查找的时间复杂度为O(M Alpha(N)), 4 这里Alpha是 ...
- 【PAT甲级】1118 Birds in Forest (25分)(并查集)
题意: 输入一个正整数N(<=10000),接着输入N行数字每行包括一个正整数K和K个正整数,表示这K只鸟是同一棵树上的.输出最多可能有几棵树以及一共有多少只鸟.接着输入一个正整数Q,接着输入Q ...
- PAT-1021 Deepest Root (25 分) 并查集判断成环和联通+求树的深度
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- [并查集] 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 1009 Product of Polynomials (25分) 指数做数组下标,系数做值
题目 This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: E ...
随机推荐
- Hibernate小解惑.
1.什么是SessionFactory?什么是Session?httpsession和hibernate的session的有什么区别? SessionFactory接口负责初始化Hiber ...
- Linux常用基本命令:三剑客命令之-awk格式化动作
我们之前说过,awk是一个超强的文本格式化工具,而本文的printf动作就是经常用来做格式化文本的.使用方式跟c语言的printf差不多. 1,printf默认不会回车换行 ghostwu@dev:~ ...
- 【工具相关】Web-ionic-npm的安装
一, 一般最新版本的Node.js集悲剧了npm.在命令行下,输入命令: "npm-v",可以测试npm是否安装.如下图所示: 二,上图显示了npm的版本号,即代表已经安装了npm ...
- 方差variance, 协方差covariance, 协方差矩阵covariance matrix
https://www.jianshu.com/p/e1c8270477bc?utm_campaign=maleskine&utm_content=note&utm_medium=se ...
- centos 多个yum源,系统怎么选择
yum配置文件: /etc/yum.conf pkgpolicy:包的策略.一共有两个选项,newest和last,这个作用是如果你设置了多个repository,而同一软件在不同的repositor ...
- 世界地图和主要国家的 JSON 文件
转自:http://blog.csdn.net/chinagissoft/article/details/52136253 世界地图: world.json 美洲: 美国:USA.json 加拿大:C ...
- javasscript基础
一.使用JS完成注册表单数据校验 1.需求分析 用户在进行注册的时候会输入一些内容,但是有些用户会输入一些不合法的内容,这样会导致服务器的压力过大,此时我们需要对用户输入的内容进行一个校验(前端校验和 ...
- 阿里SopHix热修复框架
2015年以来,Android开发领域里对热修复技术的讨论和分享越来越多,同时也出现了一些不同的解决方案,如QQ空间补丁方案.阿里AndFix以及微信Tinker(Bugly sdk也集成Tikner ...
- 移动端web页面开发常用的头部标签设置
在移动端web页面开发中,我们常需要设置各种头部标签以帮助浏览器更好的解析页面,将页面完美呈现,这里列出了工作中常用的各种头部标签,以备查询. viewport <meta name=" ...
- [20171211][转载]如何实现dbms_output输出没有打开serveroutput on.txt
[20171211]如何实现dbms_output输出没有打开serveroutput on.txt http://orasql.org/2017/12/10/sqlplus-tips-8-dbms_ ...