传送门:

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. OpenStack IceHouse 部署 - 4 - 计算节点部署

    Nova计算服务(计算节点)  参考 本页内容依照官方安装文档进行,具体参见Configure a compute node(nova service) 前置工作 数据库 由于我们在Nova(计算管理 ...

  2. LinkedList实现队列存储结构

    package com.tercher.demo; import java.util.LinkedList; public class Queue { //用LinkedList 实现队列的数据存储结 ...

  3. 初学Node.js

    下载Node.js,官方网址:https://nodejs.org/en/download/ 可根据根据自己的电脑配置来下载相当于的Node.js 下载完成后使用Windows键+R 输入cmd 输入 ...

  4. web 开发人员必须学习的 3 门语言

    web 开发人员必须学习的 3 门语言:html  css  js HTML 定义了网页的内容 CSS 描述了网页的布局 JavaScript 网页的行为

  5. 远景平台开发者上线,专业API免费使用

    远景平台开发者上线,欢迎大伙围观使用. 在开发者中心你可以做什么? 1.管理你的应用,通过APPKEY获取在线API.使用云中的数据和地图. 2.学习API的使用,包含API参考和部分例子(目前例子很 ...

  6. C++学习笔记(5)----重载自增自减运算符

    自增运算符“++”和自减运算符“--”分别包含两个版本.即运算符前置形式(如 ++x)和运算符后置形式(如 x++),这两者进行的操作是不一样的.因此,当我们在对这两个运算符进行重载时,就必须区分前置 ...

  7. 移动端App开发 - 02 - iPhone/iPad/Android UI尺寸规范

    移动端app开发 - iPhone/iPad/Android UI尺寸规范 本笔记抛去无用的前期分析什么的,全是干货,简洁干练 本笔记不单独针对 ios 或者 Android,两种都介绍,当然我们实际 ...

  8. centos7安装lua语言环境

    Linux 上安装 Lua 安装非常简单,只需要下载源码包并在终端解压编译即可. 官网地址:http://www.lua.org/download.html 我这里安装的是:lua-5.3.0.tar ...

  9. Spring MVC基本配置和实现(三)

    Item public class Item { private Integer id; private String name; public Integer getId() { return id ...

  10. Linux 硬件RAID详解系统功能图

    RAID-0(条带模式) 特点: 在读写的时候可以实现并发,所以相对其读写性能最好,每个磁盘都保存了完整数据的一部分,读取也采用并行方式,磁盘数量越多,读取和写入速度越快. 因为没有冗余,一个硬盘坏掉 ...