并查集合并

#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)【并查集】的更多相关文章

  1. PAT甲级——1118 Birds in Forest (并查集)

    此文章 同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/89819984   1118 Birds in Forest  ...

  2. PAT题解-1118. Birds in Forest (25)-(并查集模板题)

    如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...

  3. [并查集] 1118. Birds in Forest (25)

    1118. Birds in Forest (25) Some scientists took pictures of thousands of birds in a forest. Assume t ...

  4. 1118. Birds in Forest (25)

    Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...

  5. 【PAT甲级】1118 Birds in Forest (25分)(并查集)

    题意: 输入一个正整数N(<=10000),接着输入N行数字每行包括一个正整数K和K个正整数,表示这K只鸟是同一棵树上的.输出最多可能有几棵树以及一共有多少只鸟.接着输入一个正整数Q,接着输入Q ...

  6. PAT1118. Birds in Forest (并查集)

    思路:并查集一套带走. AC代码 #include <stdio.h> #include <string.h> #include <algorithm> using ...

  7. 1118 Birds in Forest (25 分)

    1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume ...

  8. PAT 1118 Birds in Forest [一般]

    1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume ...

  9. PAT1067. Sort with Swap(0, *) (25) 并查集

    PAT1067. Sort with Swap(0, *) (25) 并查集 题目大意 给定一个序列, 只能进行一种操作: 任一元素与 0 交换位置, 问最少需要多少次交换. 思路 最优解就是每次 0 ...

随机推荐

  1. 安装zookeeper遇到的问题以及解决方案

    伪分布式安装基本思想 zookeeper的安装包保存一份,但是zoo.cfg配置多份,启动zookeeper服务器的时候指定不同的zoo.cfg即可.即启动时这样启动:zkServer.sh star ...

  2. 日常资料(TNB)

    http://www.med66.com/html/2005/5/hu47563183418550025733.html http://www.guokr.com/article/192045/ ht ...

  3. XP---------专区(Windows Exprience)--体验的简写

    待完善中 -------------------------------------- 1.0用u盘安装xp系统 用u盘给自己的或者朋友的电脑装上一个系统. 首先当然是下载一个系统文件,下面是本站的最 ...

  4. [UML]UML系列——类图class的关联关系(聚合、组合)

    关联的概念 关联用来表示两个或多个类的对象之间的结构关系,它在代码中表现为一个类以属性的形式包含对另一个类的一个或多个对象的应用. 程序演示:关联关系(code/assocation) 假设:一个公司 ...

  5. BCP导出导入大容量数据实践

    前言 SQL SERVER提供多种不同的数据导出导入的工具,也可以编写SQL脚本,使用存储过程,生成所需的数据文件,甚至可以生成包含SQL语句和数据的脚本文件.各有优缺点,以适用不同的需求.下面介绍大 ...

  6. 创立一个网站的前前后后(起因,域名,云平台,备案,CDN等等)(1)

    起因 写完<完美软件开发:方法与逻辑>这书后,原本想继续写书的,可出来参加了些社区活动后,我发现我写的书大家评价还行,但其实不太理解.而接下来想写的书更加抽象点,准备叫<管理的解析& ...

  7. angularjs 中state.go 跳转并且打开新的浏览器窗口

    包子最近遇到业务人员提的非常无厘头的需求,就是调页面的时候,一定要打开一个新的浏览器窗口...>o<奇葩!!! 但是我的页面都是state.go跳转的呀,我各种百度,发现,貌似state, ...

  8. 一段freemarker高级分页效果的代码

    <a onclick="page(1)">首页</a> [#if currpage != 1] [#assign last=currpage - 1] &l ...

  9. Asp.Net Core--自定义基于策略的授权

    翻译如下: 在封面下,角色授权和声明授权使用需求,需求的处理程序和预配置的策略. 这些构建块允许您在代码中表示授权评估,从而允许更丰富,可重用和容易测试的授权结构. 授权策略由一个或多个需求组成,并在 ...

  10. POJ2914 (未解决)无向图最小割|Stoer-Wagner算法|模板

    还不是很懂,贴两篇学习的博客: http://www.hankcs.com/program/algorithm/poj-2914-minimum-cut.html http://blog.sina.c ...