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. 如何实现Asp与Asp.Net共享Session

    <iframe align="top" marginwidth="0" marginheight="0" src="http ...

  2. hive组件和执行过程

    转自http://blog.csdn.net/lifuxiangcaohui/article/details/40262021 对Hive的基本组成进行了总结: 1.组件: 元存储(Metastore ...

  3. linux -- ubuntuserver 安装图形界面

    安装Gnome桌面 1.安装全部桌面环境,其实Ubuntu系列桌面实际上有几种桌面应用程序,包括Ubuntu-desktop.Kubunut-desktop和Xubuntu- desktop. 我们就 ...

  4. 【BZOJ】1068: [SCOI2007]压缩(dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1068 发现如果只设一维的话无法转移 那么我们开第二维,发现对于前i个来说,如果确定了M在哪里,第i个 ...

  5. 【代码备份】原图降采样后进行NLM滤波

    文件路径: 滤波算法main.m: %% 测试函数 %NLM滤波及滤波与 clc,clear all,close all; ima_ori=double(imread('F:\Users\****n\ ...

  6. Laravel5.1 搭建博客 --文章的增删改查

    教程源于:Laravel学院 继文件上传后呢,咱来搞一搞文章的事情. 1 更改数据表 我们需要改改数据表的结构 因为涉及到重命名列名 所以咱需要引入一个包:Doctrine: composer req ...

  7. 编程之美 set 12 快速找出故障机器

    题目 1. 所有的 ID 都出现 2 次, 只有一个例外, 找到那个例外的 ID 2. 所有的 ID 都出现两次, 只有两个例外, 找出例外的那两个 总计 1. 剑指 offer 上有这两道题的解法, ...

  8. iOS开发之--一些有用的博文和博客

    应用内购的详细教程: http://mp.weixin.qq.com/s?__biz=MzA3NzM0NzkxMQ==&mid=2655357883&idx=2&sn=7cda ...

  9. Invalid property 'driverClassName' of bean class [com.mchange.v2.c3p0.ComboPooledDataSource]

    spring配置文件中配置c3p0错误,错误原因在于c3p0连接池与DBCP连接池在驱动.连接.数据库用户名这些属性名称的差别

  10. shell脚本学习总结02--数组

    bash同时支持普通数组个关联数组,普通数组只能使用整数作为数组的索引,关联数组可以使用字符串作为数组的索引. 数组的定义方法: 在单行中使用一列值定义一个数组 [root@new ~]# array ...