传送门:

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. MyBatis缓存通俗易懂

    1.1     mybatis缓存介绍 如下图,是mybatis一级缓存和二级缓存的区别图解: Mybatis一级缓存的作用域是同一个SqlSession,在同一个sqlSession中两次执行相同的 ...

  2. SPOJ:NSUBSTR - Substrings

    题面 字符串$ S \(最多包含\) 25 \(万个小写拉丁字母.我们将\) F(x) \(定义为长度为\) x \(的某些字符串出现在\) s \(中的最大次数.例如,对于字符串\) "a ...

  3. MyEclipse去除网上复制下来的代码带有的行号(使用正则表达式)

    一.正则表达式去除代码行号 作为开发人员,我们经常从网上复制一些代码,有些时候复制的代码前面是带有行号,如: MyEclipse本身自带有查找替换功能,并且支持正则表达式替换,使用正则替换就可以很容易 ...

  4. JavaScript自定义字符串格式化

    在JS中没有字符串拼接的方法,如过要使用怎么办呢?这时我们可以通过字符串的prototype可以自定义方法. <script> String.prototype.format = func ...

  5. along.js

    平时写代码用到的方法,就给封装了一下.需要的拿走不谢... 1.数组去重 并判断一个元素出现的次数 handle(str){ let arr=str.split('') var newarr=[]; ...

  6. 为什么推荐用ui-router替代ngRoute

    初学angularjs,第一个实例是官网的phoneCat,里面路由用的是ngRoute,后来看到别的用ui-router,觉得好奇,ui-route是什么呢?百度一些,得到如下解释: ui-rout ...

  7. Web开发须知的浏览器内幕 缓存与存储篇(2)

    本文禁止转载,由UC浏览器内部出品. 3. HTTP Cache 综述 HTTP Cache是完全按照IETF规范实现的,最新的RFC规范地址是 https://tools.ietf.org/html ...

  8. 使用WICleanup清理Windows Installer 冗余文件

    使用WICleanup清理Windows Installer 冗余文件 | 浏览:816 | 更新:2015-11-02 10:43 | 标签:Win7 Win10 1 2 3 4 5 6 7 分步阅 ...

  9. 一些 Mysql 维护命令

    ----------------------------------------------------------------------------使用mysql客户端程序------------ ...

  10. Centos7 下安装Apache2 + MySQL + PHP7

    Apache 1.安装Apache yum install httpd 2.设置服务器开机自动启动Apache systemctl enable httpd.service 若要验证是否自动启动可在重 ...