传送门:

http://acm.hdu.edu.cn/showproblem.php?pid=3367

Pseudoforest

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

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
 
大致题意:
给出一个图,要求出最大的pseudoforest,所谓pseudoforest就是指这个图的一个子图,这个子图的每个连通分量重最多一个环,而且这个子图的权值之和要求最大,这个就是所谓的伪森林
 
采用克鲁斯卡尔算法,但是有不同,每个连通分量允许有一个环,所以在进行合并两棵树的时候,要判断这两棵树是否有环,如果两棵树都有环的话,不能进行合并,如果只有一棵树有环,那么可以进行合并,然后标上记号,如果两个都没有环,直接合并就好,如果两点属于同一颗树上,那么要判断这棵树是否有环,如果没有环的话,允许有一个环,合并这两个点,然后标记
 
总结:
可以合并的情况:
1.两个点属于同一颗树,该树没有环
2.两个点属于两棵树,最多只有一颗树有环,或者两颗树都没有
 
代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define max_n 10005
#define max_m 100005
struct edge
{
int x,y;
int w;
};
edge e[max_m];
int cy[max_n];
int pa[max_n];
int sum;
bool cmp(edge a,edge b)
{
return a.w>b.w;
}
void make_set(int x)
{
pa[x]=x;
cy[x]=;
}
int find_set(int x)
{
if(x!=pa[x])
pa[x]=find_set(pa[x]);
return pa[x];
}
void union_set(int x,int y,int w)
{
int a=find_set(x);
int b=find_set(y);
if(a==b&&cy[a]==)//两点在同一颗树,且该树没有环
{
cy[a]=;
sum+=w;
}else if(a!=b&&(cy[a]==||cy[b]==))//两点在不同树上,两个树最多一个有环
{
sum+=w;
pa[a]=b;
if(cy[a]==)
cy[b]=cy[a];
}
}
int main()
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
sum=;
if(n+m==)
break;
for(int i=;i<n;i++)
make_set(i);
for(int i=;i<m;i++)
{
scanf("%d %d %d",&e[i].x,&e[i].y,&e[i].w);
}
sort(e,e+m,cmp);
for(int i=;i<m;i++)
{
union_set(e[i].x,e[i].y,e[i].w);
}
printf("%d\n",sum);
}
}

HDU 3367 (伪森林,克鲁斯卡尔)的更多相关文章

  1. HDU 1863 畅通工程 克鲁斯卡尔算法

    畅通工程 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  2. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  3. HDU 1233 还是畅通工程(模板——克鲁斯卡尔算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1233 题意描述: 输入n个城镇以及n*(n-1)/2条道路信息 计算并输出将所有城镇连通或者间接连通 ...

  4. hdu 1233 还是畅通project (克鲁斯卡尔裸题)

    还是畅通project                                              Time Limit: 4000/2000 MS (Java/Others)    M ...

  5. HDU 1879 继续畅通工程 (Prim(普里姆算法)+Kruskal(克鲁斯卡尔))

    继续畅通工程 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  6. hdu 1233(还是畅通project)(prime算法,克鲁斯卡尔算法)(并查集,最小生成树)

    还是畅通project Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  7. hdu 1679 The Unique MST (克鲁斯卡尔)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24152   Accepted: 8587 D ...

  8. hdu 3367(Pseudoforest ) (最大生成树)

    Pseudoforest Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  9. 【hdu3367】Pseudoforest(伪森林)

    http://acm.hdu.edu.cn/showproblem.php?pid=3367 题目大意 伪森林就是一个无向图,这个无向图有多个连通块且每个连通块只有一个简单环. 给你一个无向图,让你找 ...

随机推荐

  1. C# 字符串中英文对齐

    StringBuilder str = new StringBuilder(); str.AppendFormat( - Encoding.Default.GetBytes(); str.Append ...

  2. swiper动态改变滑动内容

    假设当前显示的是1,往左滑动一个递减1,往右滑动一个递增1 body下面添加如下代码 <div class="swiper-container temp"> <d ...

  3. eclipse调试 10个技巧

    先提三点 不要使用System.out.println作为调试工具 启用所有组件的详细的日志记录级别 使用一个日志分析器来阅读日志 1.条件断点 想象一下我们平时如何添加断点,通常的做法是双击行号的左 ...

  4. App更新之dialog数字进度条

    App更新之dialog数字进度条 前言:现在一般的Android软件都是需要不断更新的,当你打开某个app的时候,如果有新的版本,它会提示你有新版本需要更新.当有更新时,会弹出一个提示框,点击下载, ...

  5. 在 O(1) 时间删除链表结点(C 和 Python 实现)

    (说明:本博客中的题目.题目详细说明及参考代码均摘自 “何海涛<剑指Offer:名企面试官精讲典型编程题>2012年”) 题目 给定单向链表的头指针和一个结点指针,定义一个函数在 O(1) ...

  6. jQuery 插件封装的方法

    方式1.$.fn.xxx ==>针对元素添加方法: ;(function ($) { $.fn.m​​yPlugin = function () { //你自己的插件代码 }; })(jQuer ...

  7. c#之反射(Reflection)

    一.反射是什么 反射就是.Net FrameWork框架为我们提供的一个帮助类库,它可以读取我们通过编译后生成的dll和exe文件里面metadate的信息. 反射可以动态的加载dll和exe文件,动 ...

  8. byr面经两则

    人人,金山西山居,腾讯互娱,微信,网易游戏offer及面经 首先感谢师兄在两年前发的贴([天道酬勤] 腾讯.百度.网易游戏.华为Offer及笔经面经 ),这篇文章对我帮助很大. 我写这篇文章一是为了感 ...

  9. asp.net 服务器控件的 ID,ClientID,UniqueID 的区别

    1.简述 ID是设计的时候自己所指定的ID,是我们分配给服务器控件的编程标识符,我们常常使用this.controlid来寻找控件,那么这个controlid就是这里所说的ID. ClientID是由 ...

  10. 关于CATransform3D矩阵变换的简单解析

    关于CATransform3D矩阵变换的简单解析 效果图: 我能能够用上的CATransform3D其实很简单,并不复杂. CATransform3D有着4种东西我们可以设置. 1. 透视效果(由m3 ...