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
 

这道题,完全是看了别人的结题报告做的,完全没有搞懂这道题要干么.....

  给出一个图,要求出最大的pseudoforest, 所谓pseudoforest就是指这个图的一个子图,这个子图的每个连通分量中最多只能有一个环, 而且这个子图的所有权值之和最大。这个就是所谓的伪森林。

过程类似与kruskal求最小生成树,千万不要直接求最大生成树,一开始时我想到的方法是用kruskal算法求出这个图的最大生成树, 然后给每一棵数再加上一条最大的边,构成一个环。 但是WA得快吐血了。

正确的做法和求最大生成树很类似,但是有一点改变, 因为每个连通分量允许有一个回环, 所以,我们可以在进行合并两颗树时,要判断这两颗树是否有回环,如果两个树都有回环,那么明显不可以合并这两颗树, 如果只有一棵树有回环,那么可以合并,然后标上记号。如果两个都没有回环,那么就直接合并了。
如果有两个点是属于同一棵树上的,那么判断这棵树上是否已有回环,如果没有的话,那么允许有一个回环,可以链接这两点,再标上记号。

代码:

 // hdu 3367 最大生成树
// author: Gxjun
// date: 2014/11/18
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
const int maxn =; struct node {
int u,v,c;
bool operator < (const node &bb) const {
return c > bb.c;
}
}sac[maxn*]; int n,m;
int father[maxn];
bool tag[maxn]; void init()
{
for(int i=;i<n;i++){
father[i]=i;
}
} int fin(int x){
while(x!=father[x])
x=father[x];
return x;
} bool Union(int x,int y)
{
x=fin(x);
y=fin(y);
if(x==y){
if(tag[x]) return ;
else{
tag[x]=; //表示已经形成环
return ;
}
}
if(tag[x]&&tag[y]) //如果两者均形成环,这说明形成了两个环
return ;
if(tag[x]) father[y]=x; //增大原有的环
else father[x]=y;
return ;
} int main()
{
while(~scanf("%d%d",&n,&m)&&n+m){
for(int i= ; i<m ; i++ ){
scanf(" %d %d %d ",&sac[i].u,&sac[i].v,&sac[i].c);
}
sort(sac,sac+m);
init();
memset(tag,,sizeof tag);
int ans=;
for(int i= ; i<m ; i++)
{
if(Union(sac[i].u,sac[i].v))
ans+=sac[i].c;
}
printf("%d\n",ans);
}
return ;
}

hdu 3367(Pseudoforest ) (最大生成树)的更多相关文章

  1. hdu 3367 Pseudoforest(最大生成树)

    Pseudoforest Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  2. hdu 3367 Pseudoforest 最大生成树★

    #include <cstdio> #include <cstring> #include <vector> #include <algorithm> ...

  3. hdu 3367 Pseudoforest (最大生成树 最多存在一个环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3367 Pseudoforest Time Limit: 10000/5000 MS (Java/Oth ...

  4. hdu 3367 Pseudoforest

    Pseudoforest Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  5. hdu 3367(与最大生成树无关。无关。无关。重要的事情说三遍+kruskal变形)

    Pseudoforest Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  6. HDU 3367 Pseudoforest(Kruskal)

    Pseudoforest Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  7. hdu 3367 Pseudoforest (最小生成树)

    Pseudoforest Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  8. hdu 3367 Pseudoforest(并查集)

    题意:有一种叫作Pseudoforest的结构,表示在无向图上,每一个块中选取至多包含一个环的边的集合,又称“伪森林”.问这个集合中的所有边权之和最大是多少? 分析:如果没有环,那么构造的就是最大生成 ...

  9. HDU 3367 (伪森林,克鲁斯卡尔)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3367 Pseudoforest Time Limit: 10000/5000 MS (Java/Oth ...

随机推荐

  1. php cookie详解

    各参数详解 注意: 1   当一个Cookie被删除时,它的值在当前页在仍然有效的.原因是删除cookie实际也是设置cookie,  只是把cookie的值设为‘’或者null,或者把cookie的 ...

  2. MySQL做练习时总结的一些知识点

    MySQL做练习时总结的一些知识点     0:mysql有三种注释方法 上午插入记录的时候一直没有成功,郁闷不知道为什么.因为是很多条记录一起插入,中间一些不用的数据就用"--" ...

  3. FreeSWITCH第三方库(音频)的简单介绍(一)

    FreeSWITCH使用了大量的第三方库,本文档主要介绍音频相关库的信息: 视频相关库的信息介绍参考:http://www.cnblogs.com/yoyotl/p/5488890.html 其他相关 ...

  4. Ubuntu14.04设置开机root用户登录

    1.sudo vim /usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf 2.添加:greeter-show-manual-login=true 3.su ...

  5. 使用BAPI_ACC_DOCUMENT_POST,创建会计凭证,用BADI扩展字段(转)

    业务需求:和银行做一个接口,要通过银行流水产生会计凭证,会计凭证的事务码是F-02,查到了BAPI方法BAPI_ACC_DOCUMENT_POST.昨天测试发现,有一些参数在BAPI_ACC_DOCU ...

  6. php flush()刷新不能输出缓冲的原因分析

    在php程序编写中,flush()的使用率还是挺高的,它在网页表现即时信息效果时发挥了极为重要的作用,比如之前写的php实现限制文件下载速度的代码实例,flush()就起了举足轻重的作用,是进度条实现 ...

  7. Android Fragment分页显示的实现

    分页显示有两种方式 一种是使用ViewPager 另一种是使用FragmentTransaction 上代码 1 FragmentTransaction实现方式 public class MainAc ...

  8. Maven——聚合与继承

    原文:http://www.cnblogs.com/xdp-gacl/p/4058008.html 一.聚合 如果我们想一次构建多个项目模块,那我们就需要对多个项目模块进行聚合 1.1.聚合配置代码 ...

  9. jackson annotations注解详解 (zhuan)

    http://blog.csdn.net/sdyy321/article/details/40298081 ************************************** 官方WIKI: ...

  10. Eclipse下导入外部jar包的3种方式 (zhuan)

    http://blog.csdn.net/mazhaojuan/article/details/21403717 ******************************************* ...