PAT甲级——1118 Birds in Forest (并查集)
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 (≤) 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 1.
After the pictures there is a positive number Q (≤) 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
题目大意: 有N张照片,每张照片里面有K只鸟,它们属于同一棵树,每只鸟从1开始连续编号。接着有Q条查询指令,每条指令包含两只鸟的编号;要求先输出树的数量和鸟的数量,再对每条查询指令判断这两只鸟是否属于同一棵树。
思路:并查集的操作没啥好说的,略过~~,定义一个大数组S作为集合,并将所有元素初始化为-1;由于鸟的编号是连续的,那么最大的那个编号就是鸟的数量,同时也是集合S的size,遍历集合S,S中小于0的元素的数量就是树的数量。
#include <iostream>
#include <vector>
using namespace std;
int findRoot(int X);//寻找树的根
void unionSet(int root, int Y);
vector <int> S(, -);//将集合元素初始化为-1
int main()
{
int N, setSize = , Q, treeNum = ;
scanf("%d", &N);
for (int i = ; i < N; i++) {
int K, B, root;
scanf("%d%d", &K, &B);
root = findRoot(B);
if (setSize < B) setSize = B;
for (int j = ; j < K; j++) {
scanf("%d", &B);
if (setSize < B) setSize = B;
unionSet(root, B);
}
}
for (int i = ; i <= setSize; i++)
if (S[i] < )
treeNum++;
printf("%d %d\n", treeNum, setSize);
scanf("%d", &Q);
for (int i = ; i < Q; i++) {
int X, Y;
scanf("%d%d", &X, &Y);
printf(findRoot(X) == findRoot(Y) ? "Yes\n" : "No\n");
}
}
void unionSet(int root, int Y) {
int rootY = findRoot(Y);
S[rootY] = root;
}
int findRoot(int X) {
if (S[X] < ) {
return X;
}
else {
return S[X] = findRoot(S[X]);//递归地进行路径压缩
}
}
PAT甲级——1118 Birds in Forest (并查集)的更多相关文章
- 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甲级——A1118 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 [一般]
1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume ...
- [并查集] 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 分)
1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume ...
- PAT 1118 Birds in Forest
Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...
- 1118. Birds in Forest (25)
Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...
- 1118 Birds in Forest
题意: 思路:并查集模板题. 代码: #include <cstdio> #include <algorithm> using namespace std; ; int fat ...
随机推荐
- java URI 编码解码
import java.io.UnsupportedEncodingException; /** * url转码.解码 */ public class UrlUtil { private final ...
- hihocoder #1094 : Lost in the City微软苏州校招笔试 12月27日 (建图不大【暴力枚举】 子图的4种形态 1Y )
#1094 : Lost in the City 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi gets lost in the city. He ...
- 使用IIS建立主机到虚拟机的端口转发
主机是笔记本电脑,通过一个TPLINK智能扩展卡,作为服务器供给手机APP当作服务器. 但真正的Web服务,在主机的VMWare Workstation虚拟机80端口. 那么主机和手机形成的网络为19 ...
- ffmpeg 的一些学习网站
http://blog.csdn.net/leixiaohua1020/article/category/1360795 雷霄骅(leixiaohua1020)的专栏 http://dranger.c ...
- FLV文件格式解析(转)
FLV(Flash Video)是现在非常流行的流媒体格式,由于其视频文件体积轻巧.封装播放简单等特点,使其很适合在网络上进行应用,目前主流的视频网站无一例外地使用了FLV格式.另外由于当前浏览器与F ...
- HTML5 Canvas 时钟
1. [图片] QQ截图20120712130049.png 2. [代码][HTML]代码 <!DOCTYPE html><html lang="en" &g ...
- (原创)让mongodb的secondary支持读操作
对于replica set 中的secondary 节点默认是不可读的.在写多读少的应用中,使用Replica Sets来实现读写分离.通过在连接时指定或者在主库指定slaveOk,由Secondar ...
- MongoDB安装及多实例启动
MongoDB安装及多实例启动 MongoDB简介 MongoDB是一款跨平台.面向文档的数据库.可以实现高性能.高可用性,并且能够轻松扩展,是一个基于分布式文件存储的开源数据库系统,在高负载的情况下 ...
- C++之萃取技术(traits)
为什么需要类型萃取(特化) 前面我们提到了迭代器,它是一个行为类似于smart pointer之类的东西,主要用于对STL容器中的对象进行访问,而且不暴露容器中的内部结构,而迭代器所指对象的型别称为该 ...
- bzoj 3527 [Zjoi2014] 力 —— FFT
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3527 看了看TJ才推出来式子,还是不够熟练啊: TJ:https://blog.csdn.n ...