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 ...
随机推荐
- 8.并发容器ConcurrentHashMap#put方法解析
jdk1.7.0_79 HashMap可以说是每个Java程序员用的最多的数据结构之一了,无处不见它的身影.关于HashMap,通常也能说出它不是线程安全的.这篇文章要提到的是在多线程并发环境下的Ha ...
- Class<?> getClass()
getClass()方法属于Object的一部分,它将产生对象的类,并且在打印该类时,可以看到该类类型的编码字符串,前导"["表示这是一个后满紧随的类型的数组,而紧随的" ...
- 给OkHttp Client添加socks代理
Okhttp的使用没有httpClient广泛,网上关于Okhttp设置代理的方法很少,这篇文章完整介绍了需要注意的方方面面. 上一篇博客中介绍了socks代理的入口是创建java.net.Socke ...
- 如何判断页面是pc端还是移动端,进入不同的页面
vue判断是pc端还是移动端分别进入不同的页面 判断移动端代码如下: function IsPC(){ var userAgentInfo = navigator.userAgent; var Age ...
- addEventListener.js
document.addEventListener("click",function(){ console.log("添加事件监听") }) 举个例子 : 点击 ...
- ps -ef|grep ?解释
上述内容为: 命令拆解: ps:将某个进程显示出来-A 显示所有程序. -e 此参数的效果和指定"A"参数相同.-f 显示UID,PPIP,C与STIME栏位. grep命令是查找 ...
- sass @function,@for,@mixin 的应用
项目前提: 不同的汽车显示不同的图片,一共9种汽车:每种汽车显示不同状态的图片,一共6种状态,所以一共会有54张图片 后台接口返回汽车种类分别为:1-9,汽车状态分别为:0-5 项目需求: 根据后台返 ...
- Python中识别DataFrame中的nan
# 识别python中DataFrame中的nanfor i in pfsj.index: if type(pfsj.loc[i]['WZML']) == float: print('float va ...
- 山西WebGIS项目总结
有一段时间没写blog了,说实话,最近的心态一直在变化,看了一部日剧,回想了这一年所学所见,感觉生活目标变了. 做国土项目这段时间不是很忙,由于数据一直给不到位,时间拖得很久,所以在这期间也在继续学习 ...
- MHA官方文档翻译
英文官方文档 http://code.google.com/p/mysql-master-ha/wiki/TableOfContents?tm=6 转载请注明出处 Overview MHA能够在较短的 ...