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.二分+并查集 我搞了一下午+一晚上才搞出来----..(多半时间是在查错) 首先 如果我们想知道这头 ...
随机推荐
- [Javascript] Regex: '$`', '$&', '$''
var input = "foobar"; var result = input.replace('bar', '$`'); // $`: replace 'bar' with e ...
- ajaxFileUpload js判断类型
function ajaxFileUpload() { var File_box = document.getElementById('V_file'); var extend = Fil ...
- asp.net 正则表达式
在平时的开发中常常用到替换法: 普通的字符串替换可以使用string.replace(string,string),但是比较难解决HTML文本的替换. 经我几番查找,终也找出解决办法:正则匹配替换. ...
- Solr-4.10.2与Tomcat整合
1.将下载的solr解压至D:\solr,拷贝d:\solr\solr-4.10.2\example\webapps\solr.war到Tomcat的webapps\目录中.直接解压 solr.war ...
- C#操作Flash动画
对于在C#开发的过程中没有接触过Flash相关开发的人员来说,没有系统的资料进行学习,那么这篇文档针对于初学者来说是很好的学习DEMO. 本文章中的DEMO实现了C#的COM控件库中本来就带有对fla ...
- C/C++中的sizeof
代码: #include <iostream> #include <string> using namespace std; int main(){ char s1[]=&qu ...
- c#串口编程时,忽略跨线程检查报错
1.直接在main_Form_Load的初始化中加 //忽略跨线程检查 // System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls ...
- 虚拟机安装centos
linux安装 鉴于国内大多数服务器都使用的redhat系列作为操作系统,centos又是redhat的免费版本,所以可以学习一下.因为另一台古董电脑已经装了linuxmint,虽然也可以安装虚拟机, ...
- php正则验证手机号码
protected function checkphone(){ if(preg_match("/^1[34578]\d{9}$/", $phone)){ return false ...
- ubuntu14.04中解压缩window中的zip文件,文件名乱码的解决方法
在windows上压缩的文件,是以系统默认编码中文来压缩文件.由于zip文件中没有声明其编码,所以linux上的unzip一般以默认编码解压,中文文件名会出现乱码. 通过unzip行命令解压,指定字符 ...