利用并查集求最大生成树和最小生成树(nlogn)
hdu1233
还是畅通工程
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21371 Accepted Submission(s): 9515
Input
当N为0时,输入结束,该用例不被处理。
Output
Sample Input
3
1 2 1
1 3 2
2 3 4
4
1 2 1
1 3 4
1 4 1
2 3 3
2 4 2
3 4 5
0
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#define M 10001
struct st
{
int u,v,w;
}edge[M];
int f[M];
int cmp1(const void *a,const void *b)
{
return (*(struct st*)a).w-(*(struct st *)b).w;
}
int cmp2(const void *a,const void *b)
{
return (*(struct st*)b).w-(*(struct st *)a).w;
}
int finde(int x)
{
if(x!=f[x])
f[x]=finde(f[x]);
return f[x];
}
void make(int a,int b)
{
int x=finde(a);
int y=finde(b);
if(x!=y)
f[x]=y;
}
int main()
{
int i,m,n,a,b,c;
while(scanf("%d",&n),n)
{
m=n*(n-1)/2;
for(i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
edge[i].u=a;
edge[i].v=b;
edge[i].w=c;
}
for(i=1;i<=n;i++)
f[i]=i;
qsort(edge,m,sizeof(edge[0]),cmp1);
int sum=0;
for(i=0;i<m;i++)
{
int u=edge[i].u;
int v=edge[i].v;
if(finde(u)!=finde(v))
{
sum+=edge[i].w;
make(u,v);
}
}
printf("%d\n",sum);
}
return 0;
}
利用并查集求最大生成树和最小生成树(nlogn)的更多相关文章
- 分别利用并查集,DFS和BFS方法求联通块的数量
联通块是指给定n个点,输入a,b(1<=a,b<=n),然后将a,b连接,凡是连接在一起的所有数就是一个联通块: 题意:第一行输入n,m,分别表示有n个数,有输入m对连接点,以下将要输入m ...
- Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 并查集求奇偶元环
D. Dividing Kingdom II Long time ago, there was a great kingdom and it was being ruled by The Grea ...
- C. Edgy Trees Codeforces Round #548 (Div. 2) 并查集求连通块
C. Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- HDU 3018 Ant Trip (并查集求连通块数+欧拉回路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 题目大意:有n个点,m条边,人们希望走完所有的路,且每条道路只能走一遍.至少要将人们分成几组. ...
- BC68(HD5606) 并查集+求集合元素
tree Accepts: 143 Submissions: 807 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65 ...
- 利用并查集+贪心解决 Hdu1232
畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- PAT L2-013 红色警报(并查集求连通子图)
战争中保持各个城市间的连通性非常重要.本题要求你编写一个报警程序,当失去一个城市导致国家被分裂为多个无法连通的区域时,就发出红色警报.注意:若该国本来就不完全连通,是分裂的k个区域,而失去一个城市并不 ...
- 蓝桥杯历届试题 危险系数(dfs或者并查集求无向图关于两点的割点个数)
Description 抗日战争时期,冀中平原的地道战曾发挥重要作用. 地道的多个站点间有通道连接,形成了庞大的网络.但也有隐患,当敌人发现了某个站点后,其它站点间可能因此会失去联系. 我们来定义一个 ...
- 杭电 1213 How Many Tables (并查集求团体数)
Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius ...
随机推荐
- android 设计工具栏
设计工具栏Action Bar(订制工具栏类型) 工具栏给用户提供了一种熟悉和可预测的方式来执行某种动作和操纵应用程序,但是这并不意味着它就需要和其他的应用程序看起来一样的.如果想设计工具栏以使得它能 ...
- cookie 操作
//创建并赋值 重新赋值也是这样操作 document.cookie="userId=828"; document.cookie="userName=hulk" ...
- 八 mybatis查询缓存(一级缓存,二级缓存)和ehcache整合
1 查询缓存 1.1 什么是查询缓存 mybatis提供查询缓存,用于减轻数据压力,提高数据库性能. mybaits提供一级缓存,和二级缓存.
- 使用微信JS-SDK 实现 自定义 分享 功能
微信PC端点击页面,转发给朋友.
- 权限管理:(RBAC)
一般做正规的权限管理程序基本就是以下M表模式: 例1:在页面显示管理者的权限,并可以修改的管理界面 数据库表如下: 管理界面(附ajax): <body> <?php include ...
- Liunx 下使用cmake
参考 http://blog.chinaunix.net/uid-28458801-id-3501768.html http://www.ibm.com/developerworks/cn/linux ...
- C++ 简易时间类
.h file #ifndef LIBFRAME_DATETIME_H_ #define LIBFRAME_DATETIME_H_ #include <stdint.h> #include ...
- HTML5新标签和属性
1.<time>标签(支持IE9以上和其他浏览器) 今年是<time datetime="2015-12-12">2015年</time> &l ...
- .net framework环境
microsoft.com/downloads/details.aspx?FamilyId=262D25E3-F589-4842-8157-034D1E7CF3A3&displaylang=z ...
- shell 统计GMT0 时区的数据
和某个供应商核对数据,对方是GMT+0时区,我方报表默认北京时间,无法修改为GMT0, 对excel中按照小时级别的数据导出到excel处理,然后转为文本文件,shell转为GMT0进行统计: 前期处 ...