博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 2524 Ubiquitous Religions
阅读量:2441 次
发布时间:2019-05-10

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

Ubiquitous Religions
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 25316   Accepted: 12489

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

Hint

Huge input, scanf is recommended.

题意:在一个大学里面有的学生信仰多少个不同的宗教,注意一点就是下面没出现的学生,视为他们各自信仰不同的宗教

并查集模板:

int father[MAX],rank[MAX];void init(int n){	for (int i=1;i<=n;i++){		father[i]=i;		rank[i]=1;	}}int find(int x){	if (x!=father[x])		father[x] = find(father[x]);	//路径压缩	return father[x];}void Union(int x,int y){	int i=find(x);	int j=find(y);	if (i==j)		return;	if (rank[i]
本题代码:

#include 
#include
using namespace std;int father[50000+1],rank[50000+1];void init(int n){ for (int i=1;i<=n;i++){ father[i]=i; rank[i]=1; }}int find(int x){ if (x!=father[x]) father[x] = find(father[x]); //路径压缩 return father[x];}void Union(int x,int y){ int i=find(x); int j=find(y); if (i==j) return; if (rank[i]
>n>>m){ if (n==0&&m==0) break; init(n); for (int i=0;i
>x>>y; Union(x,y); } int ans=0; for (int i=1;i<=n;i++){ if (i==father[i]) ans++; } cout<<"Case "<
<<": "<
<

转载地址:http://rybqb.baihongyu.com/

你可能感兴趣的文章
Fedora Core 6的新特性(转)
查看>>
不得不说 僵尸网络导致垃圾邮件猛增(转)
查看>>
linux网络知识:TCP/IP设置内容(转)
查看>>
GNOME帮助Linux应用于商业桌面环境(转)
查看>>
linux网络知识:与网络设置有关的几个文件(转)
查看>>
Linux文件内容查询命令(转)
查看>>
libc.a 文件恢复(转)
查看>>
SCO UNIX上cpio命令详细用法(转)
查看>>
Linux系统可卸载内核模块完全指南(下)(转)
查看>>
思考-两个大表的关联.txt
查看>>
WIDTH_BUCKET和NTILE函数.txt
查看>>
sql plan baseline(二)
查看>>
第十章 sqlplus的安全性
查看>>
第十三章 sqlplus命令(一)
查看>>
第三章(backup and recovery 笔记)
查看>>
第一章(backup and recovery 笔记)
查看>>
第六章(backup and recovery 笔记)
查看>>
oracle备份功能简述
查看>>
[转]数据库三大范式
查看>>
恢复编录的创建和使用.txt
查看>>