博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj2524 Ubiquitous Religions(并查集)
阅读量:6158 次
发布时间:2019-06-21

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

Ubiquitous Religions
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 39239   Accepted: 18708

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 分析:并查集模板题。
#include
#include
#include
using namespace std;int M,N,cas=0;//N个点M条边 int pre[50010];//pre[i]=x表示点i的父亲节点为点x void init()//初始化N个元素pre[x]=x,即开始时有N个集合 { for(int i=0;i<=N;i++) pre[i]=i;}int find(int x)//查询某点所在树的根 { int root=x; while(pre[root]!=root) root=pre[root]; int i=x,k; while(k!=root)//路径压缩,让x到根经过的所有节点直接指向根节点 { k=pre[i]; pre[i]=root; i=k; } return root;}int unite(int x,int y){ int rootx=find(x),rooty=find(y); if(rootx!=rooty)//合并 {pre[rootx]=pre[rooty];return 1;} return 0;}int main(){ while(scanf("%d%d",&N,&M)) { init(); if(!M&&!N) break; int x,y,ans=N; while(M--) { scanf("%d%d",&x,&y); ans-=unite(x,y); } printf("Case %d: %d\n",++cas,ans); } return 0;}
View Code

 

 

转载于:https://www.cnblogs.com/ACRykl/p/9103982.html

你可能感兴趣的文章
关于程序的单元测试
查看>>
mysql内存优化
查看>>
都市求生日记第一篇
查看>>
Java集合---HashMap源码剖析
查看>>
SQL优化技巧
查看>>
thead 固定,tbody 超出滚动(附带改变滚动条样式)
查看>>
Dijkstra算法
查看>>
css 动画 和 响应式布局和兼容性
查看>>
csrf 跨站请求伪造相关以及django的中间件
查看>>
MySQL数据类型--与MySQL零距离接触2-11MySQL自动编号
查看>>
生日小助手源码运行的步骤
查看>>
Configuration python CGI in XAMPP in win-7
查看>>
bzoj 5006(洛谷 4547) [THUWC2017]Bipartite 随机二分图——期望DP
查看>>
CF 888E Maximum Subsequence——折半搜索
查看>>
欧几里德算法(辗转相除法)
查看>>
面试题1-----SVM和LR的异同
查看>>
MFC控件的SubclassDlgItem
查看>>
如何避免历史回退到登录页面
查看>>
《图解HTTP》1~53Page Web网络基础 HTTP协议 HTTP报文内的HTTP信息
查看>>
unix环境高级编程-高级IO(2)
查看>>