POJ - Ubiquitous Religions
Description
Input
Output
Sample Input
10 9
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
10 4
2 3
4 5
4 8
5 8
0 0
Sample Output
Case 1: 1
Case 2: 7
Hint
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<algorithm>
using namespace std; const int MX = 55555;
int stu[MX];
int n, m; void ini() {
for (int i = 1; i <= n; i++) {
stu[i] = i;
}
} int FindRoot(int pos) {
return stu[pos] == pos ? pos : (stu[pos] = FindRoot(stu[pos]));
} int main() {
//freopen("input.txt", "r", stdin);
int sign = 1;
while (scanf("%d %d", &n, &m), n || m) {
ini();
int a, b;
for (int i = 0; i < m; i++) {
scanf("%d %d", &a, &b);
int root1 = FindRoot(a);
int root2 = FindRoot(b);
if (root1 != root2) {
n--;//正是为了避免由于没有更新节点的问题,才用这种办法来求最后还有几个集合,合并一次集合就少一个
stu[root2] = root1;
}
}
printf("Case %d: %d\n", sign++, n);
}
return 0;
}
POJ - Ubiquitous Religions的更多相关文章
- 【原创】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: 23997 Accepted: ...
- POJ 2524 Ubiquitous Religions
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 20668 Accepted: ...
- Ubiquitous Religions 分类: POJ 2015-06-16 17:13 11人阅读 评论(0) 收藏
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 26678 Accepted: ...
- 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: 34122 Accepted: ...
- [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: 22389 Accepted ...
随机推荐
- 与你相遇好幸运,Sail.js新建模型控制器
sails generate api user 创建了user的controller和models sails generate api user index 创建了user的controller和 ...
- HTML5学习之视频与音频(三)
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- [Oracle] PL/SQL学习笔记
-- 1. 使用一个变量 declare -- Local variables here v_name ); begin -- Test statements here select t.user_n ...
- 使用python做科学计算
这里总结一个guide,主要针对刚开始做数据挖掘和数据分析的同学 说道统计分析工具你一定想到像excel,spss,sas,matlab以及R语言.R语言是这里面比较火的,它的强项是强大的绘图功能以及 ...
- Linux crontab 定时任务详解
1.每小时执行一次脚本 * */1 * * * /etc/init.d/smb restart #不是所有的系统都支持“*/1”这种写法可以试试: 0 * * * * /etc/init.d/smb ...
- ASP.NET MVC使用过滤器进行权限控制
1.新建MVC项目 2.找到Models文件夹,新建 LoginCheckFilterAttribute 类 public class LoginCheckFilterAttribute : Acti ...
- string、math、random、datetime类
1.string类 变量.Replace("想要替换掉的字符或字符串","转换后的字符或字符串");//替换 练习:判断邮箱格式是否正确 ...
- Chrome浏览器之 Postman 安装
Postman 是一款发送 HTTP 请求的 Chrome 插件.开发后端程序的同学可以用它来测试自己写的应用程序是否能够正常访问. 现在由于国内的网络限制, Chrome 浏览器里无法访问“扩展程序 ...
- Android:res之shape制作圆角、虚线、渐变
xml控件配置属性 android:background="@drawable/shape" 标签 corners ----------圆角gradient ----------渐 ...
- background为圆角的表框,dp转Px,Px转dp
圆角边框<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="ht ...