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 (≤10​4​​) 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 10​4​​.

After the pictures there is a positive number Q (≤10​4​​) 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 <iostream>
#include<vector>
#include<map>
#include<algorithm>
using namespace std; #define maxn 10001
int bird[maxn];//初始状态都是0,应该将他们都初始化为自己。
int exist[maxn];
int findF(int index){
// if(bird[index]==0)
// return index;
// else return findF(bird[index]);
int r=bird[index];
while(bird[r]!=r){
int t=bird[r];
bird[index]=t;
r=t;
}
return r;
}
void getUnion(int a,int b){//将a和b合并起来,
bird[b]=a;//这样就合并了呗。
} int main(){
int n;
cin>>n;
for(int i=;i<maxn;i++)
bird[i]=i;
int k,t,fa,fb;
for(int i=;i<n;i++){
cin>>k;
vector<int> vt(k);
for(int j=;j<k;j++){
cin>>vt[j];//应该把这三个都合并起来。
exist[vt[j]]=;//表示出现过。
if(j!=){//如果不是当前,那么就不能find它的father.
fa=findF(vt[j-]);//这个应该放到Union函数里的。
fb=findF(vt[j]);
if(fa!=fb)getUnion(fa,fb);
}
}
}
map<int,int> mp;
int father,bd=;
for(int i=;i<maxn;i++){
if(exist[i]==){
bd++;
father=findF(i);
mp[father]++;
}
}
cout<<mp.size()<<" "<<bd<<'\n';
int m;
cin>>m;
int a,b;
for(int i=;i<m;i++){
cin>>a>>b;
if(findF(a)==findF(b)){
cout<<"Yes"<<'\n';
}
else{
cout<<"No"<<'\n';
}
}
return ;
}

//这个AC了,出现的问题在下。

1.忽略了输出要求,原来是要求输出树的数量以及鸟的总数量;

2.需要初始化bird数组为i,这个是需要一个for循环的,不可少的。

3.再次复习了并查集的两个函数怎么写。

PAT 1118 Birds in Forest [一般]的更多相关文章

  1. PAT 1118 Birds in Forest

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

  2. PAT甲级——1118 Birds in Forest (并查集)

    此文章 同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/89819984   1118 Birds in Forest  ...

  3. 1118 Birds in Forest (25 分)

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

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

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

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

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

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

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

  7. 【PAT甲级】1118 Birds in Forest (25分)(并查集)

    题意: 输入一个正整数N(<=10000),接着输入N行数字每行包括一个正整数K和K个正整数,表示这K只鸟是同一棵树上的.输出最多可能有几棵树以及一共有多少只鸟.接着输入一个正整数Q,接着输入Q ...

  8. PAT A1118 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 (25 分)

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

随机推荐

  1. CSS实现文字半透明显示在图片上方法

    CSS实现文字半透明显示在图片上方法 在css中文字半透明我们会需要使用滤镜效果也就是css中的filter:alpha来实现了,下面来看两个文字显示在图片上并且半透明的例子. CSS让一行文字显示在 ...

  2. Hadoop源码分析之Configuration

    转自:http://www.it165.net/admin/html/201312/2178.html org.apache.hadoop.conf.Configuration类是Hadoop所有功能 ...

  3. 使用OpenFace进行人脸识别(1)

    http://www.chinacloud.cn/show.aspx?id=25992&cid=12 本文中,笔者将介绍OpenFace中实现人脸识别的pipeline,这个pipeline可 ...

  4. TensorFlow基础笔记(6) 图像风格化实验

    参考 http://blog.csdn.net/wspba/article/details/53994649 https://www.ctolib.com/AdaIN-style.html Ackno ...

  5. GUI的广泛应用是当今计算机发展的重大成就之一

    GUI的广泛应用是当今计算机发展的重大成就之一,它极大地方便了非专业用户的使用.人们从此不再需要死记硬背大量的命令,取而代之的是可以通过窗口.菜单.按键等方式来方便地进行操作.而嵌入式GUI具有下面几 ...

  6. Python+selenium之简单介绍unittest单元测试框架

    Python+selenium之简单介绍unittest单元测试框架 一.unittest简单介绍 unittest支持测试自动化,共享测试用例中的初始化和关闭退出代码,在unittest中最小单元是 ...

  7. Redis分布式锁,基于StringRedisTemplate和基于Lettuce实现setNx

    使用redis分布式锁,来确保多个服务对共享数据操作的唯一性一般来说有StringRedisTemplate和RedisTemplate两种redis操作模板. 根据key-value的类型决定使用哪 ...

  8. NPOI导出Excle

    前端: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" con ...

  9. 剑指 offer set 11 最小的K个数

    总结 1. 假如允许修改给定数组, 那么通过快排的子过程, 可以在 o(n) 时间复杂度内得出结果. 2. 对于海量数据和不允许修改的数据, 通过最小堆的方式更好, 通过维持一个大小为 K 的最小堆

  10. 第一个内核模块hello world

    1.源码树的下载和编译(只是研究内核模块的话,应该不需要源码树的) 下载很简单,压缩包解压 编译:make menuconfig make bzImage -j4 参考 2. cd  /usr/src ...