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. [51NOD1393]0和1相等串(前缀和,map)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1393 题意:中文题面. 把0看成是-1,并且存一遍前缀和.这样 ...

  2. LINQ 简单用法【1】

    LINQ:Language INtegrated Query,语言集成查询. 以下内容演示如何利用LINQ进行增加,修改,删除和查询操作,针对数据库. 首先创建Linq Class. 添加数据库信息, ...

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

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

  4. Jenkins插件及 测试源码

    Jenkins 插件: https://updates.jenkins-ci.org/download/plugins/ 小米的一份android源码,测试工具,用于抢红包: https://gith ...

  5. CSharp Similarities and Differences

    This document lists some basic differences between Nemerle and C# in a terse form. If you know Java ...

  6. mouseover和mouseenter的区别

    mouseover和mouseenter都是鼠标事件. mouseover事件,当鼠标穿过被选元素的时候,若此元素有子元素,子元素也会被触发此事件.即是使用mouseover事件,会多次触发此元素. ...

  7. LTE Module User Documentation(翻译6)——物理误差模型、MIMO模型、天线模型

    LTE用户文档 (如有不当的地方,欢迎指正!) 9 PHY Error Model   物理误差模型包含数据误差模型和下行控制误差模型,两者默认为激活.可以使用 ns-3 属性系统去激活,具体为:   ...

  8. sql概要

    sql(structured query language) 1.比较运算符一共有六种,分别为等于(=),小于(<),大于(>),小于或等于(<=),大于或等于(>=)以及不等 ...

  9. java获取中问名字的首字母

    public class FirstLetterUtil { private static int BEGIN = 45217; private static int END = 63486; // ...

  10. Generator 函数的含义与用法

    Generator 函数是协程在 ES6 的实现,最大特点就是可以交出函数的执行权(即暂停执行). function* gen(x){ var y = yield x + 2; return y; } ...