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 ...
随机推荐
- Django Rest framework 之 节流
RESTful 规范 django rest framework 之 认证(一) django rest framework 之 权限(二) django rest framework 之 节流(三) ...
- hdu-3790 最短路径问题(双重权值)
Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的. Input ...
- node.js入门学习笔记整理
(1)node Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境. Node与javaScript的区别在于,javaScript的顶层对象是window,而no ...
- SD从零开始67-70 后勤信息系统中的标准分析, 信息结构, 信息的更新规则, 建立统计数据
SD从零开始67 后勤信息系统中的标准分析 标准分析中的报表Reporting in Standard Analyses 标准分析为高质量的表达和分析LIS中的数据基础提供了大量的功能: 当你决定了一 ...
- Force.com 多租户架构
本文参考自官方文档. 多租户架构 作为云计算平台的先驱,Salesforce最大的特点是"软件即服务"(Software as a Service,Saas).实现这种技术的基础便 ...
- Django ModelForm 校验数据格式
发现ModelForm很好用,用来做form表单验证效果很好.但是也要注意几点. forms的用法: 使用默认方式:继承forms.Form类,类里面的字段名称一定要和前端HTML里面的form表单里 ...
- Linux常用命令大全(新手入门)
系统信息: arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SM ...
- HDU ACM 1869 六度分离(Floyd)
六度分离 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- [HDFS_add_1] HDFS 启动过程分析
0. 说明 HDFS 文件概念 && HDFS 启动过程分析 1. HDFS 文件概念 [1.1 NameNode 职能] 存储文件类型.大小.权限.路径等等元数据 通过 edits( ...
- nginx配置静态资源访问
本篇配置使用场景:本地通过浏览器访问linux上某个文件夹下的文件: 1.安装jdk及nginx步骤省略 2.进入正题 (1) 查看nginx安装路径:[root@localhost conf]# w ...