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 ...
随机推荐
- HackerRank leonardo-and-lucky-numbers —— 模线性方程的通解
题目链接:https://vjudge.net/problem/HackerRank-leonardo-and-lucky-numbers 题解: 1.根据扩展欧几里得:7*x + 4*y = gcd ...
- FFmpeg big changes. ffmpeg 接口的一些改变
Big changes have been made from FFmpeg 0.5.1… Refer to http://cekirdek.pardus.org.tr/~ismail/ffmpeg- ...
- PHP Json函数不能处理中文的解决办法
PHP5.2 新增的 json 功能是非常受欢迎的,但是经过测试发现,json_encode 对中文的处理是有问题的: 不能处理GB编码,所有的GB编码都会替换成空字符: utf8编码的中文被编码成u ...
- BZOJ 1632 [Usaco2007 Feb]Lilypad Pond:spfa【同时更新:经过边的数量最小】【路径数量】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1632 题意: 有一个n*m的池塘.0代表水,1代表荷花,2代表岩石,3代表起点,4代表终点 ...
- Sublime Text 相关教程(转)
曾经有人说过,世界上有两种编辑器,好用和不好用的:而在好用的编辑器中,又分两种,免费的和死贵死贵的.譬如说VIM 和 TextMate,就是免费和死贵的典型.很不幸,今天的主角 Sublime Tex ...
- 启动jmeter报错
启动jmeter.bat时报错
- HihoCoder 1640 : 命名的烦恼(预处理)
描述 程序员常常需要给变量命名.给函数命名.给项目命名.给团队命名…… 好的名字可以大大提高程序员的主观能动性,所以很多程序员在起名时都会陷入纠结和烦恼. 小Hi希望给新的项目起个拉风的名字.他希望这 ...
- python 基础之第四天
例子1: 打印列表每个元素对应的索引 [root@master script]# vim suoyin.py #!/usr/bin/python # coding:utf-8 alist = ['fu ...
- for循环的一个注意点
unsigned int i =10; for(i;i > 0; i--) { xxxxx } 因为i是unsigned int 类型的,永远不可能小于0,也就是说是个死循环了.
- hue集成各种组件
一.Hue安装 可以编译安装,我这里有已经编译好的,直接解压使用: hue默认端口:8888 http://gethue.com/ https://github.com/cloudera/hue ht ...