C - Ubiquitous Religions

Time Limit: 5000 MS Memory Limit: 65536 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

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.
 
并查集模板题简单变新,前边这种是完全没有优化的,所以时间巨长!!      错了4发,PE,这个—_—!
//3469 ms 544 KB C++ 1295 B
#include <cstdio>
using namespace std;
const int maxn = 50005;
int father[maxn];
int mark[maxn]; void init(int n)
{
for(int i = 1; i <= n; ++i)
{
father[i] = i;
mark[i] = 0;
}
} int serch(int x)
{
if(father[x] == x)
return x;
return father[x] = serch(father[x]);
} void join(int x, int y)
{
int fx = serch(x), fy = serch(y);
if(fx != fy)
father[fx] = fy;
} int main()
{
int n, m, a, b;
int casee = 0;
while(scanf("%d %d", &n, &m)!= EOF && (n || m))
{
casee++;
init(n);
for(int i = 1; i <= m; ++i)
{
scanf("%d %d", &a, &b);
join(a, b);
} int res = 0;
for(int i = 1; i <= n; ++i)
{
if(!mark[i])
{
mark[i] = 5;
for(int j = 1; j <= n; ++j)
{
if(!mark[j] && (serch(i) == serch(j)))
{
mark[j] = 1;
}
}
}
} for(int i = 1; i <= n; ++i)
{
if(mark[i] == 5)
res++;
} printf("Case %d: %d\n", casee, res);
}
return 0;
}

  

 采用路径压缩后能快点,路径压缩只需改一句话。当然还有其他优化,还在学习中。。。
 
///Accepted	3469 ms	544 KB	C++	1295 B
int serch(int x)
{
if(father[x] == x)
return x;
return father[x] = serch(father[x]);   ///以前为 return serch(father[x]); 
}

  

poj2524-Ubiquitous Religions的更多相关文章

  1. POJ2524——Ubiquitous Religions

    Ubiquitous Religions Description There are so many different religions in the world today that it is ...

  2. poj2524 Ubiquitous Religions(并查集)

    题目链接 http://poj.org/problem?id=2524 题意 有n个学生,编号1~n,每个学生最多有1个宗教信仰,输入m组数据,每组数据包含a.b,表示同学a和同学b有相同的信仰,求在 ...

  3. poj-2524 ubiquitous religions(并查集)

    Time limit5000 ms Memory limit65536 kB There are so many different religions in the world today that ...

  4. poj-2236 Wireless Network &&poj-1611 The Suspects && poj-2524 Ubiquitous Religions (基础并查集)

    http://poj.org/problem?id=2236 由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由 ...

  5. POJ2524 Ubiquitous Religions(并查集)

    题目链接. 分析: 给定 n 个点和 m 条无项边,求连通分量的数量.用并查集很简单. #include <iostream> #include <cstdio> #inclu ...

  6. poj 2524:Ubiquitous Religions(并查集,入门题)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23997   Accepted:  ...

  7. POJ 2524 Ubiquitous Religions

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 20668   Accepted:  ...

  8. Ubiquitous Religions 分类: POJ 2015-06-16 17:13 11人阅读 评论(0) 收藏

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 26678   Accepted: ...

  9. poj 2524 Ubiquitous Religions(宗教信仰)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 30666   Accepted: ...

  10. POJ 2524 :Ubiquitous Religions

    id=2524">Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 231 ...

随机推荐

  1. Mac下java开发环境的搭建与开发工具的安装

    一.安装JDK 1.根据你当前环境的需要,下载相应的JDK并安装,安装步骤与其他Mac软件安装方法相同,我安装的是jdk1.8.0_74.jdk,mac中jdk1.8的默认位置:/Library/Ja ...

  2. bootstrap垂直下拉菜单默认展开

    HTML: <div class="col-md-3"> <nav class="navbar"> <div class=&quo ...

  3. NYOJ题目273字母小游戏

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAswAAAIZCAIAAAA9QP7RAAAgAElEQVR4nO3dPXKjzBqG4bMJ5VqIU2

  4. redis 常用命令

    查看redis信息和状态: > info redis下,数据库是由一个整数索引标识,而不是由一个数据库名称.默认情况下,一个客户端连接到数据库0.redis配置文件中下面的参数来控制数据库总数: ...

  5. 《精通Hibernate:Java对象持久化技术详解》目录

    图书信息:孙卫琴 电子工业出版社 第1章 Java应用分层架构及软件模型: 1.1 应用程序的分层体系结构 1.1.1 区分物理层和逻辑层 1.1.2 软件层的特征 1.1.3 软件分层的优点 1.1 ...

  6. jQuery easyUI datagrid 增加求和统计行 分类: JavaScript 2015-01-14 17:46 2178人阅读 评论(0) 收藏

    在datagrid的onLoadSuccess事件增加代码处理. <style type="text/css"> .subtotal { font-weight: bo ...

  7. BOOL in Object-C

    BOOL in Object-C typedef signed char BOOL; #define YES (BOOL)1 #define NO (BOOL)0 重构上页中的代码 - (BOOL)s ...

  8. angularjs 菜鸟教程 版本1.4.6

    最简angularJS webstorm代码提示 在 head 里面添加:<script src="https://ajax.googleapis.com/ajax/libs/angu ...

  9. hdu 5291 dp+优化 ****

    多校实在高能 题解链接 题意:有n中糖果,每种糖果有ai个.分给A,B两个人.两人的糖果要一样多,可以都是0,1......m个.同一种糖果没有区别. 问有几种分法. 定义dp[i]表示两人之间相差i ...

  10. 1:A+B Problem

    总时间限制:  1000ms  内存限制:  65536kB 描述 Calculate a + b 输入 Two integer a,,b (0 ≤ a,b ≤ 10) 输出 Output a + b ...