POJ 2524 Ubiquitous Religions (并查集)
Description
Input
Output
解题思路:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <algorithm>
using namespace std;
const int MAXN = 1e5 + ;
int pre[MAXN];
int a[MAXN]={};
int Find(int x)
{
int r = x;
while(pre[r] != r)
{
r = pre[r];
}
int i = x,j;
while(pre[i] != r)
{
j = i;
i = pre[i];
pre[j] = r;
}
return r;
} void Mix(int a,int b)
{
int x = Find(a);
int y = Find(b);
if(x > y)
{
pre[x] = y;
}
if(x < y)
{
pre[y] = x;
}
} void Mst(int n)
{
for(int i = ; i <= n; i++)
{
pre[i] = i;
}
} int main()
{
//freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int m,n;
int kas = ;
while(~scanf("%d%d",&n,&m)&&(n||m))
{
Mst(n);
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
Mix(a,b);
}
int ans = ;
for(int i = ; i <= n; i++)
if(pre[i] == i)
ans++;
printf("Case %d: ",++kas);
cout << ans <<endl;
}
return ;
}
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 (并查集)
题目:http://poj.org/problem?id=2524 题意:问一个大学里学生的宗教,通过问一个学生可以知道另一个学生是不是跟他信仰同样的宗教.问学校里最多可能有多少个宗教. 也就是给定一 ...
- 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(简单并查集)
对与知道并查集的人来说这题太水了,裸的并查集,如果你要给别人讲述并查集可以使用这个题当做例题,代码中我使用了路径压缩,还是有一定优化作用的. #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: ...
随机推荐
- js常用框架
JS常用框架:jQuery.Prototype.MooTools 参考:w3cshool jQuery jQuery 是目前最受欢迎的 JavaScript 框架. 它使用 CSS 选择器来访问和操作 ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目10
2014-04-25 20:47 题目:分配一个二维数组,尽量减少malloc和free的使用次数,要求能用a[i][j]的方式访问数据. 解法:有篇文章讲了六种new delete二维数组的方式,其 ...
- 《Cracking the Coding Interview》——第3章:栈和队列——题目7
2014-03-19 03:20 题目:实现一个包含阿猫阿狗的先入先出队列,我要猫就给我一只来的最早的猫,要狗就给我一只来的最早的狗,随便要就给我一只来的最早的宠物.建议用链表实现. 解法:单链表可以 ...
- 【Linear Regression】林轩田机器学习基石
这一节开始讲基础的Linear Regression算法. (1)Linear Regression的假设空间变成了实数域 (2)Linear Regression的目标是找到使得残差更小的分割线(超 ...
- python - web自动化测试 - 元素操作 - 定位
# -*- coding:utf-8 -*- ''' @project: web学习 @author: Jimmy @file: find_ele.py @ide: PyCharm Community ...
- [译]17-spring基于java代码的配置元数据
spring还支持基于java代码的配置元数据.不过这种方式不太常用,但是还有一些人使用.所以还是很有必要介绍一下. spring基于java代码的配置元数据,可以通过@Configuration注解 ...
- 浅谈==和equals的区别
java中的==和equals的区别? 看上面的代码,输出的结果是: true false true true 1.为什么会有上面的区别呢?==和equals比较的到底是什么呢? 搞清楚两者的区别其实 ...
- CentOS7 设置开机直接进入命令行界面
上网查询centsos设置开机直接进入命令行界面的方法都说修改/etc/inittab文件,将文件中的“ :id:5:initdefault:”改为“ :id:3:initdefault:”,即将默认 ...
- KVC 开发详情
目录 概述 KVC基本技术 KVC访问函数 KVC搜索顺序 KVC集合操作 一.概述 kvc全名是Key-value coding,kvc是一种通过字符串间接的访问oc对象的属性的一种技术. 一个oc ...
- 【bzoj2732】[HNOI2012]射箭 二分+半平面交
题目描述 给出二维平面上n个与y轴平行的线段,求最大的k,使得存在一条形如$y=ax^2+bx(a<0,b>0)$的抛物线与前k条线段均有公共点 输入 输入文件第一行是一个正整数N,表示一 ...