poj 2524 并查集 Ubiquitous Religions
//#include<bits/stdc++.h>
#include<iostream>
#include<stdio.h>
#define max1 50005
using namespace std;
int pa[max1],vis[max1];
int find(int a)
{
while(a!=pa[a])
{
pa[a]=pa[pa[a]];
a=pa[a];
}
return a;
}
void build(int a,int b)
{
int fa=find(a);
int fb=find(b);
if(fa!=fb)
{
if(fa<fb)
pa[fb]=fa;
else
pa[fa]=fb;
}
}
int main()
{
int n,m;
int t=;
while((scanf("%d%d",&n,&m))&&(n+m!=))
{
int a,b;
for(int i=;i<=n;i++)
{
pa[i]=i;
vis[i]=;
}
for(int i=;i<m;i++)
{
cin>>a>>b;
build(a,b);
}
int ans=;
for(int i=;i<=n;i++)
{
if(!vis[find(i)])
{
ans++;
vis[find(i)]=;
}
}
printf("Case %d: %d\n",t++,ans);
}
return ;
}
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 30197 | Accepted: 14605 |
Description
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
Output
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
poj 2524 并查集 Ubiquitous Religions的更多相关文章
- POJ 2524 (简单并查集) Ubiquitous Religions
题意:有编号为1到n的学生,然后有m组调查,每组调查中有a和b,表示该两个学生有同样的宗教信仰,问最多有多少种不同的宗教信仰 简单并查集 //#define LOCAL #include <io ...
- POJ 2524 并查集
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23580 Accepted: 1160 ...
- 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找树上最长直径了,并且对树上最长直径 ...
随机推荐
- 错误 1 未知的服务器标记“asp:ScriptManager”。
如题 ... 解决方案 :将 <%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=ne ...
- Hadoop源码之Configuration
本文hadoop版本为最新版本2.6.Configuration做为Hadoop的一个基础功能承担着重要的责任,为Yarn.HSFS.MapReduce.NFS.调度器等提供参数的配置.配置文件的分布 ...
- dedecms如何随机调用指定分类下的文章到网站首页
dedecms是全静态的,有时会因为其他事情好几天没写文章推荐到首页,那样对se不是很友好.原本ytkah是想在网站首页上半部分调用几篇id从200到500的文章随机展示的,这样每次更新首页给se的赶 ...
- 最长不下降子序列 O(nlogn) || 记忆化搜索
#include<stdio.h> ] , temp[] ; int n , top ; int binary_search (int x) { ; int last = top ; in ...
- html tr td colspan
colspan 属性规定单元格可横跨的列数, 第一行的colspan规定其一行所跨越的列数,要与下一行的<td></td>个数一致 if(!empty ($alarmDesc ...
- 在Android的webview中定做js的alert,confirm和prompt对话框的方法
在Android的webview中定制js的alert,confirm和prompt对话框的方法 http://618119.com/archives/2010/12/20/199.html 1.首先 ...
- Unity3d 去掉exe版本的边框
原地址:http://blog.sina.com.cn/s/blog_697b1b8c0101gd4h.html using System; using System.Runtime.InteropS ...
- ios socket(基础demo)
http://blog.sina.com.cn/s/blog_7a2f0a830101ecv4.html clinetSocket 1.viewcontroller.h @interface View ...
- HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑
这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<al ...
- 【OpenStack】OpenStack系列6之Sheepdog环境搭建
准备 repo配置 yum clean all yum makecache yum install -y make automake autoconf gcc nss-devel wget git g ...