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找树上最长直径了,并且对树上最长直径 ...
随机推荐
- java web dao 层和dao 实现层有什么好处
接口是为了屏蔽不同数据库的JDBC实现差异而出现的. 以常见的系统组织结构为例,管理系统里有一个部门department表用于存放部门信息,因为部门下面可能有子部门所以表中的字段要定义成类似树形结构的 ...
- [Effective JavaScript 笔记]第5章:数组和字典--个人总结
前言 这节里其实一直都在讨论对象这个在js中的万能的数据结构.对象可以表式为多种的形式,表示为字典和数组之间的区别.更多的我觉得这章讨论多的是一些对应实现功能的相关操作,有可能出现的bug以及如何避免 ...
- ios5之后arc的问题
原创: 自从ios5以后, apple官方引进了ARC (automaically reference count 自动索引计数) 这个新特性, 简单的说就是对象设置为nil(空引用)之后, 当自动索 ...
- DrawText
该函数在指定的矩形里写入格式化的正文,根据指定的方法对正文格式化(扩展的制表符,字符对齐.折行等). int DrawText(HDC hDC, // 设备描述表句柄 LPCTSTR lpStri ...
- 关于DCMTK3.6.0源代码编译的总结
1.DCMTK cmake出来的代码是一样的.MT和MD版本的区别在于DCMTK工程下的每个子工程的代码生成中的MT还是MD,只要修改成为相应的值就可以了. 2.依赖包的选择.依赖包必须与上面中所说的 ...
- nyoj 20
http://acm.nyist.net/JudgeOnline/message.php?msg=已提交&url=status.php%3Fpid%3D20&second=0 #inc ...
- Android Intent不可传递大数据
今天用intent传递一个bitmap,结果一直出错,intent无法执行,原来是intent不能传递大数据导致的,具体是多大,不太清楚,但我传递的bitmap在1m以上.
- zpf 命名规则
2014年8月19日 18:48:39 所有控制器都要继承main类,main类是一个入口类,他里边根据请求初始化了一些变量,也初始化了一些系统变量等等,这些变量和函数可以被控制器类直接使用 控制器类 ...
- 2.django笔记之缓存,session,cookie,ajax
一.django缓存 1.缓存 缓存的意义在于把昂贵的计算结果保存起来一遍下次的访问,有缓存的站点的流程大概是这样子的: 给定一个url,检查页面是否在缓存中 如果在,返回缓存的页面 否则,生成该页面 ...
- 支持向量机(SVM)简介
主要内容 一:SVM简介 二:线性分类 三:分类间隔 四:核函数 五:松弛变量 SVM简介 支持向量机(support vector Machine)是由Cortes和Vapnik于1995年首先提出 ...