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优步司机奖励政策(1月11日~1月17日)

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

  2. jQuery File Upload 文件上传插件使用一 (最小安装 基本版)

    jQuery File Upload 是一款非常强大的文件上传处理插件,支持多文件上传,拖拽上传,进度条,文件验证及图片音视频预览,跨域上传等等. 可以说你能想到的功能它都有.你没想到的功能它也有.. ...

  3. apache Subversion 直接支持LDAP域群组

    如果你的Subversion已经用apache的ldap支持用户认证功能,你是否常常在想,既然都用ldap支持认证,为什么不直接支持域群组, 反而在authz文件里面一个一个的手工定义,或者有人用脚本 ...

  4. 阿里云服务器Linux系统安装配置ElasticSearch搜索引擎

    近几篇ElasticSearch系列: 1.阿里云服务器Linux系统安装配置ElasticSearch搜索引擎 2.Linux系统中ElasticSearch搜索引擎安装配置Head插件 3.Ela ...

  5. hdu1848Fibonacci again and again(sg函数)

    Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  6. python操作符及其循环语句(非常全)

    //2018.10.14 1. Windows + R可以直接进行运行cmd 2. Random.randint(a,b):产生a-b的任意一个整数,在IDLE里面运行时需要注意在前面写好调用impo ...

  7. HPUX 11.31 MC/SG恢复丢失的锁盘

    有时候由于一些特殊的原因,用户的cluster中的锁盘信息丢失,或者需要更换锁盘,只要执行一个命令就可以了. #cmdisklock reset /dev/vglock:/dev/disk/diskX ...

  8. leetcode7_C++整数反转

      给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 输出:  示例 2: 输入: - 输出: - 示例 3: 输入: 输出: 注意: 假设我们的环境只能存 ...

  9. Js全反选DataGrid

    // **************************************************************** // // function Trim(value) // -- ...

  10. Image控件显示以byte[]字节数组形式存在的图片

    工作中遇到了这样的一个问题.起初觉得很简单,获得了图片的byte[]后,可以将其转换成内存中的图片对象(如System.Drawing.Image),而后赋给页面的Image控件.尝试后才发现这样根本 ...