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: ...
随机推荐
- 《Cracking the Coding Interview》——第5章:位操作——题目5
2014-03-19 06:22 题目:将整数A变成整数B,每次只能变一个二进制位,要变多少次呢. 解法:异或,然后求‘1’的个数. 代码: // 5.5 Determine the number o ...
- 《Cracking the Coding Interview》——第1章:数组和字符串——题目1
2014-03-18 01:25 题目:给定一个字符串,判断其中是否有重复字母. 解法:对于可能有n种字符的字符集,用一个长度为n的数组统计每个字符的出现次数,大于1则表示有重复. 代码: // 1. ...
- 【 Logistic Regression 】林轩田机器学习基石
这里提出Logistic Regression的角度是Soft Binary Classification.输出限定在0~1之间,用于表示可能发生positive的概率. 具体的做法是在Linear ...
- js 请求异常重连或断线后联网重连机制(ajax)
转到到 https://blog.csdn.net/mengtoumingren/article/details/80296788
- 给vmstat加上时间戳
vmstat -n 5 | awk '{print strftime("[%Y-%m-%d %H:%M:%S]"),$0}' 或者 vmstat -n 5 | awk '{ pri ...
- django QuerySet 的常用API
为了加深对queryset对象api的了解,我们建立了以下示例模型: from django.db import models class Author(models.Model): "&q ...
- HDU 3033 组合背包变形 I love sneakers!
I love sneakers! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- 利用VS2013 XSLT对 XML进行转换
1.打开VS2013 2.文件-->新建-->文件-->XML文件 3.文件-->新建-->文件-->XSLT文件 4.CTRL+SHIFT+S 保存2个文件位置 ...
- CLion 的 Debug 模式是怎么回事
我对这个问题产生兴趣的起因是 Codeoforces Avito Challenges 2018 的 E 题,我想到了正解,但写得太慢,最后一刻才提交.有个地方写错,结果是 Runtime error ...
- NOIP2017赛前模拟11月2日总结
分数爆炸的一天··但也学了很多 题目1:活动安排 给定n个活动的开始时间与结束时间··只有一个场地··要求保留尽量多的活动且时间不冲突···场地数n<=100000 考点:贪心 直接将结束时间按 ...