题目链接: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): 2870    Accepted Submission(s): 1126

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

题目大意:在一个无向图中,给定一些边的联通情况以及边的权值,求最大生成树(最多存在一条环路)。

解题思路:用kruskal的方法按照求最大生成树那样求的,只不过要加一个判断,就是判断两颗子树是够成环,

     如果各成环,就不能合并,如果只有其中一个成环或者都不成环,那么就可以合并,并对其进行标记。。。

AC代码:

20041234    2017-03-08 16:17:45    Accepted    3367    546MS    2668K    1272 B    G++

#include <stdio.h>
#include <string.h>
#include <algorithm> using namespace std; struct point
{
int u,v,l;
}p[];
int parent[],n,m,vis[]; // vis数组用来标记是否形成环
bool cmp(point a, point b)
{
return a.l > b.l; // 从大到小排列
} int find (int x)
{
int s,tmp;
for (s = x; parent[s] >= ; s = parent[s]);
while (s != x)
{
tmp = parent[x];
parent[x] = s;
x = tmp;
}
return s;
}
void Union(int A, int B)
{
int a = find(A), b = find(B);
int tmp = parent[a]+parent[b];
if (parent[a] < parent[b])
{
parent[b] = a;
parent[a] = tmp;
}
else
{
parent[a] = b;
parent[b] = tmp;
}
}
int kruskal()
{
int sum = ,max = ;
sort(p,p+m,cmp);
memset(vis,,sizeof(vis));
memset(parent,-,sizeof(parent));
for (int i = ; i < m; i ++)
{
int u = find(p[i].u), v = find(p[i].v);
if (u != v)
{
if (vis[u] && vis[v]) continue; // 如果两棵子树,各自能够形成一个环,则不合并
if (vis[u] || vis[v]) // 如果只有其中一个形成环,或者两个都没形成环,合并同时标记
vis[u] = vis[v] = ;
max += p[i].l;
Union(u,v);
}
else if(!vis[u] || !vis[v]) // 在同一连通分量内且有一个或者两个都没形成环 合并且标记
{
vis[u] = vis[v] = ;
max += p[i].l;
Union(u,v);
}
}
return max;
}
int main ()
{
while (scanf("%d%d",&n,&m),n+m!=)
{
for (int i = ; i < m; i ++)
scanf("%d%d%d",&p[i].u,&p[i].v,&p[i].l);
printf("%d\n",kruskal());
}
return ;
}

hdu 3367 Pseudoforest (最大生成树 最多存在一个环)的更多相关文章

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

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

  2. hdu 3367 Pseudoforest 最大生成树★

    #include <cstdio> #include <cstring> #include <vector> #include <algorithm> ...

  3. hdu 3367(与最大生成树无关。无关。无关。重要的事情说三遍+kruskal变形)

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

  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 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  6. hdu 3367 Pseudoforest(并查集)

    题意:有一种叫作Pseudoforest的结构,表示在无向图上,每一个块中选取至多包含一个环的边的集合,又称“伪森林”.问这个集合中的所有边权之和最大是多少? 分析:如果没有环,那么构造的就是最大生成 ...

  7. hdu 3367 Pseudoforest

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

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

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

  9. HDU 3367 (伪森林,克鲁斯卡尔)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3367 Pseudoforest Time Limit: 10000/5000 MS (Java/Oth ...

随机推荐

  1. 剑指offer——链表

    #include"stdio.h" #include"stdlib.h" #include"iostream" using namespac ...

  2. 《架构设计之[CAP定理]》读后感

    现在有许多互联网项目都是采用分布式结构进行部署.而cap定理是分布式系统中最近出的原则.所以对于哦我们来说,学习cap非常重要.CAP定理又称为布鲁尔定理.CAP定理是指对于一个分布式系统,不能同时满 ...

  3. idea常用快捷汇总

    目录 1.查询 2. 自动提示 3. 代码生成 4. 摆脱鼠标 5.源码阅读 1.查询 Shift+ Ctrl + F 全文搜索 Shift + Shift 文件名搜索 Ctrl + F 在当前文件进 ...

  4. redis cluster 部署过程

    一, 特点 高性能: 1.在多分片节点中,将16384个槽位,均匀分布到多个分片节点中 2.存数据时,将key做crc16(key),然后和16384进行取模,得出槽位值(0-16383之间) 3.根 ...

  5. 如何去除内联元素(inline-block元素)之间的间距

    <body><a href="http://www.baidu.com">百度</a><a href="http://www.i ...

  6. (转)MySQL open_files_limit相关设置

    http://www.cnblogs.com/zhoujinyi/archive/2013/01/31/2883433.html---------------------------MySQL ope ...

  7. 加载 Firefox 配置

    有小伙伴在用脚本启动浏览器时候发现原来下载的插件不见了,无法用 firebug在打开的页面上继续定位页面元素,调试起来不方便 .加载浏览器配置,需要用 FirefoxProfile(profile_d ...

  8. linux install tomcat

    折腾了好久,按照官网的安装流程安装了不止3次,发现还是不能成功,最终发现是linux机器本身的问题,因为我用的公司的virtual machine,可能是机器本身在一次迁移的过程当中出现了问题,导致了 ...

  9. 实现JS继承的几种方法

    总的来说,JS的继承大体上分为两种:借用构造函数方式和原型方式 首先,我们来看看借用构造函数方式的几种做法: //方式一function Person(name, sex){ this.name = ...

  10. 九度oj 1031 xxx定律 2009年浙江大学计算机及软件工程研究生机试真题

    题目1031:xxx定律 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5153 解决:3298 题目描述:     对于一个数n,如果是偶数,就把n砍掉一半:如果是奇数,把n变成 3*n ...