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》——第3章:栈和队列——题目5
2014-03-18 05:33 题目:用两个栈来实现一个队列. 解法:栈是反的,队列是正的,反了再反就正过来了.所以,请看代码.操作中时间复杂度有O(1)的,有O(n)的,但均摊下来时间符合O(1) ...
- leetcode 【 Linked List Swap Nodes in Pairs 】 python 实现
题目: Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- ffifdyop
题目地址:http://www.shiyanbar.com/ctf/2036 后台登陆 上来看到这个界面,果断先看一波源代码. 看到是拼接字符串进行sql查询,就想到了注入了. 但是很不幸的是md5( ...
- jmeter 运行脚本报错 java.net.BindException: Address already in use
在win下跑jmeter时,在聚合报告中出现错误.打开日志文件(前提是将日志写入了指定文件) 发现报错的原因为:java.net.BindException: Address already in u ...
- Python 3基础教程9-函数
本文介绍Python中的函数,主要了解如何定义一个函数,如何调用一个函数. 如果上面你不写调用函数这行代码,你运行后,是没有打印输出的.我们这里来,结合前面的if语句来定义一个,两个数比较,判断最大的 ...
- jwt手动生成access_token
from rest_framework_jwt.settings import api_settings # 手动为用户生成tokenjwt_payload_handler = api_setting ...
- Scrapy 学习笔记(一)数据提取
Scrapy 中常用的数据提取方式有三种:Css 选择器.XPath.正则表达式. Css 选择器 Web 中的 Css 选择器,本来是用于实现在特定 DOM 元素上应用花括号内的样式这样一个功能的. ...
- vmware设置静态ip(复制)
一.安装好虚拟后在菜单栏选择编辑→ 虚拟网络编辑器,打开虚拟网络编辑器对话框,选择Vmnet8 Net网络连接方式,随意设置子网IP,点击NAT设置页面,查看子网掩码和网关,后面修改静态IP会用到. ...
- Mysql入门的10条语句
1.查看当前服务器下面,有那些库(database)? 答: show databases; 2.如何创建一个数据库? 答 : create database 最后面在写数据库名:分号结束 3.选择数 ...
- Android使用adb命令查看CPU信息
Android中使用JNI编程的时候会需要编译出不同的SO文件,以供适配不同的机型. 例如: 由此需要查看不同机型的CPU信息. 使用ADB命令查看CPU信息命令如下: 1. adb shell 2. ...