POJ 2524 并查集
Ubiquitous Religions
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 23580 Accepted: 11609
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 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.
Source
Alberta Collegiate Programming Contest 2003.10.18
<span style="color:#6633ff;">*********************************************************************** author : Grant Yuan
time : 2014.7.25
algorithm : 并查集 *********************************************************************** #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define MAX 50001 using namespace std;
int ans,par[MAX];
long long n,m;
int ct=1; void init()
{
for(int i=0;i<=n;i++)
{
par[i]=i;
}
} int find(int x)
{
if(x==par[x]) return x;
else
return par[x]=find(par[x]);
} void unite(int x,int y)
{
int xx,yy;
xx=find(x);yy=find(y);
if(yy==xx) return;
par[yy]=xx;ans--; } int main()
{
while(1){
cin>>n>>m;int a,b;
if(n==0&&m==0) break;
init();
ans=n;
for(int i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
unite(a,b);
}
printf("Case %d: %d\n",ct++,ans); }
return 0;
}
</span>
POJ 2524 并查集的更多相关文章
- poj 2524 并查集 Ubiquitous Religions
//#include<bits/stdc++.h> #include<iostream> #include<stdio.h> #define max1 50005 ...
- poj 1984 并查集
题目意思是一个图中,只有上下左右四个方向的边.给出这样的一些边, 求任意指定的2个节点之间的距离. 就是看不懂,怎么破 /* POJ 1984 并查集 */ #include <stdio.h& ...
- poj 1797(并查集)
http://poj.org/problem?id=1797 题意:就是从第一个城市运货到第n个城市,最多可以一次运多少货. 输入的意思分别为从哪个城市到哪个城市,以及这条路最多可以运多少货物. 思路 ...
- POJ 2492 并查集扩展(判断同性恋问题)
G - A Bug's Life Time Limit:10000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u S ...
- POJ 2492 并查集应用的扩展
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28651 Accepted: 9331 Descri ...
- POJ 3228 [并查集]
题目链接:[http://poj.org/problem?id=3228] 题意:给出n个村庄,每个村庄有金矿和仓库,然后给出m条边连接着这个村子.问题是把所有的金矿都移动到仓库里所要经过的路径的最大 ...
- poj 1733 并查集+hashmap
题意:题目:有一个长度 已知的01串,给出多个条件,[l,r]这个区间中1的个数是奇数还是偶数,问前几个是正确的,没有矛盾 链接:点我 解题思路:hash离散化+并查集 首先我们不考虑离散化:s[x] ...
- poj 3310(并查集判环,图的连通性,树上最长直径路径标记)
题目链接:http://poj.org/problem?id=3310 思路:首先是判断图的连通性,以及是否有环存在,这里我们可以用并查集判断,然后就是找2次dfs找树上最长直径了,并且对树上最长直径 ...
- POJ 3657 并查集
题意: 思路: 1.二分+线段树(但是会TLE 本地测没有任何问题,但是POJ上就是会挂--) 2.二分+并查集 我搞了一下午+一晚上才搞出来----..(多半时间是在查错) 首先 如果我们想知道这头 ...
随机推荐
- poj 2388 Who's in the Middle
Who's in the Middle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31149 Accepted: 1 ...
- EL 表达式中自己定义函数
第一步: 在WEB-INF/tld/ 文件夹下创建一个func.tld文件例如以下: <taglib xmlns="http://java.sun.com/xml/ns/j2ee&qu ...
- 程序员的绘图利器 — Gnuplot
介绍 Gnuplot is a command-line program that can generate two- and three-dimensional plots. It is fre ...
- iOS平台下cookie的使用
iOS平台下cookie的使用 首先,先介绍下iOS对cookie的操作的两个类: 帖子来源于:http://blog.csdn.net/chun799/article/details/1720690 ...
- CSS样式之背景、文本
一.背景 1.背景颜色用background-color属性,例如:body{background-color:red} 2.用图像做背景用background-image属性,例如b ...
- Windows消息机制(转)1
Windows的应用程序一般包含窗口(Window),它主要为用户提供一种可视化的交互方式,窗口是总是在某个线程(Thread)内创建的.Windows系统通过消息机制来管理交互,消息(Message ...
- Struts2.3.16.3 基本9个jar包
实践证明,Struts2.3.16.3 至少要下面9个Jar包才能正常启动. commons-fileupload-1.3.1.jar commons-logging-1.1.3.jar freema ...
- Qt将表格table保存为excel(odbc方式)
首先是保存excel的方法,可参照: http://dzmlmszp.blog.163.com/blog/static/179271962014819111812531/ ok,进入正题. 现在我有一 ...
- Oracle11g R2学习系列 之十 解决EM不能用
不知道是什么原因https://localhost:1158/em,今天突然就不能用了.做了好多搜索也没有解决.现象是在services.msc中,不能重启OracleDBConsole服务,提示: ...
- 数据链路层-点对点协议PPP
在通信质量较差的年代,在数据链路层使用可靠传输协议曾是一个好的办法.因此,能实现可靠传输的高级数据链路控制HDLC(High-Level Data Link Control)就称为当时比较流行的数据链 ...