Pseudoforest

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

Total Submission(s): 2311    Accepted Submission(s): 892
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   |   We have carefully selected several similar problems for you:  3362 3368 3364 3365 3369 

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
int n,m,minn,vis[10000+10],pre[10000+10];
struct node
{
int u,v,val;
}edge[100000+10];
void init()
{
for(int i=0;i<10010;i++)
pre[i]=i;
}
int cmp(node s1,node s2)
{
return s1.val>s2.val;
}
int find(int x)
{
return pre[x]==x?x:pre[x]=find(pre[x]);
}
int main()
{
while(scanf("%d%d",&n,&m),n||m)
{
init();
for(int i=0;i<m;i++)
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].val);
sort(edge,edge+m,cmp);
minn=0;
memset(vis,0,sizeof(vis));
for(int i=0;i<m;i++)
{
int fx=find(edge[i].u);
int fy=find(edge[i].v);
if(fx!=fy)
{
if(!vis[fx]&&!vis[fy])
{
minn+=edge[i].val;
pre[fx]=fy;
}
else if(!vis[fx]||!vis[fy])
{
minn+=edge[i].val;
pre[fx]=fy;
vis[fx]=vis[fy]=1;
}
}
else
{
if(!vis[fx]&&!vis[fy])
{
minn+=edge[i].val;
vis[fx]=vis[fy]=1;
}
}
}
printf("%d\n",minn);
}
return 0;
}

hdoj--3367--Pseudoforest(伪森林&&最大生成树)的更多相关文章

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

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

  2. 【hdu3367】Pseudoforest(伪森林)

    http://acm.hdu.edu.cn/showproblem.php?pid=3367 题目大意 伪森林就是一个无向图,这个无向图有多个连通块且每个连通块只有一个简单环. 给你一个无向图,让你找 ...

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

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

  4. hdu3367最大伪森林(并查集)

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/3367/ 题目要求一个连通图的最大伪森林,伪森林是一个最多有一个回路的图.我们只要用Kruskal最大生成树的策略 ...

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

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

  6. hdu 3367 Pseudoforest(并查集)

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

  7. Pseudoforest(伪最大生成树)

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

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

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

  9. 【HDOJ】3367 Pseudoforest

    并查集. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXN 10005 #d ...

随机推荐

  1. C - Unary(map)

    Problem description Unary is a minimalistic Brainfuck dialect in which programs are written using on ...

  2. ios - masonry第三方库使用自动布局(参考:http://www.cocoachina.com/ios/20141219/10702.html)

    #import "ViewController.h" #import "Masonry.h" #define kWeakSelf(weakSelf) __wea ...

  3. 第二次作业&熟悉使用工具

     GIT地址  我的地址  GIT用户名  995020892w  学号后五位  81105  博客地址  我的博客  作业链接  第二次作业 一.环境配置过程 安装vs2017 因为以前学习C#相关 ...

  4. 开源作品-ThinkPHP在线分析工具(单文件绿色版)-TPLogAnalysis_PHP_1_0

    TPLogAnalysis_PHP_1_0 前言:项目开发基于ThinkPHP框架,但是在调试程序的时候,没有一款日志可视化分析工具.在网络也找不到任何相关的TP日志分析工具.求人不如求己,于是决定抽 ...

  5. QT4.8界面设计(MSVC2010X)

    1.C++ IDE设计 MFC这种半死不活的windows C++平台已经被抛弃,很无奈.转向Qt的C++还是不错的选择. QT的QML才是最新的亮点,可惜没有时间经历这些东西了. 2.程序代码 2. ...

  6. Q1002 四则运算

    #include<iostream> using namespace std; int main() { long long sum1,sum2,sum3,sum4; long int a ...

  7. (转)基于MVC4+EasyUI的Web开发框架经验总结(5)--使用HTML编辑控件CKEditor和CKFinder

    http://www.cnblogs.com/wuhuacong/p/3780356.html Web开发上有很多HTML的编辑控件,如CKEditor.kindeditor等等,很多都做的很好,本文 ...

  8. (转) RabbitMQ学习之spring整合发送同步消息(注解实现)

    http://blog.csdn.net/zhu_tianwei/article/details/40918477 上一篇文章通过xml配置rabbitmq的rabbitTemplate,本节将使用注 ...

  9. 【disconf】环境搭建【linux】

     1.搭建disconf需要安装的配置. 安装Linux:CentOS7     安装Zookeeper:zookeeper-3.4.6     安装Redis:redis-3.0.0     安装N ...

  10. Python笔记22-----高阶函数

    1.sorted(排序对象,key=):排序对象可以是类别,也可以是字符串和字典,key为自定义排序,如:[key=abs,按绝对值排序][key=lambda x:x[1],按排序对象的第二个值排序 ...