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 ...
随机推荐
- 异步编程——promise
异步编程--promise 定义 Promise是异步编程的一个解决方案,相比传统的解决方法--回调函数,使用Promise更为合理和强大,避免了回调函数之间的层层嵌套,也使得代码结构更为清晰,便于维 ...
- 计算从哪天起应该购买预售火车票.cs
代码直接CSC编译即可. 计算从哪天起应该购买预售火车票.cs using System; using System.Diagnostics; using System.IO; class Progr ...
- 如何在修改bug时切换分支保留修改又不提交
使用git的时候,我们往往使用branch解决任务切换问题,例如,我们往往会建一个自己的分支去修改和调试代码, 如果别人或者自己发现原有的分支上有个不得不修改的bug,我们往往会把完成一半的代码 co ...
- iptables(二)iptables实际操作之规则查询
如果你是一个新手,在阅读如下文章时,请坚持读到最后,读的过程中可能会有障碍,但是在读完以后,你会发现你已经明白了. 在进行iptables实验时,请务必在测试机上进行. 之前在iptables的概念中 ...
- STM32F407: USART 遇到的问题
今天初次使用STM32F407进行USART串口通讯实验,按照f103的代码写完了,发现没法发送数据, 查看文档后发现是由于没有将端口映射到USART1,然后添加如下代码: 1 GPIO_PinAFC ...
- webpack4工具链升级排坑记录
1.webpack4号称是0配置,于是我就只设置了entry.resolve.output.module->rules之类的属性,结果通过webpack-bundle-analyzer跑出来发现 ...
- Selenium2+Python自动化学习笔记(第1天)
参考[http://blog.csdn.net/henni_719/article/details/51096531]大神写的笔记,多谢大神共享. 哈哈,今天又找到一位大神写的Selenium2+Py ...
- Python基础学习(第6天)
1.zip函数 1)zip函数在只有一个参数时运作的方式. x = [1, 2, 3] x = zip(x) print x输出:[(1,), (2,), (3,)] 2)zip函数在没有参数时运作的 ...
- Visual Studio 2008常见问题
1.asp.net在什么软件上运行?学习asp往往需要测试asp程序,电脑不能直接测试,需要装IIS才能运行,但装IIS要么需要安装盘,要么需要安装包,而且设置也很复搜索杂.这里给大学推荐两个替代II ...
- css两列自适应宽度布局(左定宽,右自适应)
1.利用BFC: <div id="root"> <div class="left">左</div> <div cla ...