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 题意:问一个大学里学生的宗教,通过问一个学生可以知道另一个学生是不是跟他信仰同样的宗教.问学校里最多可能有多少个宗教. 也就是给定一 ...
随机推荐
- Struts2学习笔记-jsp中引用struts2框架
如果在jsp中需要引用struts2 框架,需在前面加上以下内容 <%@taglib prefix="s" uri="/struts-tags" %> ...
- Socket通信实例(C#)
SOCKET原理 一.套接字(socket)概念 套接字(socket)是通信的基石,是支持TCP/IP协议的网络通信的基本操作单元.它是网络通信过程中端点的抽象表示,包含进行网络通信必须的五种信息: ...
- struts2 iterator排序
因为存入数据库的数据可能不是按照我们想要的方式存进去的.这就导致取出来的时候,不是按照我们想要的方式排序.这时候就要利用struts2的iterator排序功能,按照我们想要的顺序排列. 首先.一个付 ...
- Android 5.0/5.1开发问题专贴
注:非5.0特定的开发问题,可以在这个帖子里查:Android开发问题汇总. 1.官方提供的例子android-support-v7-appcompat编译时提示android:actionModeS ...
- 如何通过linux ssh远程linux不用输入密码登入
如何通过一台linux ssh远程其他linux服务器时,不要输入密码,可以自动登入.提高远程效率,不用记忆各台服务器的密码. 工具/原料 ssh,ssh-keygen,scp 方法/步骤 首 ...
- [算法导论]红黑树实现(插入和删除) @ Python
class RBTree: def __init__(self): self.nil = RBTreeNode(0) self.root = self.nil class RBTreeNode: de ...
- 【C/C++】随机数问题
最初问题:从n个数中随机选择m个数(0<=m<=n). 为了便于描述,可以将该问题抽象为:从0-n-1这n个数中随机选择m个数.计算机能够提供的随机数都是伪随机的,我们假设计算机提供的伪随 ...
- [2013 eoe移动开发者大会]靳岩:从码农到极客的升级之路
(国内知名Android开发论坛 eoe开发者社区推荐:http://www.eoeandroid.com/) 前天,2013 eoe 移动开发者大会在国家会议中心召开,eoe 开发者社区创始人靳岩在 ...
- 如何在IIS7下配置ASP+ACCESS环境
如何在IIS7下配置ASP+ACCESS环境 | 浏览:901 | 更新:2013-01-16 17:46 1 2 3 4 5 6 7 分步阅读 默认装完IIS7之后,使用ASP程序会发现提示数据库连 ...
- "无法启动应用程序,工作组信息文件丢失,或是已被其他用户已独占方式打开"解决办法
当程序用ado的jet4.0方式连接mdb数据库的时候,对于设有access数据库密码的mdb的访问居然报错“无法启动应用程序,工作组信息文件丢失,或是已被其他用户已独占方式打开”. ADOConne ...