并查集合并

#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. 微信JS接口

      微信JS接口 分享到朋友圈 分享给朋友 分享到QQ 拍照或从手机相册中选图 识别音频并返回识别结果 使用微信内置地图查看位置来源:http://www.cnblogs.com/txw1958/p/ ...

  2. Python 安装easy_install详解

    easy_install 真的特别方便.我之前安装pip.wheel.bs4.requests等都是用python setup.py install 安装,操作步骤还不算很烦(但是对比easy_ins ...

  3. 数据结构图文解析之:二叉堆详解及C++模板实现

    0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...

  4. php魔术方法使用场景

    php魔术方法-----__tostring(),__invoke,__call(),__callStatic ... __tostring(),__invoke() __tostring()方法是在 ...

  5. Leetcode 40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  6. eclipse发布web项目到生产环境的方式汇总(tomcat)

    参考: http://www.cnblogs.com/xiohao/p/3689832.html http://www.111cn.net/jsp/J2EE-EJB/90337.htm 我收集了几点: ...

  7. 项目中CKEditor修改宽度为自适应

    项目中用到CKEditor,在config.js中直接定义config.width使得宽度无法自适应,尝试了好多次后发现了一种方法: 放弃在config.js中配置宽度 在页面检查元素,找到id为ck ...

  8. rabbitmq python

    import pika connection = pika.BlockingConnection(pika.ConnectionParameters( 'localhost',9672)) chann ...

  9. Unity3D 搭建优雅的UI框架

    为什么要使用UI框架?直接使用NGUI或UGUI一拖一拉直接搭载出界面不就行了? 我相信很多小白,包括我在刚学习Unity3D UI的时候都这样想过. 我的第一款款Unity2D游戏<山地赛车& ...

  10. 【Alpha版本】冲刺-Day1

    队伍:606notconnected 会议时间:11月9日 会议总结 张斯巍(433) 今天安排:设计登陆界面背景,图标的大小规定 完成度:90% 明天计划:主界面图标的修改,侧边栏背景设计,个人信息 ...