zoukankan      html  css  js  c++  java
  • HDU5438:Ponds(拓扑排序)

    Ponds

    Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
    Total Submission(s): 3204    Accepted Submission(s): 995


    Problem Description
    Betty owns a lot of ponds, some of them are connected with other ponds by pipes, and there will not be more than one pipe between two ponds. Each pond has a value v.

    Now Betty wants to remove some ponds because she does not have enough money. But each time when she removes a pond, she can only remove the ponds which are connected with less than two ponds, or the pond will explode.

    Note that Betty should keep removing ponds until no more ponds can be removed. After that, please help her calculate the sum of the value for each connected component consisting of a odd number of ponds
     
    Input
    The first line of input will contain a number T(1T30) which is the number of test cases.

    For each test case, the first line contains two number separated by a blank. One is the number p(1p104) which represents the number of ponds she owns, and the other is the number m(1m105) which represents the number of pipes.

    The next line contains p numbers v1,...,vp, where vi(1vi108) indicating the value of pond i.

    Each of the last m lines contain two numbers a and b, which indicates that pond a and pond b are connected by a pipe.
     
    Output
    For each test case, output the sum of the value of all connected components consisting of odd number of ponds after removing all the ponds connected with less than two pipes.
     
    Sample Input
    1
    7 7
    1 2 3 4 5 6 7
    1 4
    1 5
    4 5
    2 3
    2 6
    3 6
    2 7
     
    Sample Output
    21
     
    思路:拓扑排序,动态删边,注意结果要用long long类型存储,int 会爆
    #include <iostream>
    #include <queue>
    #include <vector>
    using namespace std;
    const int MAXN=10005;
    int n,m;
    vector<int> arc[MAXN];
    int val[MAXN],deg[MAXN],vis[MAXN];
    void topsort()
    {
        queue<int> que;
        for(int i=1;i<=n;i++)
        {
            if(!vis[i]&&deg[i]<=1)
            {
                que.push(i);
                vis[i]=1;
            }    
        }
        while(!que.empty())
        {
            int u=que.front();que.pop();
            for(int i=0;i<arc[u].size();i++)
            {
                int v=arc[u][i];
                if(!vis[v])
                {
                    deg[v]--;
                    if(deg[v]<=1)
                    {
                        que.push(v);
                        vis[v]=1;
                    }
                }
            }
        }
    }
    void dfs(int u,int& num,long long& sum)
    {
        sum+=val[u];
        num++;
        vis[u]=1;
        for(int i=0;i<arc[u].size();i++)
        {
            int v=arc[u][i];
            if(!vis[v])
            {
                dfs(v,num,sum);    
            }
        }
    }
    int main()
    {
        int T;
        cin>>T;
        while(T--)
        {
            cin>>n>>m;
            for(int i=1;i<=n;i++)    arc[i].clear();
            memset(deg,0,sizeof(deg));
            memset(vis,0,sizeof(vis));
            for(int i=1;i<=n;i++)
            {
                cin>>val[i];
            }
            for(int i=0;i<m;i++)
            {
                int u,v;
                cin>>u>>v;
                arc[u].push_back(v);
                arc[v].push_back(u);
                deg[u]++;
                deg[v]++;
            }
            topsort();    
            long long res=0;
            for(int i=1;i<=n;i++)
            {
                if(vis[i])    continue;
                int num=0;
                long long sum=0;
                dfs(i,num,sum);
                if(num&1)    res+=sum;
            }
            cout<<res<<endl;
        }
        return 0;
    }
  • 相关阅读:
    001-读书笔记-企业IT架构转型之道-阿里巴巴中台战略思想与架构实战-第一章 阿里巴巴集团中台战略引发的思考
    java-mybaits-011-mybatis-Interceptor-拦截器原理、统一赋值、计算耗时
    007-Redi-命令-脚本命令、链接命令、服务器命令、事务、HyperLogLog
    006-Redis 发布订阅
    005-redis-命令-4、无序集合,5、有序集合
    004-redis-命令-2、哈希操作命令,3、列表操作命令
    Ubuntu Server 14.04 --secure-file-priv error in MySql 解决方案
    Mysql Sql语句令某字段值等于原值加上一个字符串
    hdu 1281 二分图最大匹配
    hdu1045 DFS
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5653612.html
Copyright © 2011-2022 走看看