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 ...
随机推荐
- Three.js基础:导入STL模型文件
stlloadertest.html: <!DOCTYPE html> <html lang="en"> <head> <title> ...
- # IFE前端(2015春)-task2
第一章 JavaScript数据类型及语言基础 期望达成 掌握JavaScript的各种数据类型概念.判断方法 掌握JavaScript函数.对象的概念 掌握字符串.数字.数组.日期等对象的方法 了解 ...
- X2.5 添加自定义数据调用模块(简单方法)
Discuz!X系列的diy功能还是相当不错的,在对其进行二次开发的过程中,或许需要加入新的数据调用模块,这样可以使你开发的功能模块也像原来的模块一样,只需要点点鼠标,填写一些简单的信息,就可以在各个 ...
- shell awk使用(转)
#####awk " 对每一行操作的代码块" 操作的文件 awk -F: '{print $1,$2,$3,$4,$5}' helloworld.sh 打印字段 ...
- css中zoom:1以及z-index的作用
一.CSS中zoom:1的作用在做IE6.IE7.IE8浏览器兼容的时候,经常会遇到一些问题,可以使用zoom:1来解决,有如下作用:1.触发IE浏览器的haslayout2.解决IE下的浮动,mar ...
- ARM汇编指令集4
协处理器cp15操作指令: mcr & mrc •mrc用于读取CP15中的寄存器 •mcr用于写入CP15中的寄存器 什么是协处理器? •SoC内部另一处理核心,协助主CPU实现某些功能 ...
- ARM汇编指令集1
(汇编)指令是CPU机器指令的助记符,经过编译过会得到一串0011组成的机器码,可以由CPU读取执行. (汇编)伪指令本质不是指令(只是和指令一起写在代码中),它是编译器环境提供的,目的是用来指导编译 ...
- 广义线性模型(GLM)
一.广义线性模型概念 在讨论广义线性模型之前,先回顾一下基本线性模型,也就是线性回归. 在线性回归模型中的假设中,有两点需要提出: (1)假设因变量服从高斯分布:$Y={{\theta }^{T}}x ...
- 大马猴队-Alpha阶段项目复审
队名 优点 缺点 名次 菜鸡互坑队 经典游戏,情怀加分. 刷新的苹果会在蛇身上出现 14 菜鸡互啄 利用python语言实现git版本管理,典型用户清晰. 没有很好地分析用户痛点,没有测试计划 4 ...
- svn的使用流程
一.安装: 1. 服务器端:VisualSVN_Server 2. 客户端:TortoiseSVN 二.使用VisualSVN Server建立版本库 1. 首先打开VisualSVN Server ...