1118 Birds in Forest
题意:
思路:并查集模板题。
代码:
#include <cstdio>
#include <algorithm>
using namespace std;
;
int father[maxn];
};
void Init()
{
;i<maxn;i++)
father[i]=i;
}
int FindSet(int a)
{
int root=a;
while(father[root]!=root)
root=father[root];
//剪枝,(不剪枝会超时哦)
while(father[a]!=a){
int tmp=a;
a=father[a];
father[tmp]=root;
}
return root;
}
void Union(int a,int b)
{
int seta=FindSet(a);
int setb=FindSet(b);
if(seta!=setb) father[setb]=seta;
}
int main()
{
//freopen("pat.txt","r",stdin);
Init();
int n,m,pre,curr;
;
scanf("%d",&n);
;i<n;i++){
scanf("%d%d",&m,&pre);
if(pre>birdCnt) birdCnt=pre;
;j<m;j++){
scanf("%d",&curr);
if(curr>birdCnt) birdCnt=curr;
Union(pre,curr);
pre=curr;
}
}
;i<=birdCnt;i++)
birdSet[FindSet(i)]++;
;
;i<=birdCnt;i++)
) setCnt++;
printf("%d %d\n",setCnt,birdCnt);
int query,a,b;
scanf("%d",&query);
;i<query;i++){
scanf("%d%d",&a,&b);
if(FindSet(a)==FindSet(b)) printf("Yes\n");
else printf("No\n");
}
;
}
1118 Birds in Forest的更多相关文章
- 1118 Birds in Forest (25 分)
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 ...
- PAT 1118 Birds in Forest [一般]
1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume ...
- PAT甲级——1118 Birds in Forest (并查集)
此文章 同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/89819984 1118 Birds in Forest ...
- 1118 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
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 ...
- 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 ...
随机推荐
- 【C#基本功 控件的用法】 委托
接触C#这段时间,很多内容容易理解,但也也有很多内容总是无法踏过门槛,就像Delegate 委托,这种新的机制是Labview不具备的,他的一个用法,也让我们这些从labview跨越过来的coder, ...
- dotnet core项目升级到 .net core 2.0
这几天无疑我们已经让.net core 2.0正式版发布的消息刷屏,这次发布整整提前了一个月的时间,关于具体的发布信息,可以去看善友大神的博客,.NET Core 2.0 正式发布信息汇总,废话不多说 ...
- easyui-textbox高为0
之前在项目中也遇到过,一段时间没遇到这种问题居然又忘记了,想着还是在博客中记录一下,方便自己记忆,也供大家参考. 大家是否也遇到过easyui-textbox高为0的情况呢 像这样: 用户名:< ...
- 将从mysql数据库查询的信息,遍历到List<>以及一些随机数的生成
将从mysql数据库查询的信息,遍历到List<>以及一些随机数的生成. 代码比较乱,但是方法还是对的,大家又需要的选择看,希望对博友 有帮助,欢迎留言分享! public class s ...
- Arcgis for Js之featurelayer实现空间查询和属性查询
空间查询和属性查询是常用的两种对数据的检索与查询方式,在本节,将讲述Arcgis for Js下如何实现featurelayer的这两种查询方式,先贴图给大家看看: 实现界面 属性查询 空间查询 看完 ...
- ng json格式的序列化和反序列化
ng中自带方法 angular.toJson 序列化angular.fromJson 反序列化 结果: 代码: <!DOCTYPE html> <html ng-app=" ...
- Metasploit的基本使用
Metasploit可以在Linux.Windows和Mac OS X系统上运行.我假设你已安装了Metasploit,或者你使用的系统是Kali Linux.它有命令行接口也有GUI接口. 我使用的 ...
- 【MFC】断言(ASSERT)的用法
摘自:Moondark http://www.cnblogs.com/moondark/archive/2012/03/12/2392315.html 断言(ASSERT)的用法 我一直以为as ...
- 【学习】Git和Github菜鸟入门
Git 是一个开源的分布式版本控制系统,用以有效.高速的处理从很小到非常大的项目版本管理. 目录(自己创建吧) 生成ssh密钥:ssh-keygen -t rsa -C "邮箱" ...
- MYSQL中防止插入重复记录的解决方案(无重复值更新)
说明:一般我们使用MYSQL插入记录时,类似于这样的语句: insert into table_name(email,phone,user_id) values(‘test9@163.com’,’99 ...