博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ2524——Ubiquitous Religions
阅读量:4362 次
发布时间:2019-06-07

本文共 2398 字,大约阅读时间需要 7 分钟。

Ubiquitous Religions

Description

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 91 21 31 41 51 61 71 81 91 1010 42 34 54 85 80 0

Sample Output

Case 1: 1Case 2: 7 题目大意:有n个学生,每个都有信仰的宗教。。。 有m组数据(i,j)表示i与j同学是同一个宗教的。 输出有多少个宗教。 解题思路:简单的并查集。 Code:
1 #include
2 #include
3 int fat[60000]; 4 int find(int x) 5 { 6 if (x!=fat[x]) 7 fat[x]=find(fat[x]); 8 return fat[x]; 9 }10 void join(int x,int y)11 {12 int fx=find(x),fy=find(y);13 if (fx!=fy)14 fat[fx]=fy;15 }16 int main()17 {18 int n,m,times=0,i,t1,t2,cnt;19 while (scanf("%d %d",&n,&m)!=EOF)20 {21 times++;22 cnt=0;23 if (n==0&&m==0) break;24 for (i=1; i<=n; i++)25 fat[i]=i;26 for (i=1; i<=m; i++)27 {28 scanf("%d %d",&t1,&t2);29 join(t1,t2);30 }31 for (i=1; i<=n; i++)32 if (fat[i]==i) cnt++;33 printf("Case %d: %d\n",times,cnt);34 }35 return 0;36 }

 

转载于:https://www.cnblogs.com/Enumz/p/3763240.html

你可能感兴趣的文章
Django连接MySQL数据库
查看>>
漫游Kafka入门篇之简单介绍(1)
查看>>
redis学习之旅-初识Redis
查看>>
WinForm 小程序 NotePad
查看>>
JSTL 核心标签库 使用
查看>>
线程池ThreadPool
查看>>
hibernate入门实例
查看>>
WPF路由事件二:路由事件的三种策略(转)
查看>>
Java中的内存泄露
查看>>
asp.net 自定义控件验证FCKeditor是否为空
查看>>
oracle 查看表空间的脚本
查看>>
Python 描述符是什么?以及如何实现
查看>>
程序员的激情其实是一种痛苦
查看>>
MySQL后台线程的清理工作
查看>>
连接mysql数据库,创建用户模型
查看>>
cogs1885 [WC2006]水管局长数据加强版
查看>>
paramiko模块
查看>>
[原创]茗洋AaronYang的 jquery.myselect.js 我的一次前端突破[上]
查看>>
1083 是否存在相等的差
查看>>
配置APP的图标
查看>>