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: ...
随机推荐
- 跟着学!教你开发第一个Java程序
今天我们的目标是开发人生中的第一个Java程序,虽然可能会很简单,但是这小小的一步却是跨入IT行业的一大步!下面我们来一起来仔细的了解开发的流程. 准备工作 1,作为一名准程序猿自备一台电脑那是必不可 ...
- php 值传递和引用传递的区别
值传递:函数范围内对值的任何改变在函数外部都会被忽略 引用传递:函数范围内对值的任何改变在函数外部也能反映出这些修改 A:按值传递时,php必须复制值.特别是对于大型的字符串和对象来说,这将会是一个代 ...
- 雅礼集训 Day7 T1 Equation 解题报告
Reverse 题目背景 小\(\text{G}\)有一个长度为\(n\)的\(01\)串\(T\),其中只有\(T_S=1\),其余位置都是\(0\).现在小\(\text{G}\)可以进行若干次以 ...
- sql或oracle插入数据时进行md5加密
1.sql简单直接调用: SELECT hashbytes(') ; 2.oracle要复杂些 首写需要建函数: CREATE OR REPLACE FUNCTION MD5( passwd IN V ...
- html li css选中状态切换
思路:点击当前li元素后是用removeClass()删除所有兄弟元素(使用siblings()获取)的class样式,然后使用addClass()为当前li添加class. 具体演示如下: 1.HT ...
- Yii2.0 使用createcommand从数据库查询出来的int类型变成了string型
在给客户端写接口文档时,会给每个返回的字段标上数据类型,客户端会根据标注的类型来解析数据,如果标注的类型错误,会导致客户端解析出错,造成崩溃. 一直以来我都是用的Yii进行项目的开发,之前使用Yii1 ...
- mac使用基础
Mac 系统的桌面 Mac 的桌面是一个很炫的3D, 背景是一张“星空”图. 2 Dock: 在桌面的下方有一排图标, 叫Dock, 用来快速启动程序, 进入文件夹, 它同时还可以停靠正在运行的程序 ...
- [bzoj 2216] [Poi2011] Lightning Conductor
[bzoj 2216] [Poi2011] Lightning Conductor Description 已知一个长度为n的序列a1,a2,-,an. 对于每个1<=i<=n,找到最小的 ...
- Scaffold your ASP.NET MVC 3 project with the MvcScaffolding package
原文发布时间为:2011-05-21 -- 来源于本人的百度文章 [由搬家工具导入] http://blog.stevensanderson.com/2011/01/13/scaffold-your- ...
- 关于tbody js取法兼容。
本来jquery就是为了兼容才用的,动态添加表格的时候,ie必须要append 到tbody上,不然无法取上. 而chrome等则可以,最终还是选appendto tobody