poj 2542
2016-05-26 14:49
447 查看
题意:给定多组(x,y),x和y的信仰相同,估计信仰的数量
解法:并查集,初始化ans为总人数,当添加一对(x,y)后,判断,如果已经是一组了,不做,如果不是,ans-1.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
*This file is about union set operations.
*and the program is c-style,you can transfer it to
*cpp-style.
*you can copy,re-write,sell this program but no need to
*let me know this.but you need copy this comment to your
*program's header comment.
*hujian nankai university
*2016/5/26
*/
#define UNION_SET_MAX_SIZE 1024*50 //the size of union.
//the parent array
int parent[UNION_SET_MAX_SIZE];
//the height of the union tree
int rank[UNION_SET_MAX_SIZE];
//the size of union
int union_size=0;
//the ans
int ans=0,n,m;
//initialze the union set
void init_set(int s)
{
int i;
for(i=0;i<=s;i++){
parent[i]=i;
rank[i]=0;
}
}
//find the root of this tree
int find_set(int x){
if(x==parent[x]){
return x;
}else{
return parent[x]=find_set(parent[x]);
}
}
//union x and y is the root of each tree
void union_set(int x,int y)
{
x=find_set(x);
y=find_set(y);
if(x==y) return;//same tree
//x and y have same religions
ans--;
if(rank[x]<rank[y]){
//the height x<height y
//then,let the y as the root
parent[x]=y;
}else{
parent[y]=x;
if(rank[x]==rank[y]){
rank[x]++;
}
}
}
//judge if the x and y is the same tree
bool same_set(int x,int y)
{
return find_set(x)==find_set(y);
}
int main()
{
int i,x,y,j=1;
while(scanf("%d%d",&n,&m)){
if((n+m)==0) break;
init_set(n);
ans=n;
for(i=0;i<m;i++){
scanf("%d%d",&x,&y);
union_set(x,y);
}
printf("Case %d: %d\n",j++,ans);
}
return 0;
}
解法:并查集,初始化ans为总人数,当添加一对(x,y)后,判断,如果已经是一组了,不做,如果不是,ans-1.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
*This file is about union set operations.
*and the program is c-style,you can transfer it to
*cpp-style.
*you can copy,re-write,sell this program but no need to
*let me know this.but you need copy this comment to your
*program's header comment.
*hujian nankai university
*2016/5/26
*/
#define UNION_SET_MAX_SIZE 1024*50 //the size of union.
//the parent array
int parent[UNION_SET_MAX_SIZE];
//the height of the union tree
int rank[UNION_SET_MAX_SIZE];
//the size of union
int union_size=0;
//the ans
int ans=0,n,m;
//initialze the union set
void init_set(int s)
{
int i;
for(i=0;i<=s;i++){
parent[i]=i;
rank[i]=0;
}
}
//find the root of this tree
int find_set(int x){
if(x==parent[x]){
return x;
}else{
return parent[x]=find_set(parent[x]);
}
}
//union x and y is the root of each tree
void union_set(int x,int y)
{
x=find_set(x);
y=find_set(y);
if(x==y) return;//same tree
//x and y have same religions
ans--;
if(rank[x]<rank[y]){
//the height x<height y
//then,let the y as the root
parent[x]=y;
}else{
parent[y]=x;
if(rank[x]==rank[y]){
rank[x]++;
}
}
}
//judge if the x and y is the same tree
bool same_set(int x,int y)
{
return find_set(x)==find_set(y);
}
int main()
{
int i,x,y,j=1;
while(scanf("%d%d",&n,&m)){
if((n+m)==0) break;
init_set(n);
ans=n;
for(i=0;i<m;i++){
scanf("%d%d",&x,&y);
union_set(x,y);
}
printf("Case %d: %d\n",j++,ans);
}
return 0;
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- 书评:《算法之美( Algorithms to Live By )》
- 动易2006序列号破解算法公布
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C++联合体转换成C#结构的实现方法
- C#实现的算24点游戏算法实例分析
- C++高级程序员成长之路
- C++编写简单的打靶游戏