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. 初识shell脚本

    shell字符串.shell数组.shell echo指令.shell test命令.shell if语句.shell case语句.shell for语句.shell while语句.shell b ...

  2. 2014年辛星完全解读Javascript第八节 json

    json是JavaScript Object Notation的简写,它是一种轻量级的数据交换格式,而且表达上很容易靠字面去理解.json是用于存储和传输数据的格式,通常用于向服务器端传递数据. ** ...

  3. Fedora 17安装NFS

    1.NFS概述 NFS(Network File System)是一种分布式文件系统,允许网络中的安装不同操作系统的计算机间共享文件和外设,所以它的通讯协定设计与主机及作业系统无关. 它是由SUN公司 ...

  4. shutdown彻底关闭tomcat,以及多线程关闭

    最近做的一个Web项目,发现shutdown.sh后,无法关掉tomcat进程. ps -ef | grep tomcat 返回tomcat进程仍然存在.经过调查发现是因为在Web应用中启动了线程池, ...

  5. 微软职位内部推荐-Principal Dev Manager for Windows Phone Shell

    微软近期Open的职位: Location: China, BeijingDivision: Operations System Group Engineering Group OverviewOSG ...

  6. ubuntu的vi

    ubuntu12.04的vi 1. 安装vim full版本由于Ubuntu预安装的是tiny版本,就会导致我们在使用上的产生不便.所以我们要安装vim的full版本.首先,先卸掉旧版的vi,输入以下 ...

  7. "reactive programming"的概念

    下面的内容大多是翻译来的. Reactive Programming? What is Reactive Programming? 为了了解Reactive——从编程范式至其背后的动机,有必要了解现在 ...

  8. Firefly Http通信简单介绍

    原地址:http://www.9miao.com/question-15-54042.html 首先创建firefly工程,firefly-admin.py createproject httptes ...

  9. POJ1260Pearls

    http://poj.org/problem?id=1260 题意 :这个题大概是讲,给你几种等级不同的珠宝,然后告诉你它的数量和价值,等级是升序排列的,且随等级的升高价值也随之升高,但为了防止有的客 ...

  10. SQLite入门与分析(三)---内核概述(2)

    写在前面:本节是前一节内容的后续部分,这两节都是从全局的角度SQLite内核各个模块的设计和功能.只有从全局上把握SQLite,才会更容易的理解SQLite的实现.SQLite采用了层次化,模块化的设 ...