2524 克鲁斯卡尔算法
来源:互联网 发布:手机淘宝更多宝贝链接 编辑:程序博客网 时间:2024/04/28 06:14
#include"iostream"#include"stdio.h"#include"algorithm"#include"stdlib.h"using namespace std;const int maxn = 50000+5;int pre[maxn];void init(int n){ for(int i=1;i<=n;i++) pre[i] = i;}int find(int x){ return pre[x] == x ? x : pre[x] = find(pre[x]);}bool join(int a,int b){ a=find(a);b=find(b); if(a==b) return false; else{ int father = min(a,b); int son = max(a,b); pre[son]=father; return true; }}int main(){ //freopen("input","r",stdin); //freopen("output","w",stdout); int n,m; int Case = 0; while( scanf("%d%d",&n,&m)&&(n+m) ) { init(n); int u,v; for(int i=0;i<m;i++){ scanf("%d%d",&u,&v); join(u,v); } int cnt=0; for(int i=1;i<=n;i++) { if(i==pre[i]) cnt++; } cout<<"Case "<<++Case<<":"<<" "; cout<<cnt<<endl; } return 0;}
0 0
- 2524 克鲁斯卡尔算法
- 克鲁斯卡尔算法
- 克鲁斯卡尔算法
- 克鲁斯卡尔算法
- 克鲁斯卡尔算法
- 克鲁斯卡尔算法
- 克鲁斯卡尔算法
- 克鲁斯卡尔算法
- 克鲁斯卡尔算法
- 克鲁斯卡尔算法
- 克鲁斯卡尔算法
- 克鲁斯卡尔算法
- 克鲁斯卡尔(Kruskal)算法
- 克鲁斯卡尔算法模版
- 克鲁斯卡尔(Kruskal)算法
- kruskal克鲁斯卡尔算法
- 克鲁斯卡尔算法(Kruskal)
- 克鲁斯卡尔算法&普里姆算法
- iOS 开发中的八种锁(Lock)
- Unity3D使用TCP/IP协议,传递protocol buffer消息protobuf-net
- poj2029 Get Many Persimmon Trees
- Squid的refresh_pattern配置
- Material Design Library 使用汇总
- 2524 克鲁斯卡尔算法
- Mac和Windows中常见中文字体的英文名称
- 从Handler到runOnUiThread
- GPS定位
- C# Unicode与中文互转
- 敏捷开发
- 利用负载均衡优化和加速HTTP应用
- springmvc使用声明式事物简单配置步骤
- C语言笔记4--变量作用域