poj 2524 Ubiquitous Religions (并查集)
题目: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 (并查集)的更多相关文章
- [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: 23090 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: 22389 Accepted ...
- poj 2524 Ubiquitous Religions(并查集)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23168 Accepted: ...
- POJ 2524 Ubiquitous Religions (并查集)
Description 当今世界有很多不同的宗教,很难通晓他们.你有兴趣找出在你的大学里有多少种不同的宗教信仰.你知道在你的大学里有n个学生(0 < n <= 50000).你无法询问每个 ...
- poj 2524 Ubiquitous Religions(简单并查集)
对与知道并查集的人来说这题太水了,裸的并查集,如果你要给别人讲述并查集可以使用这个题当做例题,代码中我使用了路径压缩,还是有一定优化作用的. #include <stdio.h> #inc ...
- 【原创】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: 20668 Accepted: ...
随机推荐
- asp:HyperLink vs asp:LinkButton
asp:HyperLink NavigateUrl - 页面刷新为新的页面 pageload ispostback = false asp:LinkButton PostbackUrl - postb ...
- PHP学习之环境搭建
计算机环境 win7 64位 搭建 apache-httpd-2.2-win64 + php-5.3.6-Win32-VC9-x64 +MySQL_5.5.13_winx64开发环境 参考: ...
- corsproxy
安装完 node.js运行 npm install -g corsproxy安装完成 corsproxy
- sublime text2 中文乱码的解决办法
1.安装Sublime Package Control 在Sublime Text 2上用Ctrl+-打开控制台并在里面输入以下代码,Sublime Text 2就会自动安装Package Contr ...
- spot 5、ALOS监督分类波段组成
spot 5监督分类RGB:412 ALOS分类波段RGB:432
- php生成xml的四种方法(转)
<?xml version="1.0" encoding="utf-8"?> <article> <item> <ti ...
- Ext.Ajax.request()方法和FormPanel.getForm().submit()方法,都返回success()方法的差异
我还是不发表到博客园首页吧,要不然还是要被取消,>_< 还是言归正传吧,关于Ext.Ajax.request()方法和FormPanel.getForm().submit()方法返回suc ...
- XSD - <schema> 元素
<schema> 元素 <schema> 元素是每一个 XML Schema 的根元素: <?xml version="1.0"?> <x ...
- eclipse中切换jre后报错:Java compiler level does not match the version of the installed Java project facet.
项目移除原来的jre环境lib后,添加本地的jre,报错如下: Java compiler level does not match the version of the installed Java ...
- easyui 使特定tab处于选中状态
<div id="tabs" class="easyui-tabs"> <div title="Tab1" style=& ...