PAT A 1118. Birds in Forest (25)【并查集】
并查集合并
#include<iostream>
using namespace std;
const int MAX = 10010;
int father[MAX],root[MAX];
int findfather(int x){
if(x==father[x]) return x;
else{
int F=findfather(father[x]);
father[x]=F;
return F;
}
}
void Union(int a , int b){
int faA=findfather(a);
int fbB=findfather(b);
if(faA!=fbB){
father[faA]=fbB;
}
}
void init(){
for(int i=0;i<MAX;i++){
father[i]=i;
root[i]=0;
}
}
int n,q;
int main(){
init();
cin>>n;
int maxbirds=0;
for(int i=0;i<n;i++){
int nbirds,firstbird;
scanf("%d%d",&nbirds,&firstbird);
if(firstbird>maxbirds) maxbirds=firstbird;
for(int j=1;j<nbirds;j++){
int bird;
scanf("%d",&bird);
if(bird>maxbirds) maxbirds=bird;
Union(firstbird,bird);
}
}
int trees=0;
for(int i=1;i<=maxbirds;i++){
int fa=findfather(i);
root[fa]++;
if(root[fa]==1) trees++;
}
printf("%d %d\n",trees,maxbirds);
cin>>q;
while(q--){
int a , b;
scanf("%d%d",&a,&b);
if(findfather(a)==findfather(b)) printf("Yes\n");
else printf("No\n");
}
}
非递归压缩并查集
int father[MAX];
bool isRoot[MAX];//判根 int findFather( int x ) {
int a = x;
while( x != father[x] ) {
x = father[x];
} while( a != father[a] ) {
int z = a;
a = father[a];
father[z] = x;
}
return x;
} void Union( int a, int b ) {
int faA = findFather( a );
int faB = findFather( b );
if( faA != faB ) {
father[faA] = faB;
}
} void init( int n ) {
for( int i = 1; i <= n; i++ ) { //从1开始
father[i] = i;
isRoot[i] = false;
}
}
PAT A 1118. Birds in Forest (25)【并查集】的更多相关文章
- PAT甲级——1118 Birds in Forest (并查集)
此文章 同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/89819984 1118 Birds in Forest ...
- PAT题解-1118. Birds in Forest (25)-(并查集模板题)
如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...
- [并查集] 1118. Birds in Forest (25)
1118. Birds in Forest (25) Some scientists took pictures of thousands of birds in a forest. Assume t ...
- 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 (25分)(并查集)
题意: 输入一个正整数N(<=10000),接着输入N行数字每行包括一个正整数K和K个正整数,表示这K只鸟是同一棵树上的.输出最多可能有几棵树以及一共有多少只鸟.接着输入一个正整数Q,接着输入Q ...
- PAT1118. Birds in Forest (并查集)
思路:并查集一套带走. AC代码 #include <stdio.h> #include <string.h> #include <algorithm> using ...
- 1118 Birds in Forest (25 分)
1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume ...
- PAT 1118 Birds in Forest [一般]
1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume ...
- PAT1067. Sort with Swap(0, *) (25) 并查集
PAT1067. Sort with Swap(0, *) (25) 并查集 题目大意 给定一个序列, 只能进行一种操作: 任一元素与 0 交换位置, 问最少需要多少次交换. 思路 最优解就是每次 0 ...
随机推荐
- 用类(function(){})()实现点击显示index索引值的详解
code: <script type="text/javascript"> ; i < ; i++){ var btn = document.createElem ...
- TypeScript Declaration Merging(声明合并)
TypeScript中有一些独特的概念,来自需要描述JavaScript对象类型发生了哪些变化.举个例子,最为独特的概念就是"声明合并".理解了这个概念将会对你在当前JavaScr ...
- bzoj 1004 Cards
1004: [HNOI2008]Cards Description 小春现在很清闲,面对书桌上的N张牌,他决定给每张染色,目前小春只有3种颜色:红色,蓝色,绿色.他询问Sun有 多少种染色方案,Sun ...
- c# 程序设计教程笔记
值类型:[简单类型[整数类型(sbyte,byte,short,ushort,int uint,long....),字符类型),布尔类型,实数类型],结构类型, 枚举类型]. 引用类型:[类,委托,数 ...
- AE开发能否实现TOC Control里添加多个Data Frame
问题: 在ArcMap中,菜单Insert下Data Frame,可以在TOC中增加Data Frame,在MapControl或者PageLayoutControl下都可以正常显示多个Data Fr ...
- SDK 支付
充值:用什么买什么 MSDK: Q点:百科 http://baike.baidu.com/link?url=Dw8ySUIvv6AAprULG_wnI7Mst61gG4bO2qzfpfi1j9xx6c ...
- 图解TCP、IP笔记
七层:应用层.表示层.会话层.传输层.网络层.数据链路层.物理层 应用层:与通信无关的功能 表示层:例如转换编码格式 会话层:采用哪种连接方法 传输层以下: 传输层:确立连接与断开连接重发 网络层:从 ...
- git学习之旅
http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013743256916071d ...
- [NHibernate]存储过程的使用(二)
目录 写在前面 文档与系列文章 创建对象 更新对象 总结 写在前面 上篇文章介绍了如何使用MyGeneration代码生成器生成存储过程,以及nhibernate中通过存储过程删除数据的内容,这篇文章 ...
- 大熊君JavaScript插件化开发------(实战篇之DXJ UI ------ ItemSelector)
一,开篇分析 Hi,大家好!大熊君又和大家见面了,还记得前两篇文章吗.主要讲述了以“jQuery的方式如何开发插件”,以及过程化设计与面向对象思想设计相结合的方式是 如何设计一个插件的,两种方式各有利 ...