题目链接: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. luogu P2365 任务安排(FJOI2019 batch)

    洛谷传送门 FJOI 日常原题 $2333$(似乎还不如 SDOI2012 任务安排 $2333$) 显然考虑 $dp$,这个是经典的把未来的代价先计算的 $dp$,然后才是斜率优化 一开始想状态时一 ...

  2. Owin WebAPI上传文件

    Owin是微软出了几年的东东了,一直没时间学习.大概了解了下,是一个脱离IIS环境,快速搭建WebAPI服务的东西. 刚好想尝试下尽量脱离IIS创建简单快捷配置的项目,就是用了Nginx+Owin的模 ...

  3. SpringCloud---消息总线---Spring Cloud Bus

    1.概述 1.1 在微服务架构的系统中,我们通常会使用   轻量级的消息代理  来  构建一个共同的消息主题   让系统中所有微服务实例都连接上来: 由于  该主题中产生的消息  会被所有实例监听和消 ...

  4. 那些H5用到的技术(3)——屏幕场景滑动

    前言Swiper.js一些需要我们手动设置的参数排版元素需要设置position:absolute绝对元素定位swiperAnimate方法的使用动画播放完成之后的监听上滑提示屏幕适配的问题Anima ...

  5. Mac OS terminal终端常用命令

    基础概念 OS X 采用的Unix文件系统,所有文件都挂在跟目录“ /” 下面,所以不在要有Windows 下的盘符概念.比如什么“C:”你在桌面上看到的硬盘都挂在 /Volumes 下.比如接上个叫 ...

  6. WPF的ProgressBar进度条

    1. ProgressBar常用属性 1.1.  Minimum:进度条的最小值,一般为 0 1.2. Maximum:进度条的最大值,一般为100 或者是 某一个数, 如复制文件时,总文件数等 1. ...

  7. X-Frame-Options配置

    因为最近项目需要接入数据统计,其中一项功能需要开启iframe形式来加载页面,所以就开始研究一下iframe如何配置~~~ X-Frame-Options: 他的值有三个: (1)DENY --- 表 ...

  8. Amoeba+Mysql 实现读写分离

    About Amoeba Amoeba可译为阿米巴.变型虫Amoeba是一个开源项目,致力于Mysq的分布式数据库前端代理层Amoeba是一个以MySQL为底层数据存储,并对应用提供MySQL协议接口 ...

  9. SQL Server 2008中的MERGE(数据同步)

    OK,就像标题呈现的一样,SQL Server 2008中的MERGE语句能做很多事情,它的功能是根据源表对目标表执行插入.更新或删除操作.最典型的应用就是进行两个表的同步. 下面通过一个简单示例来演 ...

  10. php多进程实现 亲测

    php多进程实现 PHP有一组进程控制函数(编译时需要–enable-pcntl与posix扩展),使得php能在nginx系统中实现跟c一样的创建子进程.使用exec函数执行程序.处理信号等功能. ...