hdu 3367(与最大生成树无关。无关。无关。重要的事情说三遍+kruskal变形)
Pseudoforest
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2351 Accepted Submission(s): 910
graph theory, a pseudoforest is an undirected graph in which every
connected component has at most one cycle. The maximal pseudoforests of G
are the pseudoforest subgraphs of G that are not contained within any
larger pseudoforest of G. A pesudoforest is larger than another if and
only if the total value of the edges is greater than another one’s.
input consists of multiple test cases. The first line of each test case
contains two integers, n(0 < n <= 10000), m(0 <= m <=
100000), which are the number of the vertexes and the number of the
edges. The next m lines, each line consists of three integers, u, v, c,
which means there is an edge with value c (0 < c <= 10000) between
u and v. You can assume that there are no loop and no multiple edges.
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 <algorithm>
#include <string.h>
using namespace std; const int N = ;
const int M =;
int father[N];
int vis[N]; ///判断此树是否有环
int _find(int x){
if(x==father[x]) return x;
return father[x]=_find(father[x]);
}
struct Edge{
int u,v,c;
}edge[M];
int cmp(Edge a,Edge b){
return b.c < a.c;
} int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF,(n+m)){
memset(vis,,sizeof(vis));
for(int i=;i<n;i++){
father[i] = i;
}
for(int i=;i<m;i++){
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].c);
}
sort(edge,edge+m,cmp);///从大到小排序
long long ans = ;
for(int i=;i<m;i++){
int x = _find(edge[i].u);
int y = _find(edge[i].v);
if(x==y){ ///已经是同一棵树了,判断是否该树是否存在环,不存在则可以连接两点形成环
if(!vis[x]){
vis[x]=vis[y]=;
ans+=edge[i].c;
}
}else{ ///不是同一棵树
if(vis[x]&&vis[y]) continue; ///都存在环了不可能合并了
if(vis[x]||vis[y]) vis[x]=vis[y]=; ///只要两棵子树中有一棵有环,则新生成的树也是有环的
father[x]=y;
ans+=edge[i].c;
}
}
printf("%lld\n",ans);
}
return ;
}
hdu 3367(与最大生成树无关。无关。无关。重要的事情说三遍+kruskal变形)的更多相关文章
- hdu 3367 Pseudoforest(最大生成树)
Pseudoforest Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- hdu 3367 Pseudoforest 最大生成树★
#include <cstdio> #include <cstring> #include <vector> #include <algorithm> ...
- hdu 3367(Pseudoforest ) (最大生成树)
Pseudoforest Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- hdu 3367 Pseudoforest (最大生成树 最多存在一个环)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3367 Pseudoforest Time Limit: 10000/5000 MS (Java/Oth ...
- 【与软件无关】2013赤峰地区C1科目三考试攻略【绝对原创】
期待很久的科目三,终于在开考了.传说中的全部电子评判,让习惯给考官送礼的赤峰人民无所是从.据说前几天曾经有一个驾校,考了一整天,八十多个人一个没过的. 我这个攻略是今天通过考试后的一点心得,希望能有用 ...
- HDU 3367 (伪森林,克鲁斯卡尔)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3367 Pseudoforest Time Limit: 10000/5000 MS (Java/Oth ...
- HDU 4786 Fibonacci Tree(生成树,YY乱搞)
http://acm.hdu.edu.cn/showproblem.php? pid=4786 Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others ...
- hdu 3367 Pseudoforest
Pseudoforest Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- HDU 3367 Pseudoforest(Kruskal)
Pseudoforest Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
随机推荐
- 【状压DP】【UVA11795】 Mega Man's Mission
传送门 Description 你要杀n个怪,每杀掉一个怪那个怪会掉落一种武器,这种武器可以杀死特定的怪.游戏初始你有一把武器,能杀死一些怪物.每次只能杀一只,求有多少种杀怪方法. Input 多组数 ...
- idea导入web项目tomcat
概述 主要分为项目配置和tomcat配置两大步骤. 一.项目配置 打开idea,选择导入项 选择将要打开的项目路径后,继续选择项目的原本类型(后续引导设置会根据原本的项目类型更新成idea的项目),此 ...
- SELECT LAST_INSERT_ID() 的使用和注意事项
SELECT LAST_INSERT_ID() 的使用和注意事项 尊重个人劳动成果,转载请注明出处: http://blog.csdn.net/czd3355/article/details/7130 ...
- @RequestParam 注解的使用
@RequestParam 注解的使用 前言 在SpringMvc后台进行获取数据,一般是两种. 1.request.getParameter(“参数名”) 2.用@RequestParam注解获取 ...
- ACE中TCP通信
转载于:http://www.cnblogs.com/TianFang/archive/2006/12/07/585095.html 概述: 传输控制协议TCP(Transmission Contro ...
- 题解【luoguP4053 bzojP1029 [JSOI2007]建筑抢修】
洛谷题链 bzoj题链 PS: \(t_i\) : 在什么时候建筑 \(i\) 自爆 \(a_i\) : 修复 \(i\) 所花时间 题解 算法:贪心+堆维护 贪心策略: 直接按 \(t\) 贪心?显 ...
- javascript功能封装
实现方式其实很简单,我在代码打上注释,大家就懂了! var _date=[],dateData=["1月","2月","3月",&qu ...
- idea 多模块引用
roma-server 引用common-utils的类,所以在roma-server 的pom中配置 <dependency> <groupId>org.springfram ...
- AngularJs 中的CheckBox前后台交互
前台页面: <div class="form-group"> <label for="CompanyName" class="col ...
- 基本控件文档-UILabel属性---iOS-Apple苹果官方文档翻译
本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址 //转载请注明出处--本文永久链接:http://www.cnblogs.com/ ...