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;
    }
  • 相关阅读:
    gridview展示行号
    DateEdit如果开启Vista模式并显示日期+时间模式
    DevExpress GridView 添加和设置右键菜单
    C# WinForm实现粘贴图片到PictureBox及复制PictureBox中的图片
    dll反编译工具(ILSpy)的使用
    Dev的双击Gridview的DoubleClick
    SQL Server日期时间格式转换字符串详解
    LabelControl文本居中显示
    C# winform 判断click事件点击的是左键还是右键
    Winform窗体状态的判断及调用打开的窗体的方法
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5653612.html
Copyright © 2011-2022 走看看