Pseudoforest

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

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
 
题意:
给出n个点和m条边,问最大生成树并且此生成树中有且只有一个环。
代码:
 //排序后每枚举一条边时判断两端点所在的集合中有没有环,如果不在一个集合中:都有环不能合并,否则可以合并。
//在一个集合中:都没有环,把这条边上就有环了。记住更新loop.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int fat[],loop[];//loop 标记有没有环。
struct Lu
{
int u,v,w;
};
bool cmp(Lu x,Lu y)
{
return x.w>y.w;
}
int find(int x)
{
if(fat[x]!=x)
fat[x]=find(fat[x]);
return fat[x];
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)&&(n+m))
{
for(int i=;i<n;i++)
fat[i]=i;
memset(loop,,sizeof(loop));
Lu L[];
int sum=;
for(int i=;i<m;i++)
{
scanf("%d%d%d",&L[i].u,&L[i].v,&L[i].w);
}
sort(L,L+m,cmp);
for(int i=;i<m;i++)
{
int x=find(L[i].u),y=find(L[i].v);
if(x!=y)
{
if(!loop[x]||!loop[y])
{
sum+=L[i].w;
fat[y]=x;
loop[x]=loop[x]|loop[y];
}
}
else if(!loop[x])
{
sum+=L[i].w;
loop[x]=;
}
}
printf("%d\n",sum);
}
return ;
}

*HDU3367 最小生成树的更多相关文章

  1. 最小生成树(Kruskal算法-边集数组)

    以此图为例: package com.datastruct; import java.util.Scanner; public class TestKruskal { private static c ...

  2. 最小生成树计数 bzoj 1016

    最小生成树计数 (1s 128M) award [问题描述] 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的最小生成树.(如果两颗最小生成树中至少有一 ...

  3. poj 1251 Jungle Roads (最小生成树)

    poj   1251  Jungle Roads  (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...

  4. 【BZOJ 1016】【JSOI 2008】最小生成树计数

    http://www.lydsy.com/JudgeOnline/problem.php?id=1016 统计每一个边权在最小生成树中使用的次数,这个次数在任何一个最小生成树中都是固定的(归纳证明). ...

  5. 最小生成树---Prim算法和Kruskal算法

    Prim算法 1.概览 普里姆算法(Prim算法),图论中的一种算法,可在加权连通图里搜索最小生成树.意即由此算法搜索到的边子集所构成的树中,不但包括了连通图里的所有顶点(英语:Vertex (gra ...

  6. Delaunay剖分与平面欧几里得距离最小生成树

    这个东西代码我是对着Trinkle的写的,所以就不放代码了.. Delaunay剖分的定义: 一个三角剖分是Delaunay的当且仅当其中的每个三角形的外接圆内部(不包括边界)都没有点. 它的存在性是 ...

  7. 最小生成树(prim&kruskal)

    最近都是图,为了防止几次记不住,先把自己理解的写下来,有问题继续改.先把算法过程记下来: prime算法:                  原始的加权连通图——————D被选作起点,选与之相连的权值 ...

  8. 最小生成树 prime poj1258

    题意:给你一个矩阵M[i][j]表示i到j的距离 求最小生成树 思路:裸最小生成树 prime就可以了 最小生成树专题 AC代码: #include "iostream" #inc ...

  9. 最小生成树 prime + 队列优化

    存图方式 最小生成树prime+队列优化 优化后时间复杂度是O(m*lgm) m为边数 优化后简直神速,应该说对于绝大多数的题目来说都够用了 具体有多快呢 请参照这篇博客:堆排序 Heapsort / ...

随机推荐

  1. PHP读写大“二进制”文件,不必申请很大内存(fopen、fread、fwrite、fclose)

    <?php /** * 读写大二进制文件,不必申请很大内存 * 只有读取到内容才创建文件 * 保证目录可写 * * @param string $srcPath 源文件路径 * @param s ...

  2. 【重点】Shell入门教程:流程控制(3)条件判断式的真假值

    之前曾提到,在Bash中什么是真什么是假,是以命令的结束状态是否为0来做判断.传回0,即为真:传回非0,即为假. 在Bash中,这种可以影响程序流程的式子,称为条件判断式.判断式的操作数分成“单元”及 ...

  3. 用python修改haproxy配置文件

    需求: 当用户输入域名的时候,显示出来下面的记录 当用户需要输入添加纪录的时候,添加到你需要的那个域名下面 global log 127.0.0.1 local2 daemon maxconn 256 ...

  4. Angularjs 双向绑定机制解析

    文章转自:http://www.2cto.com/kf/201408/327594.html AngularJs 的元素与模型双向绑定依赖于循环检测它们之间的值,这种做法叫做脏检测,这几天研究了一下其 ...

  5. Android编程容易犯的错误之一

    1.设置TextView的文本颜色 TextView tv; ... tv.setTextColor(R.color.white); 其实这样设置的颜色是 R.color.white的资源ID值所代表 ...

  6. iOS之九宫格图片

    照片 现在人们的生活越来越丰富了,很多美好的瞬间都定格在一张张色彩绚丽的照片上,或许把照片珍藏在相册里,或许通过社交软件分享给亲朋好友.那社交软件上的照片是以什么形式展现的呢?那么接下来就要说到九宫格 ...

  7. 会话控制:Cookie和session

    HTTP(超文本传输协议)定义了通过万维网(WWW)传输文本.图形.视频和所有其他数据所有的规则.HTTP是一种无状态的协议,说明每次请求的处理都与之前或之后的请求无关.虽然这种简化实现对于HTTP的 ...

  8. thinkphp中的setInc、setDec方法

    thinkphp中setInc.setDec方法 可用于统计字段(通常是数字类型的字段)的更新,例如积分,等级,登陆次数等 必须配合连贯操作where一起使用 $User = M("User ...

  9. 与你相遇好幸运,mocha接口测试

    var rest = require('restler');var assert = require("assert");var systemID;var userID; cons ...

  10. [译]:Orchard入门——使用WebMatrix管理Orchard网站

    原文链接:Working with Orchard in WebMatrix WebMatrix--微软一站式Web开发工具,包括网站的创建.编辑以及发布--不过现在微软更推荐VS code .Web ...