题目:http://poj.org/problem?id=2524

题意:问一个大学里学生的宗教,通过问一个学生可以知道另一个学生是不是跟他信仰同样的宗教。问学校里最多可能有多少个宗教。

也就是给定一个图的点数和相应的边,问有多少个连通分量。

 #include<stdio.h>
#include<iostream>
#include<string.h> using namespace std;
int bin[]; int find(int a)
{
if(bin[a]!=a)
return bin[a]=find(bin[a]);
}; int main()
{
int n,m,i,a,b,sum,x=;
while(cin>>n>>m&&(n||m))
{
sum=n;
for(i=; i<=n; i++)
bin[i]=i; for(i=; i<=m; i++)
{
cin>>a>>b;
if(find(a)!=find(b))
{
sum--;
bin[find(a)]=find(b);
}
} printf("Case %d: %d\n",x,sum);
x++;
}
}

跑时有点多,4600MS 低空掠过

poj 2524 Ubiquitous Religions (并查集)的更多相关文章

  1. [ACM] POJ 2524 Ubiquitous Religions (并查集)

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

  2. POJ 2524 Ubiquitous Religions (幷查集)

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

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

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

  4. poj 2524 Ubiquitous Religions 一简单并查集

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

  5. poj 2524 Ubiquitous Religions(并查集)

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

  6. POJ 2524 Ubiquitous Religions (并查集)

    Description 当今世界有很多不同的宗教,很难通晓他们.你有兴趣找出在你的大学里有多少种不同的宗教信仰.你知道在你的大学里有n个学生(0 < n <= 50000).你无法询问每个 ...

  7. poj 2524 Ubiquitous Religions(简单并查集)

    对与知道并查集的人来说这题太水了,裸的并查集,如果你要给别人讲述并查集可以使用这个题当做例题,代码中我使用了路径压缩,还是有一定优化作用的. #include <stdio.h> #inc ...

  8. 【原创】poj ----- 2524 Ubiquitous Religions 解题报告

    题目地址: http://poj.org/problem?id=2524 题目内容: Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 6 ...

  9. POJ 2524 Ubiquitous Religions

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

随机推荐

  1. python学习之html从0开始(一)

    <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content ...

  2. json数据与字符串相互转化的例子

    json与字符串之间的转换,本文分享一个小例子.   json转成string[需要引用json2.js文件]:  var arr=[{id:'id',name:'Spring'},{id:'id2' ...

  3. lazy instructor

    Description A math instructor is too lazy to grade a question in the exam papers in which students a ...

  4. dive into python 读笔(1)

    chapter2 and 3: 使用Python IDE来交互式地测试表达式 编写Python程序并且从IDE运行,或者从命令行运行 导入模块及调用它们的函数 声明函数以及doc string.局部变 ...

  5. 【BZOJ 1821】 [JSOI2010]Group 部落划分 Group

    Description 聪聪研究发现,荒岛野人总是过着群居的生活,但是,并不是整个荒岛上的所有野人都属于同一个部落,野人们总是拉帮结派形成属于自己的部落,不同的部落之间则经常发生争斗.只是,这一切都成 ...

  6. 【BZOJ2428】[HAOI2006]均分数据

    Description 已知N个正整数:A1.A2.…….An .今要将它们分成M组,使得各组数据的数值和最平均,即各组的均方差最小.均方差公式如下: ,其中σ为均方差,是各组数据和的平均值,xi为第 ...

  7. velocity语法

    1.声明变量 #set($var = XXX) 右边可以是以下的内容 Variable reference String literal Property reference Method refer ...

  8. DateTime.ToString()

       /*            [y]代表年份,注意是小写的y,大写的Y并不代表年份.            [M]表示月份.            [d]表示日期,注意D并不代表什么.       ...

  9. js常识

    btnDelAll.Attributes.Add("onclick", "<script lunguage='javascript'>return windo ...

  10. SQL Server CONVERT() 函数

    http://www.w3school.com.cn/sql/func_convert.asp 定义和用法 CONVERT() 函数是把日期转换为新数据类型的通用函数. CONVERT() 函数可以用 ...