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. TCP/IP协议简介

    计算机网络是什么? 简单地理解,计算机网络的任务就是传输数据.为了完成这一复杂的任务,国际标准化组织ISO提供了OSI参考模型,这种模型把互联网网络氛围7层,分别是物理层.数据链路层.网络层.传输层. ...

  2. C#打印条码的几种方式

    标题虽然是说C#,但是以下介绍的几种方法不是只能在C#中使用,在其它的语言里面也行. 总结一下常见的条码打印方法,其实打条码的方式很多,大概有以下几种: 1.斑马打印软件制作好模板,保存为.prn格式 ...

  3. Servlet学习

    编写Servlet应该注意的一些细节: 1: 由于客户端是通过URL地址访问web服务器中的资源,所以Servlet程序若想被外界访问,必须把servlet程序映射到一个URL地址上,这个工作在web ...

  4. week7团队项目体会

    经过这几个星期以来的软件工程的学习,还有自己在个人项目.结对编程.团队项目的感受,总结了一些感受和理解. 总结收获 首先,一个月的软件工程团队项目的进行让我对软件开发有了比较实际的认识,之前的概念仅限 ...

  5. iOS - NSURLConnection 网络请求

    前言 @interface NSURLConnection : NSObject class NSURLConnection : NSObject DEPRECATED: The NSURLConne ...

  6. Python学习(17)异常处理

    目录 Python 异常处理 Python 标准异常 异常处理 使用except而不带任何异常类型 使用except而带多种异常类型 try-finally 语句 异常参数 异常的参数 用户自定义参数 ...

  7. thinkphp分页显示

    先在Common\Comon里建一个function.php公共方法,然后在里面新建一个getpage方法,代码如下: <?php /** * TODO 基础分页的相同代码封装,使前台的代码更少 ...

  8. (二)程序中的内存&&栈

    一.程序运行为什么需要内存?基本概念? 内存是程序运行的立足之地,程序需要用内存来存储一些变量. 内存管理最终是由操作系统完成的,内存在本质上是一个硬件器件,由硬件系统提供:内存由操作系统统一管理,为 ...

  9. 列车时刻表查询 jqm/ajax/xml

    <!doctype html><html lang="en"><head> <meta charset="UTF-8" ...

  10. 20160815_Redis安装

    OS: CentOS6.4(x64) 参考网址: http://www.cnblogs.com/haoxinyue/p/3620648.html http://www.codeceo.com/arti ...