POJ 2524 Ubiquitous Religions

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <list>
#include <map>
#define INF 0x3f3f3f3f
#define MID l + r >> 1
#define lsn rt << 1
#define rsn rt << 1 | 1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qrusing namespace std;
typedef long long ll;
const int maxN = 5e4 + 5;
const int maxM = 2e4 + 5;int n, m;
int root[maxN], high[maxN], size[maxN];
void init()
{for(int i = 1 ; i <= n ; i ++){root[i] = i;high[i] = 0;size[i] = 1;}
}
int Find(int x) { return root[x] == x ? x : root[x] = Find(root[x]); }
int Same(int x, int y) { return Find(x) == Find(y); }
void Merge(int x, int y)
{x = Find(x) ; y  = Find(y);if(high[x] < high[y] || size[x] < size[y]){root[x ] = y;size[y] += size[x];}else{root[y] = x;size[x] += size[y];if(high[x] == high[y]) high[x] ++ ;}
}int main()
{int Case = 0;while(~scanf("%d%d", &n, &m) && (n || m)){init(); ++ Case;for(int i = 0; i < m ; i ++ ){int a, b; scanf("%d%d", &a, &b);if(!Same(a,b)) Merge(a, b);}set<int> st;for(int i = 1; i <= n ; i ++ )st.insert(Find(i));printf("Case %d: %d\n", Case, st.size());}return 0;
}

【并查集】POJ 2524 Ubiquitous Religions相关推荐

  1. SSL-ZYC POJ 2524 Ubiquitous Religions

    题目大意: 你知道你的大学里有N个学生.每个人都信仰宗教,你向每个学生请教他们的宗教信仰是不可行的.避免这些问题的一种方法是问M对学生,问他们是否相信同一宗教.从这些数据中,你可能不知道每个人都相信什 ...

  2. poj 2524 Ubiquitous Religions (简单并查集)

    题目链接:http://poj.org/problem?id=2524 There are so many different religions in the world today that it ...

  3. poj 2524 Ubiquitous Religions (并查集)

    题目:http://poj.org/problem?id=2524 题意:问一个大学里学生的宗教,通过问一个学生可以知道另一个学生是不是跟他信仰同样的宗教.问学校里最多可能有多少个宗教. 也就是给定一 ...

  4. 并查集入门+初级专题训练

    介绍   摘自罗勇军,郭卫斌的<算法竞赛入门到进阶>上的说明:   并查集(Disjoint Set)是一种非常精巧而且食用的数据结构,它主要用于处理一些不相交集合的合并问题.经典的例子有 ...

  5. 【转】并查集MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU] 1213 How Many Tables 基础并查集★ 1272 小希的迷宫 ...

  6. Ubiquitous Religions (并查集)

    Ubiquitous Religions POJ - 2524 当今世界有许多不同的宗教,很难跟踪它们.你有兴趣找出你大学里有多少不同的宗教学生相信. 你知道你的大学里有 n 学生 (0 < n ...

  7. POJ2524——宗教(Ubiquitous Religions)【图论,并查集】

    正题 题目链接: http://poj.org/problem?id=2524 大意 有n个学生,告诉你哪两个学生的宗教相等,求校园里有多少个宗教. 解题思路 并查集链接就好了 代码 #include ...

  8. POJ-2524 Ubiquitous Religions(无处不在的宗教)解题报告(并查集)

    目录 题目描述 思路分析 今天没有考试,那就再刷点题吧(难道就不怕c语言挂科吗?).因为昨天写了道并查集,所以今天再来一道,还是有所收获的. 题目描述 题目:https://vjudge.net/pr ...

  9. POJ 并查集 题目汇总 ——czyuan原创(转)

    继续数据结构的复习,本次的专题是:并查集. 并查集,顾名思义,干的就是"并"和"查"两件事.很多与集合相关的操作都可以用并查集高效的解决. 两个操作代码:    ...

最新文章

  1. android 不重新加载,android – 返回Activity而不重新创建(不调用onCreate())
  2. 静态链接库与动态链接库的区别(Sqlite\Visual Studio 2017)
  3. C#-数组定义及使用数组的好处 046
  4. java B2B2C springmvc mybatis电子商城系统(四)Ribbon
  5. 天池 在线编程 到达终点
  6. 2021计算机应用基础统考,2021年度计算机应用基础统考练习题及答案.doc
  7. unity打包android 乱码,Unity3d发布IOS9应用时出现中文乱码的解决方法
  8. 如何生成可变表头的excel
  9. 泰坦尼克 数据集_数据分析(一):11招入门数据分析
  10. 查看oracle空间使用情况,Oracle 查看数据库空间使用情况
  11. 常见图像加密性能评价指标(详解加python实现)
  12. 集成谷歌地图不显示的问题
  13. 专注于Win7系统清除附带推广和工具
  14. 第62次上IM课(IMO71:How to use Adverb)
  15. ARIMA模型实例讲解:时间序列预测需要多少历史数据?
  16. “浅尝”JavaScript设计模式
  17. IIS 编译器错误消息: CS0016未能写入输出文件“c:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files\roo
  18. 使用键盘发布手柄控制信息
  19. 苹果微信换行怎么打_微信空白朋友圈怎么发?安卓苹果双平台教程
  20. Dart笔记(10):Runes

热门文章

  1. 15个魔术方法的总结
  2. c语言调用system返回值,在C语言中获取用system函数调用shell的返回值
  3. 成都平面设计培训学校那家好,平面设计培训班学费一般多少:夏雨老师
  4. 赖春林Python初学笔记 Day0.1
  5. html+css布局实例:CSS过渡-Transitions文字逐渐变大的效果
  6. 如何将webp与JPG/PNG互转的两种方法
  7. vue2中使用VantUI
  8. 提取图片色彩比例(Java 代码实现)
  9. wps、word转pdf断字换行
  10. ts流中的时间概念: pcr,pts,dts 实例解说