题意:有编号为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. Itunes connect State: Developer Action Needed

    In-App Purchases have been returned and are highlighted in the table below. Your In-App Purchase has ...

  2. POJ 2141

    #include<iostream> #include<stdio.h> using namespace std; int main() { //freopen("1 ...

  3. ThreadPoolTaskExecutor异步的处理报警发送邮件短信比较耗时的东东

    package com.elong.ihotel.util; import org.springframework.beans.factory.DisposableBean; import org.s ...

  4. 安装wps for linux无法启动

    我下载的是deb包,双击安装完之后,打开wps没有反应,重启了一下 计算机也不行. 改从命令行出现如下信息: /opt/kingsoft/wps-office/office6/wps: error w ...

  5. POJ 3177 Redundant Paths(Tarjan)

    题目链接 题意 : 一个无向连通图,最少添加几条边使其成为一个边连通分量 . 思路 :先用Tarjan缩点,缩点之后的图一定是一棵树,边连通度为1.然后找到所有叶子节点,即度数为1的节点的个数leaf ...

  6. Zabbix监控解决方案

    思通运维监控主要用来监控IT 基础设施组件的可用性和性能.监控项目是不受限制的,并且可以对IT 基础设施健康状态进行复杂分析.通过确定IT 系统问题的“来源”,使用户快速响应故障来降低宕机成本. 网络 ...

  7. Java学习笔记之:Java 内部类

    一.介绍 内部类:存在与类中的类就是内部类,一般用于Android开发. 可以把内部类理解成一种继承关系 1.普通内部类 2.局部内部类 3.静态内部类 4.匿名内部类 二.笔记 1.普通内部类 /* ...

  8. qt中如何启动其他应用程序(如果不成功,还有许多原因即QProcess::ProcessError可供分析)

    类 QDesktopServices 提供的方法 访问 常用的桌面 服务 , 如 浏览 器 . 播放器. 电子邮件客户端 . 我们 使用 QDesktopServices :: openUrl(url ...

  9. Delphi操作XML的几个博客

    http://www.cnblogs.com/acuier  整整十几篇,省得我自己研究,学一下就可以了. http://www.cnblogs.com/del/category/113561.htm ...

  10. 【查找结构5】多路查找树/B~树/B+树

    在前面专题中讲的BST.AVL.RBT都是典型的二叉查找树结构,其查找的时间复杂度与树高相关.那么降低树高自然对查找效率是有所帮助的.另外还有一个比较实际的问题:就是大量数据存储中,实现查询这样一个实 ...