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. wifi diplasy流程介绍

    转自:http://blog.csdn.net/dnfchan/article/details/8558552/  另外一篇不错的参考文章:http://www.360doc.com/content/ ...

  2. Sphinx的介绍和原理探索

    What/Sphinx是什么 定义 Sphinx是一个全文检索引擎. 特性 索引和性能优异 易于集成SQL和XML数据源,并可使用SphinxAPI.SphinxQL或者SphinxSE搜索接口 易于 ...

  3. 如何减少JS的全局变量污染

    A,唯一变量 B,闭包

  4. react.js 多个组件集成示例

    这个看得有点懵, 可能要结合其它实例看. html <!DOCTYPE html> <html> <head> <script src="http: ...

  5. RTP 与RTCP 解释. 含同步时间戳

    转自:http://blog.csdn.net/wudebao5220150/article/details/13816225 RTP协议是real-time transport protocol的缩 ...

  6. ok6410按键中断编程,linux按键裸机

    6410按键中断编程 一.流程分析 外部中断控制寄存器(s3c6410x  359页) 1.EINTxCONy: 外部中断组x的第y个控制器.这个就是设置中断的触发方式.有5种触发方式. 2.EINT ...

  7. 在ubuntu中安装jdk

    安装环境 操作系统:ubuntu 14.04.1 server amd64 下载jdk wget http://download.oracle.com/otn-pub/java/jdk/7u67-b0 ...

  8. Linux2.6 内核的 Initrd 机制解析

    文章来自:www.ibm.com/developerworks/cn/linux/l-k26initrd/ 1.什么是 Initrd initrd 的英文含义是 boot loader initial ...

  9. SQL Server 创建表 添加主键 添加列常用SQL语句

    --删除主键 alter table 表名 drop constraint 主键名 --添加主键 alter table 表名 add constraint 主键名 primary key(字段名1, ...

  10. 在 SQL Server 中查询EXCEL 表中的数据遇到的各种问题

    SELECT * FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0','Data Source="D:\KK.xls";User ID=A ...