Pseudoforest

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1070    Accepted Submission(s): 417 
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
 

Recommend
lcy
// 开始我想错了 我是先搞生成树,然后在每个分块加入一条最大边使其 构成环
// 这样是不对的 应该能成环的先让成环 两个成环的就算有边也不连,不然达不到最大的效果
// 这是求最大生成树问题
#include <iostream>
#include <algorithm>
#include <queue>
#include <math.h>
#include <stdio.h>
#include <string.h>
using namespace std;
struct node{
int u,v,c;
bool operator <(const node b) const{
return c>b.c;
}
}Eg[];
int f[];
bool flag[];
int Find(int x){
if(x!=f[x])
f[x]=Find(f[x]);
return f[x];
}
int main()
{
int n,m;
while(scanf("%d %d",&n,&m),n|m){
int i,j,k;
int u,v;
for(i=;i<m;i++)
scanf("%d %d %d",&Eg[i].u,&Eg[i].v,&Eg[i].c);
sort(Eg,Eg+m);
int sum=;
for(i=;i<n;i++) f[i]=i,flag[i]=false;
for(i=;i<m;i++){
u=Eg[i].u;
v=Eg[i].v;
u=Find(u);
v=Find(v);
if(u!=v){
if(!flag[u]&&!flag[v]){
f[u]=v;
sum+=Eg[i].c;
}
else if(!flag[u]||!flag[v]){
f[u]=v;
sum+=Eg[i].c;
flag[u]=flag[v]=true;
}
}
else{
if(!flag[u]){
flag[u]=true;
sum+=Eg[i].c;
}
}
}
printf("%d\n",sum);
} return ;
}

hdu 3367 Pseudoforest的更多相关文章

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

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

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

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

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

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

  4. HDU 3367 Pseudoforest(Kruskal)

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

  5. hdu 3367 Pseudoforest(并查集)

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

  6. hdu 3367 Pseudoforest 最大生成树★

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

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

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

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

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

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

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

随机推荐

  1. Treimu更新记录1.2.9.0

    Treimu是一个WPF音乐播放器个人小项目.程序集文件:http://pan.baidu.com/s/1pJLSHsB项目源代码:http://pan.baidu.com/s/1jGHtjfC 1. ...

  2. 1 server - n clients 模型实现(select)

    拓扑结构: 各个客户端创建读写管道,通过“上下线信息管道”向服务器发送上下线信息和读写管道名称.服务器接受信息,修改链表(存储客户端信息).客户端.服务器打开读写管道,服务器通过“W”管道接收从客户端 ...

  3. Nested Loop,Sort Merge Join,Hash Join

    三种连接工作方式比较: Nested loops 工作方式是从一张表中读取数据,访问另一张表(通常是索引)来做匹配,nested loops适用的场合是当一个关联表比较小的时候,效率会更高. Merg ...

  4. hadoop可能遇到的问题

    1.hadoop运行的原理? 2.mapreduce的原理? 3.HDFS存储的机制? 4.举一个简单的例子说明mapreduce是怎么来运行的 ? 5.面试的人给你出一些问题,让你用mapreduc ...

  5. [译] ASP.NET 生命周期 – ASP.NET 应用生命周期(一)

    概述 ASP.NET 平台定义了两个非常重要的生命周期.第一个是 应用生命周期  (application life cycle),用来追踪应用从启动的那一刻到终止的那一刻.另一个就是 请求生命周期 ...

  6. 关于count(1) 和 count(*)

    Q:What is the difference between count(1) and count(*) in a sql queryeg.select count(1) from emp; an ...

  7. ios 基于CAEmitterLayer的雪花,烟花,火焰,爱心等效果demo(转)

    转载自:http://blog.csdn.net/mad2man/article/details/16898369 分类: cocoa SDK2013-11-23 11:52 388人阅读 评论(0) ...

  8. 【BZOJ 1067】 [SCOI2007]降雨量

    Description 我们常常会说这样的话:“X年是自Y年以来降雨量最多的”.它的含义是X年的降雨量不超过Y年,且对于任意Y<Z<X,Z年的降雨量严格小于X年.例如2002,2003,2 ...

  9. 应该如何入门deep learning呢?从UFLDL开始!

    抱歉,大家,这里不是要分享如何学习deep learning,而是想要记录自己学习deep learning的小历程,算是给自己的一点小动力吧,希望各位业内前辈能够多多指教! 看到有网友提到,Andr ...

  10. HTTP协议返回代码含义

    1XX 代码这些状态代码表示临时的响应.客户端在收到常规响应之前,应准备接收一个或多个1xx 响应 100 – 继续101 – 切换协议2XX 代码这类状态代码表明服务器成功地接受了客户端请求,一般日 ...