题意:有编号为1到n的学生,然后有m组调查,每组调查中有a和b,表示该两个学生有同样的宗教信仰,问最多有多少种不同的宗教信仰

简单并查集

 //#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; const int maxn = + ;
int parent[maxn], n, m; int GetParent(int x)
{
return parent[x] == x ? x : parent[x] = GetParent(parent[x]);
} void Init(void)
{
for(int i = ; i <= n; ++i)
parent[i] = i;
} int main(void)
{
#ifdef LOCAL
freopen("2524in.txt", "r", stdin);
#endif int kase = ;
while(scanf("%d%d", &n, &m) == && (m + n))
{
Init();
for(int i = ; i < m; ++i)
{
int a, b;
scanf("%d%d", &a, &b);
int pa = GetParent(a);
int pb = GetParent(b);
if(pa != pb)
parent[pa] = pb;
}
int sum = ;
for(int i = ; i <= n; ++i)
if(parent[i] == i)
++sum;
printf("Case %d: %d\n", ++kase, sum);
}
return ;
}

代码君

POJ 2524 (简单并查集) Ubiquitous Religions的更多相关文章

  1. poj 2524 (并查集)

    http://poj.org/problem?id=2524 题意:在一所学校里面的人,都有宗教信仰,不过他们的宗教信仰有可能相同有可能不同,但你又不能直接去问他们,但你可以问他们和谁是同一个宗教.通 ...

  2. poj 2524 并查集 Ubiquitous Religions

    //#include<bits/stdc++.h> #include<iostream> #include<stdio.h> #define max1 50005 ...

  3. POJ 2492 (简单并查集) A Bug's Life

    题意:有编号为1~n的虫子,开始假设这种昆虫是异性恋.然后已知xi 和 yi进行交配,根据已知情况分析能否推理出其中是否有同性恋 这道题和 POJ 1182 食物链 十分相似,不过在更新与父节点关系的 ...

  4. Poj(2236),简单并查集

    题目链接:http://poj.org/problem?id=2236 思路很简单,傻逼的我输出写成了FALL,然后遍历的时候for循环写错了,还好很快我就Debug出来了. #include < ...

  5. POJ 2236 (简单并查集) Wireless Network

    题意: 有n个电脑坏掉了,分别给出他们的坐标 有两种操作,可以O x表示修好第x台电脑,可以 S x y表示x y是否连通 两台电脑的距离不超过d便可连通,两台电脑是连通的可以直接连通也可以间接通过第 ...

  6. POJ 2524(并查集)

    这道题多了一个检查是否包含所有元素 可以设一个cnt表示集合里的数量,再与外面比较 #include <cstdio> #include <iostream> #include ...

  7. poj 1611 简单并查集的应用

    #include<stdio.h> #define N 31000 int pre[N]; int find(int x) { if(x!=pre[x])     pre[x]=find( ...

  8. poj1611 简单并查集

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 32781   Accepted: 15902 De ...

  9. 1213 How Many Tables(简单并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单并查集,统计单独成树的数量. 代码: #include <stdio.h> #i ...

随机推荐

  1. Why we have to use epsg:900913 in OpenLayers

    reference:http://docs.openlayers.org/library/spherical_mercator.html epsg:900913 is spicfy the Soher ...

  2. HDU4718 The LCIS on the Tree(LCT)

    又是一枚LCT,写一发加深一下对LCT的理解.本题的坑爹之处就在于,它实在是太坑爹了.询问的是树路径上的最长连续上升的子串,考验的是怎么样去维护.一开始的想法是维护三个变量 ls,rs,mxl,分别表 ...

  3. iOS模型以及使用

    个人习惯,也可以不这样写 创建模型基类: #import <Foundation/Foundation.h> @interface WJBaseModel : NSObject //将字典 ...

  4. ExtJs布局之viewport

    <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...

  5. ExtJs布局之table

    <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...

  6. UVA 10943 How do you add? DP

    Larry is very bad at math — he usually uses a calculator, whichworked well throughout college. Unfor ...

  7. Linux - wxWidgets安装和编译HelloWorld

    安装参考http://codelite.org/LiteEditor/WxWidgets30Binaries#toc2 源 /etc/apt/source.list deb http://repos. ...

  8. MyEclipse — Maven+Spring+Struts+Hibernate 整合 [学习笔记-2]

    引入Spring 修改 pox.xml 文件 添加jar包引用 <!-- spring3 --> <dependency> <groupId>org.springf ...

  9. 快速构建自己的CentOS发行版

    一.制作LTOS具体过程 光盘结构介绍 * isolinux 目录存放光盘启动时的安装界面信息 * images 目录包括了必要的启动映像文件 * CentOS 目录存放安装软件包及信息 * .dis ...

  10. Linux inotify功能及实现原理

    http://www.cnblogs.com/jiejnan/archive/2012/05/18/2507476.html 简介: 当需要对 Linux®文件系统进行高效率.细粒度.异步地监控时,可 ...