[poj2524]Ubiquitous Religions
来源:互联网 发布:汕头峡山淘宝拍摄 编辑:程序博客网 时间:2024/06/06 01:43
题目←
裸的并查集,比较在意老师的扩展:如果包括两个同学不信仰一种宗教的关系,好像要用到hash?
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;const int MAXN = 100000 + 50;int fa[MAXN],n,m,a,b;void init(int n){ for(int i = 1;i <= n;i ++) fa[i] = i;}int find(int x){ return fa[x] == x ? x : fa[x] = find(fa[x]);}void merge(int a,int b){ a = find(a); b = find(b); fa[b] = a;}int cnt;int main(){ while(true) { cnt ++; scanf("%d%d",&n,&m); if(!n && !m) { return 0; } init(n); for(int i = 1;i <= m;i ++) { scanf("%d%d",&a,&b); merge(a,b); } int temp = n; for(int i = 1;i <= n;i ++) { int t = find(i); if(t != i)temp --; } printf("Case %d: %d\n",cnt,temp); } return 0;}
阅读全文
0 0
- poj2524 Ubiquitous Religions
- Ubiquitous Religions POJ2524
- POJ2524 Ubiquitous Religions
- POJ2524:Ubiquitous Religions
- poj2524 Ubiquitous Religions
- poj2524 Ubiquitous Religions
- poj2524 Ubiquitous Religions
- POJ2524 Ubiquitous Religions
- poj2524 Ubiquitous Religions
- poj2524--Ubiquitous Religions
- POJ2524 Ubiquitous Religions
- POJ2524,Ubiquitous Religions
- poj2524--Ubiquitous Religions
- POJ2524--Ubiquitous Religions
- 【POJ2524】Ubiquitous Religions
- POJ2524 Ubiquitous Religions
- poj2524 Ubiquitous Religions
- poj2524 Ubiquitous Religions
- Spring Cache-缓存概述及使用
- 安装源代码版本php7
- 洛谷 P1522 牛的旅行 Cow Tours(Floyd, 并查集)
- js快速排序
- ATP的开学第一个月
- [poj2524]Ubiquitous Religions
- Rxjava2.x很详细的介绍
- [BZOJ2326][HNOI2011]数学作业(矩乘)
- js归并排序
- POJ 2051 Argus
- 从零开始写Python爬虫 --- 1.1 requests库的安装与使用
- Bootstrap初步了解 起步 基本模板
- [LintCode]175.翻转二叉树
- js封装cookie函数