poj2524
来源:互联网 发布:怎样优化直通车 编辑:程序博客网 时间:2024/05/21 14:06
简单并查集。这里要注意一点:当n>0且m为0是此时应该输出1。其它就是并查集的典型水题了。
下面是代码:352K+313MS
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <algorithm>#define Max 50010#define Maxx(a,b) (a)>(b)?(a):(b)#define Min(a,b) (a)<(b)?(a):(b)using namespace std;int set[Max];int find(int x){ int j=x;while(x!=set[x])x=set[x]; int temp;while(j!=x){temp=set[j];set[j]=x;j=temp;}return x;}int n,m;int main(){int Case=1;while(scanf("%d%d",&n,&m)){if(n==0)break;int a,b,x,y;for(int i=1;i<=n;i++)set[i]=i;for(int i=1;i<=m;i++){scanf("%d%d",&a,&b);x=find(a);y=find(b);if(x!=y)set[Maxx(x,y)]=Min(x,y);}for(int i=1;i<=n;i++)set[i]=find(i);sort(set+1,set+1+n);int ans=1;for(int i=2;i<=n;i++)if(set[i]!=set[i-1])ans++;printf("Case %d: %d\n",Case++,ans);}return 0;}
0 0
- poj2524
- poj2524
- POJ2524
- poj2524
- POJ2524
- poj2524
- poj2524
- [POJ2524]宗教
- poj2524 Ubiquitous Religions
- Ubiquitous Religions POJ2524
- poj2524解题报告
- POJ2524 Ubiquitous Religions
- POJ2524:Ubiquitous Religions
- poj2524 Ubiquitous Religions
- POJ2524(并查集)
- poj2524(并查集)
- POJ2524-并查集
- poj2524 Ubiquitous Religions
- 算法 老鼠迷宫问题
- 消息队列之JMS和AMQP对比
- ORACLE 自带的元数据管理
- C#泛型
- 关于MMS,SMS常用的Uri
- poj2524
- 个的风格的风格的风格
- Python 发送带 附件 邮件 脚本
- hdoj3549 Flow Problem(裸的网络流)
- Unable to execute dex: Multiple dex files define
- JAVA与ACM的那些事(不断整理更新ing)
- Largest Rectangle in Histogram
- 浅谈面相对象思想
- android代码混淆常见问题及解决