HDU 3367 (伪森林,克鲁斯卡尔)
传送门:
http://acm.hdu.edu.cn/showproblem.php?pid=3367
Pseudoforest
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3242 Accepted Submission(s): 1273
The last test case is followed by a line containing two zeros, which means the end of the input.
0 1 1
1 2 1
2 0 1
4 5
0 1 1
1 2 1
2 3 1
3 0 1
0 2 2
0 0
5
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define max_n 10005
#define max_m 100005
struct edge
{
int x,y;
int w;
};
edge e[max_m];
int cy[max_n];
int pa[max_n];
int sum;
bool cmp(edge a,edge b)
{
return a.w>b.w;
}
void make_set(int x)
{
pa[x]=x;
cy[x]=;
}
int find_set(int x)
{
if(x!=pa[x])
pa[x]=find_set(pa[x]);
return pa[x];
}
void union_set(int x,int y,int w)
{
int a=find_set(x);
int b=find_set(y);
if(a==b&&cy[a]==)//两点在同一颗树,且该树没有环
{
cy[a]=;
sum+=w;
}else if(a!=b&&(cy[a]==||cy[b]==))//两点在不同树上,两个树最多一个有环
{
sum+=w;
pa[a]=b;
if(cy[a]==)
cy[b]=cy[a];
}
}
int main()
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
sum=;
if(n+m==)
break;
for(int i=;i<n;i++)
make_set(i);
for(int i=;i<m;i++)
{
scanf("%d %d %d",&e[i].x,&e[i].y,&e[i].w);
}
sort(e,e+m,cmp);
for(int i=;i<m;i++)
{
union_set(e[i].x,e[i].y,e[i].w);
}
printf("%d\n",sum);
}
}
HDU 3367 (伪森林,克鲁斯卡尔)的更多相关文章
- HDU 1863 畅通工程 克鲁斯卡尔算法
畅通工程 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...
- HDU 1233 还是畅通工程(模板——克鲁斯卡尔算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1233 题意描述: 输入n个城镇以及n*(n-1)/2条道路信息 计算并输出将所有城镇连通或者间接连通 ...
- hdu 1233 还是畅通project (克鲁斯卡尔裸题)
还是畅通project Time Limit: 4000/2000 MS (Java/Others) M ...
- HDU 1879 继续畅通工程 (Prim(普里姆算法)+Kruskal(克鲁斯卡尔))
继续畅通工程 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- hdu 1233(还是畅通project)(prime算法,克鲁斯卡尔算法)(并查集,最小生成树)
还是畅通project Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- hdu 1679 The Unique MST (克鲁斯卡尔)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24152 Accepted: 8587 D ...
- hdu 3367(Pseudoforest ) (最大生成树)
Pseudoforest Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- 【hdu3367】Pseudoforest(伪森林)
http://acm.hdu.edu.cn/showproblem.php?pid=3367 题目大意 伪森林就是一个无向图,这个无向图有多个连通块且每个连通块只有一个简单环. 给你一个无向图,让你找 ...
随机推荐
- docker 安装PIL python 报错 IOError: decoder zip not available
按照网上得方法:安装依赖库:yum install freetype freetype-develyum install libjpeg libjpeg-develyum install zlib z ...
- 理解webpack4.splitChunks
一.前言 之前一直也没有研究过webpack4是基于怎样的规则去拆分模块的,现在正好有时间打算好好了解一下,看了官方文档也陆陆续续的看了看网上别人写的文章,感觉大部分都是将官方文档翻译了一遍,很多问题 ...
- 须知的css——margin不重叠的情形
margin重叠 摘自css2.1规范中文版 CSS中,两个或者多个盒(可能但不一定是兄弟)的相邻的margin会被结合成一个margin.Margin按这种方式结合叫重叠(collapse),产生的 ...
- meta name id class 标签的区别
meta https://zhidao.baidu.com/question/2052283721385566387.html name 跟 id 的区别 http://blog.csdn.net/f ...
- Struts22222
一,什么是框架? 所谓框架就是提供了一组统一的接口和编程方式的可以重用组件,同时我们可以在框架中扩充我们自己的特定逻辑. 二,MVC设计模式 将应用程序分为3个部分:模型 Model,视图View, ...
- webform 使用log4net配置
效果: web.config配置 <configuration> <configSections> <!--log4net日志记录--> <section n ...
- AngularJS+RequireJs实现动态加载JS和页面的方案研究【下】
about.js: [html] view plain copy 在CODE上查看代码片派生到我的代码片 define(['app'], function(app) { app.controller( ...
- LeetCode 531----Lonely Pixel I----两种算法之间性能的比较
Lonely Pixel I 两种算法之间的性能比较 今天参加LeetCode Weekly Contest 22,第二题 "Lonely Pixel I" 问题描述如下: Giv ...
- JS中0与‘0’
JS中0为false,字符串‘0’为true
- 移动端tap事件的封装
/*封装tap*/ cc.tap = function(dom,callback){ /* * 要求 没有触发 touchmove 事件 * 并且响应速度要比click快 */ if(dom & ...