POJ 2524 Ubiquitous Religions
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 20668 | Accepted: 10153 |
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
题目大意:输入n,m代表有n个帮派,下面输入m行x,y,代表学生x,y同一个帮派,问总共有多少个帮派。
解题方法:并查集,每次输入x,y时如果发现他们不同帮派,则n减一,然后合并。
#include <stdio.h> typedef struct
{
int parent;
int rank;
}UFSTree; UFSTree Student[]; void MakeSet(int n)
{
for (int i = ; i <= n; i++)
{
Student[i].parent = i;
Student[i].rank = ;
}
} int FindSet(int x)
{
if (x != Student[x].parent)
{
return FindSet(Student[x].parent);
}
else
{
return x;
}
} void UnionSet(int x, int y)
{
x = FindSet(x);
y = FindSet(y);
if (Student[x].rank > Student[y].rank)
{
Student[y].parent = x;
}
else
{
Student[x].parent = y;
if (Student[x].rank == Student[y].rank)
{
Student[y].rank++;
}
}
} int main()
{
int m, n, x, y, nCase = ;
while(scanf("%d%d", &n, &m) != EOF && n != && m != )
{
MakeSet(n);
for (int i = ; i < m; i++)
{
scanf("%d%d", &x, &y);
if (FindSet(x) != FindSet(y))
{
n--;
UnionSet(x, y);
}
}
printf("Case %d: %d\n", ++nCase, n);
}
return ;
}
POJ 2524 Ubiquitous Religions的更多相关文章
- 【原创】poj ----- 2524 Ubiquitous Religions 解题报告
题目地址: http://poj.org/problem?id=2524 题目内容: Ubiquitous Religions Time Limit: 5000MS Memory Limit: 6 ...
- poj 2524 Ubiquitous Religions(宗教信仰)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 30666 Accepted: ...
- poj 2524:Ubiquitous Religions(并查集,入门题)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23997 Accepted: ...
- POJ 2524 Ubiquitous Religions 解题报告
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34122 Accepted: ...
- [ACM] POJ 2524 Ubiquitous Religions (并查集)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23093 Accepted: ...
- poj 2524 Ubiquitous Religions 一简单并查集
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 22389 Accepted ...
- poj 2524 Ubiquitous Religions(并查集)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23168 Accepted: ...
- POJ 2524 Ubiquitous Religions (幷查集)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23090 Accepted: ...
- poj 2524 Ubiquitous Religions (并查集)
题目:http://poj.org/problem?id=2524 题意:问一个大学里学生的宗教,通过问一个学生可以知道另一个学生是不是跟他信仰同样的宗教.问学校里最多可能有多少个宗教. 也就是给定一 ...
随机推荐
- C#中的ThenBy是如何实现的
C#中给继承自IEnumerable的对象(最熟知的就是List了)提供了很丰富的扩展方法,涉及列表操作的方方面面.而扩展方法ThenBy就是很有意思的一个,它的实现也很巧妙. 如果有这样的一个Tea ...
- String.Format in javascript
有些时候,我们确实需要在JavaScript中进行字符串替换,类似于C#中的String.Format()方法一样,只不过这种格式化替换只局限于对由'{0}','{1}','{2}'...所组成的“占 ...
- iOS开发-状态模式
状态模式允许对象内部状态改变时改变它的行为,对象看起来好像修改了它的类.状态模式看起来和策略模式比较相像,策略模式是将可以互换的行为封装起来,然后通过使用委托的方式,决定使用哪一个行为,状态也是封装行 ...
- Leetcode 110 Balanced Binary Tree 二叉树
判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...
- Android调用浏览器打开网址遇到的问题
我自己的手机(一加一代,升级了氢OS),然后在点击游戏内一个"隐私政策"-- 需要打开一个网页,然后就crash了.出错的信息如下: 完全是看不出来,然后我单独写了一个demo来测 ...
- linux云主机怎么安装WDCP
linux云主机 教你成功安装WDCP的2个方法(第一个不成功就试第2个) 工具/原料 Xshell 云服务器 方法/步骤 1 先用Xshell连接你的服务器 2 输入一下代码 wget ...
- 使用Gulp和Browserify创建多个绑定文件
Browserify是一个Javascript的绑定工具,帮助我们理顺module之间的依赖关系.Gulp用来优化workflow.两者的共同点都是使用流,但在使用流方面也有不同之处: Browser ...
- 诚聘:全栈开发人员,三线城市10-16K
北京快鸽联盟信息技术有限公司成立于2013年,专注于校园及社区快递和增值服务.目前已有十余家各地分部,并与上百所大学,各大快递和电商公司有密切合作,年处理快件量超千万,长期处于行业领先地位. 诚聘全栈 ...
- iOS RSA 证书加密
#import "GLQyRsa.h" #import "GLSupprot.h" #import "GLLoginViewController.h& ...
- Scala 基础入门【翻译】
原文地址 本文只是带你进入 Scala 的世界,包括安装.不可变量 val.可变量 var.定义类.集合(包括列表(list).集(set).映射(map))以及集合遍历和集合库(能达到并行/并发效果 ...