1118 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 (≤) which is the number of pictures. Then N lines follow, each describes a picture in the format:

K B​1​​ B​2​​ ... B​K​​

where K is the number of birds in this picture, and B​i​​'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 (并查集)的更多相关文章

  1. PAT A 1118. Birds in Forest (25)【并查集】

    并查集合并 #include<iostream> using namespace std; const int MAX = 10010; int father[MAX],root[MAX] ...

  2. PAT题解-1118. Birds in Forest (25)-(并查集模板题)

    如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...

  3. PAT甲级——A1118 Birds in Forest【25】

    Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...

  4. PAT 1118 Birds in Forest [一般]

    1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume ...

  5. [并查集] 1118. Birds in Forest (25)

    1118. Birds in Forest (25) Some scientists took pictures of thousands of birds in a forest. Assume t ...

  6. 1118 Birds in Forest (25 分)

    1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume ...

  7. PAT 1118 Birds in Forest

    Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...

  8. 1118. Birds in Forest (25)

    Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...

  9. 1118 Birds in Forest

    题意: 思路:并查集模板题. 代码: #include <cstdio> #include <algorithm> using namespace std; ; int fat ...

随机推荐

  1. android RefBase、sp、wp

    首先RefBase在android的c++部分是作为一个所有类的基类,其作用跟Java中的Object类似 这个类中存在一个私有成员: weakref_impl* const mRefs;(weakr ...

  2. windows server 2008 + IIS 7.5实现多用户FTP(多账号对应不同目录

    在windows server 2003 + IIS 6 的时候,就已经能实现多用户FTP的功能,不过设置有写繁琐,如果站点多的话,设置账号.权限这些东西都要搞很久.Windows server 20 ...

  3. Codeforces Round #374 (Div. 2) A. One-dimensional Japanese Crossword —— 基础题

    题目链接:http://codeforces.com/contest/721/problem/A A. One-dimensional Japanese Crossword time limit pe ...

  4. win7下使用source insight,没有Courier字体

    http://hi.baidu.com/raoxj/item/0e3a3a3b2461c5be134b14fa 1. “控制面板:--->“字体”--->找到Courier New(建议用 ...

  5. macbook清理磁盘空间

    前言:作为一名程序员,使用MacBook时间久了之后难免都会遇到“磁盘空间不足”的警告,这时就可以清理如下文件夹,一般就可以清理出几十个G的大小! 1.删除“~/资源库/Developer/Xcode ...

  6. js事件传播的一个疑惑

    在学习事件传播的时候,发现一个问题,当时是这样子的. 我给多层元素分别绑定了冒泡和捕获事件.按道理应该先从外向内执行完所有的捕获事件,再由内向外执行所有的冒泡事件. 但是天不随人愿啊,有个元素偏偏先执 ...

  7. 随应潮流-基于ABP+Angularjs现代化应用软件开发框架(1)-总体介绍

    系列文章目录 随应潮流-基于ABP+Angulsrjs现代化应用软件开发框架(1)-总体说明 随应潮流-基于ABP+Angulsrjs现代化应用软件开发框架(2)-abp说明 随应潮流-基于ABP+A ...

  8. ab-如何提交post请求

    ab -n 1 -c 1 -p post.txt http://***********/ 文件post.txt中存放要post的数据

  9. SIP呼叫流程典型流程图解及其详细解释

    目录(?)[+]   1.注册流程: 2.注销流程: 3. 基本呼叫建立过程: 4. 会话更改流程: 5. 正常呼叫释放过程: 6. 被叫忙呼叫释放: 7.被叫无应答流程一: 8.被叫无应答流程二: ...

  10. 【Boost】boost库asio详解2——io_service::run函数无任务时退出的问题

    io_service::work类可以使io_service::run函数在没有任务的时候仍然不返回,直至work对象被销毁. void test_asio_nowork() { boost::asi ...