并查集—宗教信仰

题目:

There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in finding out how many different religions students in your university believe in.

You know that there are n students in your university (0 < n <= 50000). It is infeasible for you to ask every student their religious beliefs. Furthermore, many students are not comfortable expressing their beliefs. One way to avoid these problems is to ask m (0 <= m <= n(n-1)/2) pairs of students and ask them whether they believe in the same religion (e.g. they may know if they both attend the same church). From this data, you may not know what each person believes in, but you can get an idea of the upper bound of how many different religions can be possibly represented on campus. You may assume that each student subscribes to at most one religion.

Input

The input consists of a number of cases. Each case starts with a line specifying the integers n and m. The next m lines each consists of two integers i and j, specifying that students i and j believe in the same religion. The students are numbered 1 to n. The end of input is specified by a line in which n = m = 0.

Output

For each test case, print on a single line the case number (starting with 1) followed by the maximum number of different religions that the students in the university believe in.

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

Huge input, scanf is recommended.

题意:

找出最多有多少种不同的宗教信仰。

思路:

将同一信仰的学生合并为一组,(即先将数组初始化为数组的下标,若遇到相同信仰的人就将其下标统一改成同一个固定下标,以便最后统计),最终for循环遍历一遍输出最终结果即可。

代码:

#include<stdio.h>
int f[50001],m,n,sum;
void init()
{int i;for(i=1;i<=n;i++)f[i]=i;   //将数组初始化为其下标return;
}
int getf(int k)//递归函数找爹,不停地去找爹,直到找到祖宗为止
{if(f[k]==k)return k;else{f[k]=getf(f[k]);//这里进行了路径压缩,提高寻找速度return f[k];}
}
void merge(int v,int u)
{int a,b;  //a,b分别为v和u的祖先,每次双方的会谈都必须是各自祖先级才行a=getf(v);b=getf(u);if(a!=b)//判断两个节点是否在同一个集合中,即是否为同一个祖先f[b]=a;   //把右边的集合作为左边集合的子集合return;
}
int main()
{int i,x,y,s=0;while(~scanf("%d%d",&n,&m)&&n&&m){s++;    //记录测试样例的个数sum=0;init();    //必须初始化for(i=1;i<=m;i++){scanf("%d%d",&x,&y);//开始合并拥有同一个宗教信仰的人merge(x,y);}for(i=1;i<=n;i++)//最后扫描有多少个宗教{if(f[i]==i)sum++;}printf("Case %d: %d\n",s,sum);}return 0;
}

并查集---宗教信仰相关推荐

  1. 1526:宗教信仰——简单并查集

    描述 世界上有许多宗教,你感兴趣的是你学校里的同学信仰多少种宗教. 你的学校有n名学生(0 < n <= 50000),你不太可能询问每个人的宗教信仰,因为他们不太愿意透露.但是当你同时找 ...

  2. 并查集_宗教信仰(并查集)

    题目描述 世界上有许多不同的宗教,现在有一个你感兴趣的问题:找出多少不同的宗教,在你的大学中的大学生信仰了多少种不同的宗教.你知道在你的大学有n个学生(0<n<= 50000).若直接问每 ...

  3. POJ 2524 宗教信仰 并查集 基础模板

    世界上有许多不同的宗教,现在有一个你感兴趣的问题:找出多少不同的宗教,在你的大学中的大学生信仰了多少种不同的宗教.你知道在你的大学有n个学生(0<n<= 50000).若直接问每一个学生的 ...

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

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

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

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

  6. Ubiquitous Religions (并查集)

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

  7. NOI 4.3 图论 1526:宗教信仰

    题目来源:http://noi.openjudge.cn/ch0403/1526/ 1526:宗教信仰 总时间限制: 5000ms    内存限制: 65536kB 描述 世界上有许多宗教,你感兴趣的 ...

  8. 宗教信仰解题报告(c++)

    宗教信仰 总时间限制: 5000ms 内存限制: 65536kB 描述 世界上有许多宗教,你感兴趣的是你学校里的同学信仰多少种宗教. 你的学校有n名学生(0 < n <= 50000),你 ...

  9. 计蒜客题解-T1260宗教信仰

    题目概况 链接: https://nanti.jisuanke.com/t/T1260 难度: 普及/提高-(计蒜客评级普及T3,个人评价T2) 题目分析 简化题目: 有几群人,分别信仰不同的宗教,有 ...

最新文章

  1. mysql主从复制实战
  2. Android localsocket 的基础和使用实践: 01
  3. 基于全注解的SpringMVC+Spring4.2+hibernate4.3框架搭建
  4. Java json拼接字符串_Java中拼接json格式字符串
  5. DescribingDesign Patterns 描述设计模式
  6. 卡巴斯基kis2010用授权文件激活,教你怎么才能导入授权文件
  7. 微信小程序实现类似微信提现、支付宝提现充值等 “自定义键盘“可实现自定义右下角搜索内容,手写input功能view组件,实现焦点获取事件
  8. 用计算机打女生节快乐,二十好几还没女朋友?女神节的打开方式出了问题
  9. IPFS和梅克尔森林
  10. 李子柒130个视频1万图片5万颜色数据可视化的背后,是古柳三年的念念不忘
  11. 正方形UVa201-紫书习题4-2(详细解答)
  12. AS运行app闪退,出现keeps stopping错误
  13. 使用idea手搓java计算器
  14. JavaWeb项目——基于Servlet实现的在线OJ平台 (项目问答+代码详解)
  15. [性能实验]INS山寨版---开篇
  16. Oracle SQL到DB2 SQL移植解决方案
  17. 001 电商平台核心链路_整体架构设计
  18. Java springBoot项目整合海康威视摄像头抓拍车辆功能
  19. java使用poi在word模板中替换柱状图、折线图、饼图、表格、文本、图片
  20. 银行普通员工正常的月收入在什么区间?

热门文章

  1. iOS设计规范HIG
  2. promise原理解析
  3. win7 文件夹不能同一窗口打开 完美解决!!!
  4. 虚幻引擎VR游戏开发基础教程
  5. 荣耀linux电脑怎么安装windows,荣耀笔记本不会安装 Win10 系统 只需这六步即可完美安装...
  6. Unity Shader 高光反射光照模型
  7. C语言详解 - 枚举类型
  8. 一看就懂的ReactJs入门教程(精华版)
  9. 菜鸟教程C语言-12
  10. html5语义化规范,html5语义化标签使用规范