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 Studio 模拟器无法打开 emulator: ERROR: x86 emulation currently requires hardware

    首先要打开SDK的下载位置,找到以下陌路: android-sdk\extras\intel\Hardware_Accelerated_Execution_Manager\IntelHaxm.exe ...

  2. 最简单ajax,$.post()用法

    最简单的ajax,$.post()用法 $.post("action.php",{'email':$('#email').val(),'address':$('#address') ...

  3. JavaWeb项目里面的路径获取方法总结

    仅为资源搬运,个人还未充分理解... request.getRealPath不推荐使用request.getRealPath("") 这个方法已经不推荐使用了 request.ge ...

  4. poj3904 Sky Code —— 唯一分解定理 + 容斥原理 + 组合

    题目链接:http://poj.org/problem?id=3904 Sky Code Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  5. jmeter使用笔记——脚本录制,JMeter使用plugins插件进行服务器性能监控

    脚本录制: 1.badboy录制 2.代理服务器录制 ①工作台添加HTTP代理服务器 ②设置目标控制器,分组,排除模式,包含模式(使用正则表达式筛选) ③设置浏览器,手动设置代理服务器,localho ...

  6. iap 应用内购买相关的解释

    应用范围app Store Review Guidelines : https://developer.apple.com/app-store/review/guidelines/  中 11.12  ...

  7. ansible操作模块相关

    1. 查看模块可用参数命令 ansible-doc -s module_name

  8. Java面向对象的三大特征详解

    一.封装(Encapsulation)       封装也称信息隐藏,是指利用抽象数据类型把数据和基于数据的操作封装起来,使其成为一个不可分割的整体,数据隐藏在抽象数据内部,尽可能的隐藏数据细节,只保 ...

  9. (QACNN)自然语言处理:智能问答 IBM 保险QA QACNN 实现笔记

    follow: https://github.com/white127/insuranceQA-cnn-lstm http://www.52nlp.cn/qa%E9%97%AE%E7%AD%94%E7 ...

  10. kallsyms

    kallsyms 在v2.6.0的内核中,为了更好地调试内核,引入新的功能kallsyms.kallsyms把内核用到的所有函数地址和名称连接进内核文件,当内核启动后,同时加载到内存中.