POJ 2524
并查集思想,初始化每个元素的根节点为本身。
求解目标是求解存在几个集合。解决方案:查看有多少个根节点,表现在记忆数组上就是有多少个元素的根是它本身。
#include<stdio.h>
#define M 50005
int ji[M];
int findme(int a)
{
while(ji[a]!=a)
{
a=ji[a];
}
return a;
}
void link(int a,int b)
{
int tmp;
tmp=b;
a=findme(a);
b=findme(b);
ji[b]=a;
while(ji[tmp]!=tmp)
{
tmp=ji[tmp];
ji[tmp]=a;
}
}
int main()
{
int m,a,b,n,i,rel,ca;
ca=;
scanf("%d%d",&n,&m);
while(n!=)
{
ca++;
for(i=;i<=n;i++)
{
ji[i]=i;
}
rel=;
while(m--)
{
scanf("%d%d",&a,&b);
link(a,b);
}
for(i=;i<=n;i++)
{
rel+=(ji[i]==i);
}
printf("Case %d: %d\n",ca,rel);
scanf("%d%d",&n,&m);
}
return ;
}
POJ 2524的更多相关文章
- poj 2524 (并查集)
http://poj.org/problem?id=2524 题意:在一所学校里面的人,都有宗教信仰,不过他们的宗教信仰有可能相同有可能不同,但你又不能直接去问他们,但你可以问他们和谁是同一个宗教.通 ...
- poj 2524 Ubiquitous Religions (并查集)
题目:http://poj.org/problem?id=2524 题意:问一个大学里学生的宗教,通过问一个学生可以知道另一个学生是不是跟他信仰同样的宗教.问学校里最多可能有多少个宗教. 也就是给定一 ...
- 【原创】poj ----- 2524 Ubiquitous Religions 解题报告
题目地址: http://poj.org/problem?id=2524 题目内容: Ubiquitous Religions Time Limit: 5000MS Memory Limit: 6 ...
- POJ 2524 独一无二的宗教(裸并查集)
题目链接: http://poj.org/problem?id=2524 Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K ...
- poj 2524 Ubiquitous Religions(宗教信仰)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 30666 Accepted: ...
- POJ 2524 :Ubiquitous Religions
id=2524">Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 231 ...
- 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: 20668 Accepted: ...
- poj 2524 并查集 Ubiquitous Religions
//#include<bits/stdc++.h> #include<iostream> #include<stdio.h> #define max1 50005 ...
随机推荐
- hydra
转:http://www.cnblogs.com/patf/p/3142564.html 1.yum -y install openssl-devel pcre-devel ncpfs-devel p ...
- webpack需要全局安装,才能使用webpack命令
webpack全局安装,具体项目中才能使用webpack命令: npm install webpack -g
- 关于SQLServer2005的学习笔记—异常捕获及处理
转自:http://blog.csdn.net/baoqiangwang/article/details/5395874 SQLServer2005 提供了类似于 C# 和 C++ 语言中的异常处理的 ...
- httpClenit的post出现乱码问题
在使用httpClient.executeMethod(postMethod)的时候,发现一直存在乱码问题,”book is good“被转成”book+is+good“ 返回.查看源码后,发现pos ...
- Python try/except异常处理机制
1. use try, except, finally try: data=open('its.txt','w') print('its..', file=data) except: print('f ...
- java.io.FileNotFoundException:SESSIONS.ser (系统找不到指定的路径。)
问题如下: java.io.FileNotFoundException: E:\apache-tomcat-8.0.37\work\Catalina\localhost\20161013Shoppin ...
- usb由于其配置信息(注册表中的)不完整或已损坏,Windows 无法启动这个硬件设备
在设备管理器的usb设备的属性中,显示提示“由于其配置信息(注册表中的)不完整或已损坏,Windows 无法启动这个硬件设备”.注册表坏了.经过查询,解决方法如下: 方法:打开注册表编辑器(开始--& ...
- (WPF, MVVM) Textbox Binding
参考:http://msdn.microsoft.com/en-us/library/system.windows.data.updatesourcetrigger(v=vs.110).aspx Te ...
- The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved
偶在页面里引入了标签如下: <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core&q ...
- SOA_环境安装系列2_Oracle RCU安装和环境搭建(案例)
2015-01-02 Created By BaoXinjian