题目链接: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. [USACO19FEB]Cow Dating

    Luogu5242 通过观察数据,我们可以发现,右端点的取值是单调递增的.于是,我们可以极限一波,用一个双指针法,类似于队列. 右端点的取值满足以下公式: (1-p1)(1-p2)..(1-pn) * ...

  2. 基础篇:6.3)形位公差-标注 Mark

    本章目的:了解形位公差是如何标注的,及其意义: 1.形位公差框格 Feature Control Frames 2.被测要素的标注(两国标准不同) 2.1 中国GB标准 — 形位公差框格通过用带箭头的 ...

  3. [转] 设置linux时间为网络时间

    [From] https://blog.csdn.net/weixin_35852328/article/details/79506453 Linux的时间分为System Clock(系统时间)和R ...

  4. vue相关问题在工作中的问题及ui组件及html样式搭建相关网站下载资源

    https://youzan.github.io/vant/#/zh-CN/nav-bar http://www.builive.com/docs/api/index.html    bui框架BUI ...

  5. Java学习之路(一):日常第一课,认识JAVA

    Java的介绍 语言的起源 Java是SUN(Stanford University Network 斯坦福大学网络公司) 1995年推出的一门高级编程语言. Java名称的来源: Java最初是被命 ...

  6. Selenium+excel实现参数化自动化测试

    使用到的技术:POI对excel的解析.selenium自动化测试.junit 测试用例:登陆www.1905.com执行登陆-退出的操作 执行步骤: 1.首先创建一个excel,里面有用户名和密码列 ...

  7. 在页面中嵌入svg的几种方法

    //在页面中嵌入svg的方法1:使用 <embed> 标签<embed> 标签被所有主流的浏览器支持,并允许使用脚本.注释:当在 HTML 页面中嵌入 SVG 时使用 < ...

  8. Android表格布局之设置边框

    Android表格布局本身没有边框,不过可以通过背景色的设置可以实现表格边框的显示. 首先可以设置TableRow的背景色,然后设置内容的背景色.根据它们的颜色差就出现了边框.只要微调Content与 ...

  9. 兼容IE6-9,FF,Chrome的box-shadow效果(纯CSS)

    昨天由于工作关系,遇上了这个问题,苦恼一日无解——残念. 所幸终于在今天早上得到了解决,遗憾的是灵活性不够强,不能根据内容自适应,要配合JS才能达到自适应效果 不过总结到这里已经很满意了,毕竟规律已经 ...

  10. SpringMVC入门(一)

    非注解的SpringMVC 1.创建一个web工程 2.导入工程需要的jar包 3.配置SpringMVC的前端控制器  前端控制器(DispatcherAdapter)需要在web.xml文件中进行 ...