并查集合并

#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. 红米3 TWRP-3.0.2(android_6.0.1_r72分支)中文版Recovery更新于20161018

    TWRP3.0.2更新简介 TWRP是TeamWin团队https://github.com/TeamWin/Team-Win-Recovery-Project的开源项目,也是Omnirom系统默认的 ...

  2. bc#54 div2

    用小号做的div2 A:竟然看错了排序顺序...白白WA了两发 注意读入一整行(包括空格):getline(cin,st) [gets也是资瓷的 #include<iostream> us ...

  3. 对于angularJS的一点思考

    已经找好工作近两周了,入职基本上还算顺利,自己两年来的挑灯夜战也算是有了收获,于是这两周基本上是按部就班的工作,没有学习什么新技术.在上个公司的时候,同事在项目中使用angularJs,之前他也没有接 ...

  4. Html中行内样式的设置

    Html中行内样式的设置.. <html> <head> <title>显示的页面选项卡标题</title> <style type=" ...

  5. live555库中的testH264VideoStreamer实例

    1.h264文件的推送 testH264VideoStreamer.cpp文件的开头就定义了 char const* inputFileName = "test.264"; 后面接 ...

  6. Centos下安装jdk

    下载 由于oracle官网下载jdk需要网站验证,所以不能使用wget直接下载. 一种比较快的方式是在本地下载tar.gz或者rpm,之后上传到Linux. tar.gz格式只需解压,放到指定目录下, ...

  7. Protecting against XML Entity Expansion attacks

    https://blogs.msdn.microsoft.com/tomholl/2009/05/21/protecting-against-xml-entity-expansion-attacks/ ...

  8. Npoi导入导出Excel操作

    之前公司的一个物流商系统需要实现对订单的批量导入和导出,翻阅了一些资料,最后考虑使用NPOI实现这个需求. 在winform上面实现excel操作:http://www.cnblogs.com/Cal ...

  9. 7 款顶级开源 BI(商务智能)软件和报表工具

    在这个信息化时代,每分每秒都产生海量数据.在海量数据中,挖掘出有用的数据,并且能以较人性化.直观的方式展示这些数据,变得尤为重要.本文将介绍 7款顶级开源 BI(商务智能)软件和报表工具,用于商业数据 ...

  10. 深入理解Java中的final关键字

    Java中的final关键字非常重要,它可以应用于类.方法以及变量.这篇文章中我将带你看看什么是final关键字?将变量,方法和类声明为final代表了什么?使用final的好处是什么?最后也有一些使 ...