POJ 2524 (简单并查集) Ubiquitous Religions
题意:有编号为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的更多相关文章
- poj 2524 (并查集)
http://poj.org/problem?id=2524 题意:在一所学校里面的人,都有宗教信仰,不过他们的宗教信仰有可能相同有可能不同,但你又不能直接去问他们,但你可以问他们和谁是同一个宗教.通 ...
- poj 2524 并查集 Ubiquitous Religions
//#include<bits/stdc++.h> #include<iostream> #include<stdio.h> #define max1 50005 ...
- POJ 2492 (简单并查集) A Bug's Life
题意:有编号为1~n的虫子,开始假设这种昆虫是异性恋.然后已知xi 和 yi进行交配,根据已知情况分析能否推理出其中是否有同性恋 这道题和 POJ 1182 食物链 十分相似,不过在更新与父节点关系的 ...
- Poj(2236),简单并查集
题目链接:http://poj.org/problem?id=2236 思路很简单,傻逼的我输出写成了FALL,然后遍历的时候for循环写错了,还好很快我就Debug出来了. #include < ...
- POJ 2236 (简单并查集) Wireless Network
题意: 有n个电脑坏掉了,分别给出他们的坐标 有两种操作,可以O x表示修好第x台电脑,可以 S x y表示x y是否连通 两台电脑的距离不超过d便可连通,两台电脑是连通的可以直接连通也可以间接通过第 ...
- POJ 2524(并查集)
这道题多了一个检查是否包含所有元素 可以设一个cnt表示集合里的数量,再与外面比较 #include <cstdio> #include <iostream> #include ...
- poj 1611 简单并查集的应用
#include<stdio.h> #define N 31000 int pre[N]; int find(int x) { if(x!=pre[x]) pre[x]=find( ...
- poj1611 简单并查集
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 32781 Accepted: 15902 De ...
- 1213 How Many Tables(简单并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单并查集,统计单独成树的数量. 代码: #include <stdio.h> #i ...
随机推荐
- Linux中yum和apt-get用法及区别
Linux中yum和apt-get用法及区别 一般来说著名的linux系统基本上分两大类: 1.RedHat系列:Redhat.Centos.Fedora等 2.Debian系列:Debi ...
- 利用Openvswitch实现不同物理机中的Docker容器互连
1. 测试环境 75机(10.11.150.75):Red Hat Enterprise Linux Server 7.0,无外网访问权限,已安装Docker Server 74机(10.11.150 ...
- hadoop配置错误
经过上一周的郁闷期(拖延症引发的郁闷),今天终于开始步入正轨了.今天主要是解决hadoop配置的错误以及网络时断时续的问题. 首先说明一下之前按照这篇文章的方法配置完全没有问题,但是等我配置好了发现h ...
- 用C语言代码判别CPU的大小端模式
Big-endian和little-endian是描述排列存储在计算机内存里的字节序列的术语. Big-endian是一种大值的一端(序列中更典型值)存在前面(在最小的存储地址)的顺序. ...
- Shell script for logging cpu and memory usage of a Linux process
Shell script for logging cpu and memory usage of a Linux process http://www.unix.com/shell-programmi ...
- hdu2023 求平均成绩 ~~很闲~~~
#include<iostream> #include<stdio.h> #include<math.h> #include<string.h> #in ...
- Mita和Maui
参考:http://blog.csdn.net/popeer/article/details/6002541 UI自动化的框架,MS内部使用的不对外开放的框架.UI Automation 离不开像Mi ...
- module.xml 快捷代码
以下内容为淘宝装修模块描述文件(module.xml)快捷代码块,可以快速调整模块信息,详解请查阅>> http://open.taobao.com/doc/detail.htm?id=1 ...
- WordPress主题制作教程4:调用指定页面内容
假设页面page_id=86 $page_id = 86; echo "标题:".get_post($page_id)->post_title; echo "内容: ...
- MAVEN ERROR: unable to find valid certification path to requested target 解决办法
第一次使用MAVEN编译项目,出现如下错误 解决办法:Maven的setting.xml中添加如下代码 <mirrors> <mirror> <id>Central ...