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


    传送门:

    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): 3242    Accepted Submission(s): 1273


    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
     
    大致题意:
    给出一个图,要求出最大的pseudoforest,所谓pseudoforest就是指这个图的一个子图,这个子图的每个连通分量重最多一个环,而且这个子图的权值之和要求最大,这个就是所谓的伪森林
     
    采用克鲁斯卡尔算法,但是有不同,每个连通分量允许有一个环,所以在进行合并两棵树的时候,要判断这两棵树是否有环,如果两棵树都有环的话,不能进行合并,如果只有一棵树有环,那么可以进行合并,然后标上记号,如果两个都没有环,直接合并就好,如果两点属于同一颗树上,那么要判断这棵树是否有环,如果没有环的话,允许有一个环,合并这两个点,然后标记
     
    总结:
    可以合并的情况:
    1.两个点属于同一颗树,该树没有环
    2.两个点属于两棵树,最多只有一颗树有环,或者两颗树都没有
     
    代码如下:
    #include<stdio.h>
    #include<stdlib.h>
    #include<algorithm>
    using namespace std;
    #define max_n 10005
    #define max_m 100005
    struct edge
    {
        int x,y;
        int w;
    };
    edge e[max_m];
    int cy[max_n];
    int pa[max_n];
    int sum;
    bool cmp(edge a,edge b)
    {
        return a.w>b.w;
    }
    void make_set(int x)
    {
        pa[x]=x;
        cy[x]=0;
    }
    int find_set(int x)
    {
        if(x!=pa[x])
            pa[x]=find_set(pa[x]);
        return pa[x];
    }
    void union_set(int x,int y,int w)
    {
        int a=find_set(x);
        int b=find_set(y);
        if(a==b&&cy[a]==0)//两点在同一颗树,且该树没有环
        {
            cy[a]=1;
            sum+=w;
        }else if(a!=b&&(cy[a]==0||cy[b]==0))//两点在不同树上,两个树最多一个有环
        {
            sum+=w;
            pa[a]=b;
            if(cy[a]==1)
                cy[b]=cy[a];
        }
    }
    int main()
    {
        int n,m;
        while(~scanf("%d %d",&n,&m))
        {
            sum=0;
            if(n+m==0)
                break;
            for(int i=0;i<n;i++)
                make_set(i);
            for(int i=0;i<m;i++)
            {
                scanf("%d %d %d",&e[i].x,&e[i].y,&e[i].w);
            }
            sort(e,e+m,cmp);
            for(int i=0;i<m;i++)
            {
                union_set(e[i].x,e[i].y,e[i].w);
            }
            printf("%d
    ",sum);
        }
    }
  • 相关阅读:
    DEDECMS之五 单页
    DEDECMS之六 网站地图、RSS地图
    DEDECMS之四 栏目调用
    DEDECMS之三 首页、列表页怎么调用文章内容
    DEDECMS之七 如何实现文章推荐排行榜
    centos6下安装dedecms
    C# 自动部署之附加数据库
    产品经理技能之BRD的笔记之菜鸟入门
    产品经理技能之MRD的笔记之一
    产品需求文档(PRD)的写作方法之笔记一
  • 原文地址:https://www.cnblogs.com/yinbiao/p/9202164.html
Copyright © 2020-2023  润新知