Description

当今世界有很多不同的宗教,很难通晓他们。你有兴趣找出在你的大学里有多少种不同的宗教信仰。
你知道在你的大学里有n个学生(0 < n <= 50000) 。你无法询问每个学生的宗教信仰。此外,许多学生不想说出他们的信仰。避免这些问题的一个方法是问m(0 <= m <= n(n - 1)/ 2)对学生, 问他们是否信仰相同的宗教( 例如他们可能知道他们两个是否去了相同的教堂) 。在这个数据中,你可能不知道每个人信仰的宗教,但你可以知道校园里最多可能有多少个不同的宗教。假定每个学生最多信仰一个宗教。

Input

有多组数据。对于每组数据:
第一行:两个整数n和m。
以下m行:每行包含两个整数i和j,表示学生i和j信仰相同的宗教。学生编号从1到n。
输入的最后一行中,n = m = 0。

Output

对于每组测试数据,输出一行,输出数据序号( 从1开始) 和大学里不同宗教的最大数量。(参见样例)

Sample Input

10 9
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
10 4
2 3
4 5
4 8
5 8
0 0

Sample Output

Case 1: 1
Case 2: 7

Hint

输入巨大,推荐使用scanf
 
这一题在初步掌握并查集之后也不难,我之所以还是要写上来是因为我之前犯了一个错误,就是节点的更新问题,我妄想用题目所给的数据完成合并之后通过统计不同数字的个数来得到答案
事实证明那是错的,因为有的节点还没来得及更新,比如在合并时先把1指向了0,然后数据给了1,2,合并时2指向了1,因为没有继续查找(如果调用FindRoot(2)就可以使2指向0了,仔细
想象就是因为路径压缩的原因),所以让他们变成了不同的集合
 
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<algorithm>
using namespace std; const int MX = 55555;
int stu[MX];
int n, m; void ini() {
for (int i = 1; i <= n; i++) {
stu[i] = i;
}
} int FindRoot(int pos) {
return stu[pos] == pos ? pos : (stu[pos] = FindRoot(stu[pos]));
} int main() {
//freopen("input.txt", "r", stdin);
int sign = 1;
while (scanf("%d %d", &n, &m), n || m) {
ini();
int a, b;
for (int i = 0; i < m; i++) {
scanf("%d %d", &a, &b);
int root1 = FindRoot(a);
int root2 = FindRoot(b);
if (root1 != root2) {
n--;//正是为了避免由于没有更新节点的问题,才用这种办法来求最后还有几个集合,合并一次集合就少一个
stu[root2] = root1;
}
}
printf("Case %d: %d\n", sign++, n);
}
return 0;
}

POJ - Ubiquitous Religions的更多相关文章

  1. 【原创】poj ----- 2524 Ubiquitous Religions 解题报告

    题目地址: http://poj.org/problem?id=2524 题目内容: Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 6 ...

  2. poj 2524:Ubiquitous Religions(并查集,入门题)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23997   Accepted:  ...

  3. POJ 2524 Ubiquitous Religions

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 20668   Accepted:  ...

  4. Ubiquitous Religions 分类: POJ 2015-06-16 17:13 11人阅读 评论(0) 收藏

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 26678   Accepted: ...

  5. poj 2524 Ubiquitous Religions(宗教信仰)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 30666   Accepted: ...

  6. POJ 2524 :Ubiquitous Religions

    id=2524">Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 231 ...

  7. POJ 2524 Ubiquitous Religions 解题报告

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34122   Accepted:  ...

  8. [ACM] POJ 2524 Ubiquitous Religions (并查集)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23093   Accepted:  ...

  9. poj 2524 Ubiquitous Religions 一简单并查集

    Ubiquitous Religions   Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 22389   Accepted ...

随机推荐

  1. 与你相遇好幸运,Sail.js新建模型控制器

    sails generate api user  创建了user的controller和models sails generate api user index 创建了user的controller和 ...

  2. HTML5学习之视频与音频(三)

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  3. [Oracle] PL/SQL学习笔记

    -- 1. 使用一个变量 declare -- Local variables here v_name ); begin -- Test statements here select t.user_n ...

  4. 使用python做科学计算

    这里总结一个guide,主要针对刚开始做数据挖掘和数据分析的同学 说道统计分析工具你一定想到像excel,spss,sas,matlab以及R语言.R语言是这里面比较火的,它的强项是强大的绘图功能以及 ...

  5. Linux crontab 定时任务详解

    1.每小时执行一次脚本 * */1 * * * /etc/init.d/smb restart #不是所有的系统都支持“*/1”这种写法可以试试: 0 * * * * /etc/init.d/smb  ...

  6. ASP.NET MVC使用过滤器进行权限控制

    1.新建MVC项目 2.找到Models文件夹,新建 LoginCheckFilterAttribute 类 public class LoginCheckFilterAttribute : Acti ...

  7. string、math、random、datetime类

    1.string类 变量.Replace("想要替换掉的字符或字符串","转换后的字符或字符串");//替换 练习:判断邮箱格式是否正确            ...

  8. Chrome浏览器之 Postman 安装

    Postman 是一款发送 HTTP 请求的 Chrome 插件.开发后端程序的同学可以用它来测试自己写的应用程序是否能够正常访问. 现在由于国内的网络限制, Chrome 浏览器里无法访问“扩展程序 ...

  9. Android:res之shape制作圆角、虚线、渐变

    xml控件配置属性 android:background="@drawable/shape" 标签 corners ----------圆角gradient ----------渐 ...

  10. background为圆角的表框,dp转Px,Px转dp

    圆角边框<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="ht ...