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 ...
随机推荐
- JNI_Z_09_Java的字符串
ZC: jstring 就是 Java中的String对象 ZC: 10.8 Unicode字符串结尾(http://www.360doc.cn/article/14233282_321497569. ...
- poj3311 状压dp+floyd
先floyd预处理一遍dis,枚举所有状态,dp[ i ] [ j ]表示 以 j 为终点的状态 i 使用最小的时间 #include<map> #include<set> ...
- Firefox 下载、附加组件、Flash插件、缓存位置(附加Chrome下载和Opera下载)
Firefox 下载的FTP页面: http://ftp.mozilla.org/pub/firefox/releases/ Firefox下载官方页面: https://www.mozilla.or ...
- 常见CSS浏览器兼容性问题与解决方案【转载自http://blog.csdn.net/chuyuqing/article/details/37561313/】
所谓的浏览器兼容性问题,是指因为不同的浏览器对同一段代码有不同的解析,造成页面显示效果不统一的情况.在大多数情况下,我们的需求是,无论用户用什么浏览器来查看我们的网站或者登陆我们的系统,都应该是统一的 ...
- IE浏览器兼容 css之bug 问题
bug的几种常见原因: 1.盒模型bug 原因:没有正确声明doctype(如果没有声明doctype,各浏览器对代码的解析有不同的规范).解决方法:使用严格的doctype声明. 2.各浏 ...
- Ubuntu下Python使用MySQLdb远程连接数据库的常见问题及解决方案
本文基于http://www.cnblogs.com/fnng/p/3565912.html这篇博文,学习使用MySQLdb过程中遇到下面这些问题. 1. 安装MySQLdb时运行sudo pytho ...
- JavaScript高级程序设计读后感(一)
一.什么是JavaScript? 本质? 历史? 表单验证发展成为一门语言 局限性?
- Linux命令四
作业一: 1) 开启Linux系统前添加一块大小为20G的SCSI硬盘 2) 开启系统,右击桌面,打开终端 安装的是命令行界面 3) 为新加的硬盘分区,一个主分区大小为10G,剩余空间给扩展分区,在扩 ...
- 【CSAPP】一、计算机系统漫游
一.位+上下文 文本文件 / 二进制文件: 文本文件是只由ASCII码构成的文件 二.从源代码到可执行文件的顺序 源代码 ——> 可执行文件(机器代码)共有四步: 全过程代码 gcc hello ...
- 你不知道的sticky
position:sticky,Chrome新版本已经做了支持.sticky的中文翻译是“粘性的”,position:sticky表现也符合这个粘性的表现.基本上,可以看出是position:rela ...