poj 2524 Ubiquitous Religions
来源:互联网 发布:手机淘宝店装修教程 编辑:程序博客网 时间:2024/05/17 02:07
原题:http://poj.org/problem?id=2524
简单的并查集的应用。
具体实现如下:
#include <stdio.h>#include <stdlib.h>#include <string.h>#define Max_N 50002int N,M,par[Max_N],rank[Max_N];void init(int n){ int i; for(i=1;i<=n;i++) { par[i]=i; rank[i]=0; }}int find(int x){ if(x == par[x]) return x; else return par[x]=find(par[x]);}void unite(int x,int y){ x=find(x),y=find(y); if(x == y) return; if(rank[x] < rank[y]) { par[x]=y; } else { par[y]=x; if(rank[x] == rank[y]) rank[x]++; }}int same(int x,int y){ return find(x) == find(y);}int main(){ #ifdef LOCAL freopen("input.txt","r",stdin); #endif int i,a,b,n=0; while(EOF !=scanf("%d %d",&N,&M) && 0!=N && 0!=M) { init(N); int count=0; for(i=0;i<M;i++) { scanf("%d %d",&a,&b); if(!same(a,b)) { unite(a,b),count++; }; } printf("Case %d: %d\n",++n,N-count); } return 0;}
0 0
- POJ 2524 Ubiquitous Religions
- POJ 2524 Ubiquitous Religions
- poj 2524 Ubiquitous Religions
- POJ 2524 Ubiquitous Religions
- POJ-2524 Ubiquitous Religions
- Poj 2524 Ubiquitous Religions
- poj 2524 Ubiquitous Religions
- POJ 2524 Ubiquitous Religions
- POJ 2524 Ubiquitous Religions
- poj 2524 Ubiquitous Religions
- POJ 2524 - Ubiquitous Religions
- poj 2524 Ubiquitous Religions
- POJ 2524 Ubiquitous Religions
- poj 2524 Ubiquitous Religions
- poj 2524 Ubiquitous Religions
- POJ 2524 Ubiquitous Religions
- POJ-2524-Ubiquitous Religions
- POJ 2524 Ubiquitous Religions
- java 接口使用
- There is no statement named xxx.xxx in this SqlMap
- Java反射机制
- Android 中 IntentService 的原理及使用
- 毕业半年多的总结
- poj 2524 Ubiquitous Religions
- 批量生成二维码
- iOS-prepareForSegue场景切换,KVC传值
- LeetCode Merge k Sorted Lists
- 程序员的“七宗罪”
- opencv的配置(opencv2.4.9+vs2012)
- 有关字符串和字符的函数
- 谷歌Thread物联网联盟面临重重挑战
- ios程序员6级考试