hdu 3367(Pseudoforest ) (最大生成树)
Pseudoforest
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1844 Accepted Submission(s): 704
Problem Description
In 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
The 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.
Output
Output the sum of the value of the edges of the maximum pesudoforest.
Sample Input
3 3 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
Sample Output
3 5
Source
“光庭杯”第五届华中北区程序设计邀请赛 暨 WHU第八届程序设计竞赛
这道题,完全是看了别人的结题报告做的,完全没有搞懂这道题要干么.....
给出一个图,要求出最大的pseudoforest, 所谓pseudoforest就是指这个图的一个子图,这个子图的每个连通分量中最多只能有一个环, 而且这个子图的所有权值之和最大。这个就是所谓的伪森林。
过程类似与kruskal求最小生成树,千万不要直接求最大生成树,一开始时我想到的方法是用kruskal算法求出这个图的最大生成树, 然后给每一棵数再加上一条最大的边,构成一个环。 但是WA得快吐血了。
正确的做法和求最大生成树很类似,但是有一点改变, 因为每个连通分量允许有一个回环, 所以,我们可以在进行合并两颗树时,要判断这两颗树是否有回环,如果两个树都有回环,那么明显不可以合并这两颗树, 如果只有一棵树有回环,那么可以合并,然后标上记号。如果两个都没有回环,那么就直接合并了。
如果有两个点是属于同一棵树上的,那么判断这棵树上是否已有回环,如果没有的话,那么允许有一个回环,可以链接这两点,再标上记号。
代码:
1 // hdu 3367 最大生成树
2 // author: Gxjun
3 // date: 2014/11/18
4 #include<iostream>
5 #include<cstdio>
6 #include<cstring>
7 #include<cstdlib>
8 #include<algorithm>
9 using namespace std;
10 const int maxn =10005;
11
12 struct node {
13 int u,v,c;
14 bool operator < (const node &bb) const {
15 return c > bb.c;
16 }
17 }sac[maxn*10];
18
19 int n,m;
20 int father[maxn];
21 bool tag[maxn];
22
23 void init()
24 {
25 for(int i=0;i<n;i++){
26 father[i]=i;
27 }
28 }
29
30 int fin(int x){
31 while(x!=father[x])
32 x=father[x];
33 return x;
34 }
35
36 bool Union(int x,int y)
37 {
38 x=fin(x);
39 y=fin(y);
40 if(x==y){
41 if(tag[x]) return 0;
42 else{
43 tag[x]=1; //表示已经形成环
44 return 1;
45 }
46 }
47 if(tag[x]&&tag[y]) //如果两者均形成环,这说明形成了两个环
48 return 0;
49 if(tag[x]) father[y]=x; //增大原有的环
50 else father[x]=y;
51 return 1;
52 }
53
54 int main()
55 {
56 while(~scanf("%d%d",&n,&m)&&n+m){
57 for(int i=0 ; i<m ; i++ ){
58 scanf(" %d %d %d ",&sac[i].u,&sac[i].v,&sac[i].c);
59 }
60 sort(sac,sac+m);
61 init();
62 memset(tag,0,sizeof tag);
63 int ans=0;
64 for(int i=0 ; i<m ; i++)
65 {
66 if(Union(sac[i].u,sac[i].v))
67 ans+=sac[i].c;
68 }
69 printf("%dn",ans);
70 }
71 return 0;
72 }
- mybatis xml mapper 文件中 if-else 写法
- 深入探索Scala的Option
- 在Clion的IDE中指定命令行参数
- CentOS6 安装并破解Jira 7
- Martin Odersky访谈录所思
- 解决Boost库链接出错问题
- 引入Option优雅地保证健壮性
- java正则校验,密码必须由字母和数字组成
- Spring Boot集成JasperReports生成PDF文档
- Redux框架reducer对状态的处理
- 使用Spring Cloud Security OAuth2搭建授权服务
- Nginx性能优化
- linux 如何正确的关闭mongodb
- 运用Aggregator模式实现MapReduce
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- java Spring系列之 配置文件的操作 +Bean的生命周期+不同数据类型的注入简析+注入的原理详解+配置文件中不同标签体的使用方式
- 算法系列1 初识算法 算法复杂性模型 算法复杂度的计算
- 树莓派基础实验32:DS1302实时时钟模块实验
- Failed to parse date ["1534467411000"]:Invalid time zone indicator '0'
- 快速排序 : 调优:3亿数据40秒,2亿数据30秒,1亿数据15秒
- Spring系列之新注解配置+Spring集成junit+注解注入
- Spring系列之aAOP AOP是什么?+xml方式实现aop+注解方式实现aop
- 线上工程启动日志不滚动了——通过查看堆栈信息排查的过程(ES批量插入)
- Java中使用方法的注意事项
- 微信小程序转发朋友圈详解
- Error: Protocol error, got "H" as reply type byte
- 树莓派基础实验33:TCRT5000红外循迹传感器实验
- 10W个Java对象有多大
- 一次线程池引发的线上故障分析
- dubbo 启动Failed to save registry store file报错