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. Linux进程间通信(九):数据报套接字 socket()、bind()、sendto()、recvfrom()、close()

    前一篇文章,Linux进程间通信——使用流套接字介绍了一些有关socket(套接字)的一些基本内容,并讲解了流套接字的使用,这篇文章将会给大家讲讲,数据报套接字的使用. 一.简单回顾——什么是数据报套 ...

  2. 解决VirtualBox只能安装32位系统的问题

    发现自己的笔记本(Thinkpad E440)里的 VirtualBox 只能安装 32位 的系统,如下图所示: 经过一番查资料,发现这玩意需要到BIOS里设置一下,方可安装 64位 系统,操作如下: ...

  3. Jenkins的使用

    参考: http://www.cnblogs.com/sunzhenchao/archive/2013/01/30/2883289.html

  4. LCTT 三岁啦

    导读 不知不觉,LCTT 已经成立三年了,对于我这样已经迈过四张的人来说,愈发的感觉时间过得真快.这三年来,我们 LCTT 经历了很多事情,有些事情想起来仍恍如昨日. 三年前的这一天,我的一个偶发的想 ...

  5. proj.4投影变换图示

  6. js框架设计1.2对象扩展笔记

    需要一个新的功能添加到我们的命名空间上.这方法在JS中被叫做extend或者mixin,若是遍历属性用一下1.1代码,则会遍历不出原型方法,所以1.2介绍的是mass Framework里的mix方法 ...

  7. nginx访问不了zabbix安装配置界面

    通过yum安装的php等其他各种软件,配置好后,html目录下面php可以解析,但是就是访问不到setup.php文件.后来各种查找,发现是setup解析错误 PHP Parse error:  sy ...

  8. poj1001_Exponentiation_java高精度

    Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 162918   Accepted: 39554 ...

  9. float 比较, 这是一个坑

    为了方便随机关键产品数据,做了一个随机值列的方案,列字段类型设置为float. 在测试的两个随机值的时候, 故意设置了几个随机值相同保存到数据库表中, 这样问题就出来了. 详细如下: 当进行小于比较的 ...

  10. 查看cpu的信息cat /proc/cpuinfo

    cat /proc/cpuinfo processor : vendor_id : GenuineIntel cpu family : model : model name : Intel(R) Co ...