题目链接: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. try语句块和异常处理

    在C++中,异常处理包括: · throw表达式(throw expression) 异常检测部分使用throw表达式来表示它遇到了无法处理的问题.throw表达式抛出一个异常并把控制权转移到能处理该 ...

  2. [转] 打造基于CentOS7的xfce最简工作环境[转自smstong,在此记录一下]

    [From]https://blog.csdn.net/hejianlz/article/details/78976013 3 安装步骤 3.1 执行CentOS7 最小安装 去官网下载CentOS- ...

  3. redis的主从同步

    一.redis的主从操作流程 1. 准备三个redis配置文件 #进入redis的配置文件夹,准备好这几个文件,6379不用管,默认的,和这次操作无关 [root@qishi ~]# cd /etc/ ...

  4. jieba和文本词频统计

    ---恢复内容开始--- 一.结巴中文分词涉及到的算法包括: (1) 基于Trie树结构实现高效的词图扫描,生成句子中汉字所有可能成词情况所构成的有向无环图(DAG): (2) 采用了动态规划查找最大 ...

  5. oracle12c之三 控制PDB中CPU 资源使用

      CPU资源隔离 数据库中,不同的PDB对主机CPU资源使用要求不同,那么我们就可以使用CDB resourceplans来管理不同pdb对CPU资源的使用. CDB Resource Plans ...

  6. uvm_cmdline_processor

    无意中看到uvm_cmdline_processor,之前使用+UVM_TESTNAME也没深究,现在记录一下 内部调用脚本中的参数,通过使用uvm_cmdline_processor可以从脚本层级, ...

  7. 配置idea解决乱码

    在项目开发过程中,我们一般希望在修改完代码之后不重启项目即可提现出修改的结果,那么热部署项目就显得十分必要了.在idea中将项目热部署至tomcat中的方法如下: 首先打开tomcat配置界面,在se ...

  8. MySQL 查询结果分组 group by

    [group by {col_name | position} [ASC | DESC ]] 分组条件 [HAVING where_condition] HAVING 后面的条件必须出现在select ...

  9. ztree树的模糊搜索功能

    在做机场项目的时候,业务为一个input框,点击的时候出现一个下拉树,这个下拉树是所有的设备,由于设备太多,加上分了区域,为了更好的用户体验,设计一个模糊搜索的功能,方便用户进行选择 具体实现过程如下 ...

  10. cesium运行环境搭建

    cesiumjs是什么 一个世界级3D地球仪和地图的开源JavaScript库. 1.安装node.js 环境 1)下载node.js 官网:https://nodejs.org/en/ 下载完成后双 ...