并查集合并

#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. deepin 15.3 安装配置nginx

    1.安装nginx sudo apt-get install nginx 2.配置nginx sudo gedit /etc/nginx/sites-enabled/default 找到:index ...

  2. [Think In Java]基础拾遗4 - 并发

    第21章节 并发 1. 定义任务 任务:任务就是一个执行线程要执行的一段代码.(在C语言中就是函数指针指向的某个地址开始的一段代码) [记忆误区]:任务不是线程,线程是用来执行任务的.即任务由线程驱动 ...

  3. 【bzoj1913】 Apio2010—signaling 信号覆盖

    http://www.lydsy.com/JudgeOnline/problem.php?id=1913 (题目链接) 题意 给出一个平面上n个点,求任选3个点画一个圆所包含的点的期望值. Solut ...

  4. 11月7日下午PHP----PDO访问方式操作数据库

    MySQLI是专门访问MySQL数据库的,不能访问其它数据库.PDO可以访问多种的数据库,它把操作类合并在一起,做成一个数据访问抽象层,这个抽象层就是PDO,根据类操作对应的数据库.mysqli是一个 ...

  5. winston写日志(译)

    使用 有两种方式去使用winston,直接通过默认的logger,或者实例化自己的Logger,前者设计的目的是在你的应用程序中共享logger比较方便. 使用默认Logger 使用默认的logger ...

  6. Recall, Precision and F-score

    F1 score (also F-score or F-measure) ,调和平均数稍微有点不好理解,最关键的是,不知道分子的情况下,采用调和平均数.

  7. Mac Sublime Text complie python .py error /bin/bash: shell_session_update: command not found

    1.get the rvm version rvm -v 2.make sure the version at least 1.26 above. 3.then go ahead rvm get he ...

  8. WinPcap4.13无法安装解决方法

    360软件管家提示把WinPcap更新至版本:4.1.0.2980,于是把旧版下载后,可新版本怎么也无法顺利安装,出现以下信息,旧版本已安装,关闭所有winpcap-based应用程序和再次运行安装程 ...

  9. Python之路Python3【第零篇】Python2 & Python3区别持续更新~

    print def print(self, *args, sep=' ', end='\n', file=None): # known special case of print "&quo ...

  10. LInux升级Python版本2.7.11所遇问题汇总

    首先请原谅我使用校园网络,基本上打不开谷歌,网络搜取得帮助均来自度娘. 对于我这个linux新手 IT 新手来说,自己升级点东西好担心,万一出错,可能都要重来.... 参照度娘内容和自己摸索,今天晚上 ...