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. WinForm进程 线程

    进程主要调用另一程序,线程 分配工作. 一.进程: 进程是一个具有独立功能的程序关于某个数据集合的一次运行活动.它可以申请和拥有系统资源,是一个动态的概念,是一个活动的实体.Process 类,用来操 ...

  2. 微信小程序video组件出现无法播放或卡顿

    微信小程序使用video组件播放视频的时候,会出现卡顿或者无法播放的问题,加一个custom-cache=”true“即可解决,这个属性文档上没有,是从小程序开发社区中get到的.

  3. JavaScript获取非行间样式

    <html> <head> <meta charset="utf-8"> <title>无标题文档</title> &l ...

  4. 统计之都 http://cos.name/

    http://cos.name/ IMS:一个洲际人际交流网络(为学生免费提供会员资格) 原文链接:http://cos.name/2014/07/ims-a-cross-continent-huma ...

  5. 还是UVa340

    #include<stdio.h> #define maxn 1010 int main() { int num,a[maxn],i,j,b[maxn]; ; &&num) ...

  6. 1 ERP管理系统概念

    1 ERP管理系统概念 一.ERP是什么? ERP是企业资源计划(Enterpise Resource Planning)的简称,蕴含现代企业管理理念,其核心是在制造资源计划基础上进一步发展而成的面向 ...

  7. associatedtype关联类型

    associatedtype关联类型   定义一个协议时,有的时候声明一个或多个关联类型作为协议定义的一部分将会非常有用.关联类型为协议中的某个类型提供了一个占位名(或者说别名),其代表的实际类型在协 ...

  8. Python中用绘图库绘制一条蟒蛇

    一..构思设计蟒蛇的长度颜色等 首先,我们来构思一个简单的蟒蛇.让它的颜色为黄色,形状为一条正在爬行的蟒蛇. 二..准备绘图库 Python中有一个绘图库叫turtle我们先引入它. import t ...

  9. jemeter参数化读取文件

    1.新建一个123.csv文件 如图 2.添加http请求 3.添加一个查看结果树 二.通过函数助手 (1)打开函数助手 ......... 在json属组中引入变量  这是别人参考的

  10. 训练1-T

    一个正整数,如果它能被7整除,或者它的十进制表示法中某个位数上的数字为7,则称其为与7相关的数.求所有小于等于N的与7无关的正整数的平方和. 例如:N = 8,<= 8与7无关的数包括:1 2 ...