hdu 3367 Pseudoforest (最大生成树 最多存在一个环)

题目链接: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[100010];
int parent[10010],n,m,vis[10010];  // vis数组用来标记是否形成环 
bool cmp(point a, point b)
{
    return a.l > b.l;              // 从大到小排列 
}

int find (int x)
{
    int s,tmp;
    for (s = x; parent[s] >= 0; 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 = 0,max = 0;
    sort(p,p+m,cmp);
    memset(vis,0,sizeof(vis));
    memset(parent,-1,sizeof(parent));
    for (int i = 0; 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] = 1;
            max += p[i].l;
            Union(u,v);
        }
        else if(!vis[u] || !vis[v])         // 在同一连通分量内且有一个或者两个都没形成环   合并且标记 
        {
            vis[u] = vis[v] = 1;
            max += p[i].l;
            Union(u,v);
        }
    }
    return max;
}
int main ()
{
    while (scanf("%d%d",&n,&m),n+m!=0)
    {
        for (int i = 0; i < m; i ++)
            scanf("%d%d%d",&p[i].u,&p[i].v,&p[i].l);
        printf("%d\n",kruskal());
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/yoke/p/6520080.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值