Pseudoforest

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1526    Accepted Submission(s): 580

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 
 

伪森林:

题意为有一个伪森林,即森林里的每棵树最多有一个环。

这题还算是不错的,最小生成树小变形,要用kruskal算法,边的排序要按值大到小排。

排完序后就可以按kruskal的做法做了,不过要多作一个circle[]数组来记录每棵树环的情况,如果该树有一个环了就不能再加环,并且如果一条边有两个点一个属于有环树,一个不属于有环树,那么并查集并的时候要讲第二个点并到第一个点中,这样可以避免多个环的情况。思路清晰后还是挺简单的。

 //546MS    1464K    1096 B    G++
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 10005
struct node{
int u,v,d;
}p[*N];
int set[N];
int circle[N];
int n,m;
int cmp(const void*a,const void*b)
{
return (*(node*)b).d-(*(node*)a).d;
}
int find(int x)
{
if(set[x]!=x) set[x]=find(set[x]);
return set[x];
}
int kruskal()
{
int ans=;
for(int i=;i<m;i++){
int tu=find(p[i].u);
int tv=find(p[i].v);
if(tu==tv){
if(!circle[tu]){
circle[tu]=;
ans+=p[i].d;
}
continue;
}
if(circle[tu] && circle[tv]) continue;
if(circle[tu]) set[tv]=tu;
else set[tu]=tv;
ans+=p[i].d;
}
return ans;
}
int main(void)
{
while(scanf("%d%d",&n,&m)!=EOF&&(n+m))
{
for(int i=;i<=n;i++) set[i]=i;
memset(circle,,sizeof(circle));
for(int i=;i<m;i++){
scanf("%d%d%d",&p[i].u,&p[i].v,&p[i].d);
}
qsort(p,m,sizeof(p[]),cmp);
printf("%d\n",kruskal());
}
}

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) To ...

  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 Constructing Roads (最小生成树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102 /************************************************* ...

随机推荐

  1. 北京Uber优步司机奖励政策(2月1日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  2. 北京Uber优步司机奖励政策(12月17日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  3. 几个常用的轻量级web服务

    Node.js 安装:npm install http-server 使用:hs命令,可启动以当前目前为webroot的8080端口web服务,也可指定端口 Python 安装:内置 使用:pytho ...

  4. 使用.net 更新word目录

    方案一.采用OpenXml(服务器不依赖Office组件) 在word生成的最后加上代码: using (WordprocessingDocument docx = WordprocessingDoc ...

  5. Unity初探之黑暗之光(1)

    Unity初探之黑暗之光(1) 1.镜头拉近 public float speed=10f;//镜头的移动速度 ;//镜头的结束位置 // Update is called once per fram ...

  6. Java学习笔记-13.创建窗口和程序片

    1.init()方法:程序片第一次被创建,初次运行初始化程序片时调用. start()方法:每当程序片进入web浏览器中,并且允许程序片启动他的常规操作时调用(特殊的程序片被stop()关闭):同样在 ...

  7. Java学习笔记-10.io流

    1.输入流,只能从中读取数据,而不能向其写出数据.输出流,只能想起写入字节数据,而不能从中读取. 2.InputStream的类型有: ByteArrayInputStream 包含一个内存缓冲区,字 ...

  8. Python数据分析实战-Boston Public Schools GEO数据分析-Part1

    项目目标: Boston Public Schools Geo数据是来自于Boston地区的公共学校的数据,具体描述了学校的坐标,名字,类型等.基于此数据,我们可以学习一些基本的Python数据分析的 ...

  9. 【转】jQuery最佳实践

    上周,我整理了<jQuery设计思想>. 那篇文章是一篇入门教程,从设计思想的角度,讲解"怎么使用jQuery".今天的文章则是更进一步,讲解"如何用好jQu ...

  10. NFS服务搭建使用

    需求:由于线上业务有一些数据存在了Redis数据库和mysql数据库中了,导致了数据较大迁移起来比较麻烦,所以准备搭建NFS来做WEB的共享磁盘,存储这些数据. 服务端搭建: 查看本机关于nfs的包 ...