zoukankan      html  css  js  c++  java
  • (最大生成树) hdu 3367

    Pseudoforest

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 1957    Accepted Submission(s): 756


    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
     
    题意:

    在图论中,如果一个森林中有很多连通分量,并且每个连通分量中至多有一个环,那么这个森林就称为伪森林。

     现在给出一个森林,求森林包含的最大的伪森林,

    直接按照求最大生成树的思想去搞啊

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<algorithm>
    #include<cstdlib>
    using namespace std;
    int n,m,fa[10005];
    bool vis[10005];
    struct node
    {
        int x,y,w;
    }e[100005];
    int find(int x)
    {
        if(x!=fa[x])
            fa[x]=find(fa[x]);
        return fa[x];
    }
    bool cmp(node a,node b)
    {
        return  a.w>b.w;
    }
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            if(n==0&&m==0)
                break;
            int ans=0;
            for(int i=0;i<n;i++)
                fa[i]=i,vis[i]=0;
            for(int i=1;i<=m;i++)
                scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].w);
            sort(e+1,e+1+m,cmp);
            for(int i=1;i<=m;i++)
            {
                int fx,fy;
                fx=find(e[i].x),fy=find(e[i].y);
                if(fx!=fy)
                {
                    if(!vis[fx]&&!vis[fy])
                    {
                        ans+=e[i].w;
                        fa[fx]=fy;
                    }
                    else if(!vis[fx]||!vis[fy])
                    {
                        ans+=e[i].w;
                        vis[fx]=vis[fy]=1;
                    }
                }
                else
                {
                    if(!vis[fx]&&!vis[fy])
                    {
                        ans+=e[i].w;
                        vis[fx]=vis[fy]=1;
                    }
                }
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    

      

  • 相关阅读:
    windows下查看端口占用情况及关闭相应的进程
    python学习中的一些“坑”
    python 中一些关键字的区别
    linux下配置Tomcat开机启动
    windows 下的python 安装pycrypto
    'redis-server' 不是内部或外部命令,也不是可运行的程序或批处理文件
    怎么学习代码
    crx文件不能安装,提示无效的安装包
    回调函数
    koa-router的作用
  • 原文地址:https://www.cnblogs.com/water-full/p/4510049.html
Copyright © 2011-2022 走看看