利用并查集求最大生成树和最小生成树(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 ...
随机推荐
- w_click_twice
var w_global_obj; var dat = new Date(); var w_golbal_count_millseconds; function w_click_twice(w_cur ...
- 在Delphi中如何动态创建dbf数据库(二)?
unit Form_ToChangCSVforDBFU; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics ...
- java.util.concurrent.CopyOnWriteArrayList
import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; impo ...
- linux 不能用clock 计算sleep的时间
http://bbs.csdn.net/topics/390558707 在Windows Sleep()占用processor time,Linux下的sleep()不占用processor tim ...
- string strSQL = "Select * From Employees;Select * from Customers";执行两次查询
SqlCommand对象的字符串SQL命令可以做多个,以查询为例,用到SqlDataReader的一些方法,如ExecuteReader(),Read()(一条命令内的移动至下一记录),NextRes ...
- 将自己写的Python代码打包放到PyPI上
如果是开源的Python代码,为了能够让大家更方便的使用,放到PyPI上也许是个非常不错的主意(PyPI:Python Package Index).刚开始我以为要将代码打包放到PyPI上是一件非常复 ...
- [LeetCode]题解(python):088 Merge Sorted Array
题目来源 https://leetcode.com/problems/merge-sorted-array/ Given two sorted integer arrays nums1 and num ...
- XPS1330 作为Linux服务器之安装配置计划
# Task 状态 完成时间 备注 博文链接 1. 打通SSH 未开始 -- 安装系统后已经具备 -- 2. 打通FTP 未开始 -- 安装系统后已经具备 -- 3. ...
- iOS ASIHTTPRequest 使用指南
http://www.devdiv.com/iOS_iPhone-ASIHTTPRequest使用指南---_lt__lt_翻译稿_gt__gt_---连载-thread-93741-1-1.html
- 一些常用的linux命令
查看本机ip地址 ifconfig ln -s phpmyadmin 添加链接 /var/www/ 与html 同级 打开本机电脑硬盘: cd /media/用户名 文件夹的新建命令: sudo ...