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;
    }
    

      

  • 相关阅读:
    ADO.NET Entity Framework之ObjectQuery
    高效byte[]和string互转的方法
    异步邮件发送
    ADO.NET Entity Framework之ObjectContext
    【转】Memcache and Mongodb
    一周工作总结一次SQL优化记录
    Ubuntu下有关Java和数据库的一些工作记录(一)
    自定义函数,替换字符串中指定位置的字符
    一次优化记录
    对比shrink和move
  • 原文地址:https://www.cnblogs.com/water-full/p/4510049.html
Copyright © 2011-2022 走看看