zoukankan      html  css  js  c++  java
  • HDU 5438 拓扑排序+DFS

    Ponds

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


    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
     
    题意:一直去度数小于1的点,直到不能再去。
     
    题解:对图拓扑排序后,剩余部分DFS判断是否为奇数个,奇数个直接加权值。 坑点在下面标出。
     
     
    #include <iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    #include<algorithm>
    #include <vector>
    using namespace std;
    const int maxn = 1e4+5;
    vector<int> g[maxn];
    int v[maxn];
    int ans[maxn];
    int vis[maxn];
    int in[maxn];
    
    int t;
    int p,m;
    int cnt;
    void toposort()
    {
        queue<int> q;
        for(int i = 1; i<=p; i++)
            if(in[i] <= 1)   //还有度数为0的点
                q.push(i);
        while(!q.empty())
        {
            int temp = q.front();
            q.pop();
            ans[temp]++;
            for(int j = 0; j<g[temp].size(); j++)
            {
                int vv = g[temp][j];
                if(in[vv]<=1) continue;  //z这里有坑,否则就陷入死循环了。
                in[vv]--;
                if(in[vv] <= 1)
                    q.push(vv);
            }
        }
    }
    void dfs(int s,int& count,long long& sum)
    {
        if(ans[s]>0||vis[s]) return;
        vis[s] = 1;
        for(int i = 0; i<g[s].size(); i++)
        {
            int vv = g[s][i];
            if(ans[vv] == 0&& !vis[vv])
            {
                count++;
                sum += v[vv];
                dfs(vv,count,sum);
            }
        }
    }
    int main()
    {
        scanf("%d",&t);
        while(t--)
        {
            memset(ans,0,sizeof(ans));
            memset(in,0,sizeof(in));
            memset(vis,0,sizeof(vis));
            scanf("%d %d",&p,&m);
            for(int i = 1; i<=p; i++)
            {
                scanf("%d",&v[i]);
            }
            int l,r;
            for(int i = 1; i<=p; i++) g[i].clear();
            for(int i = 1; i<=m; i++)
            {
                scanf("%d %d",&l,&r);
                g[l].push_back(r);
                g[r].push_back(l);
                in[l]++;
                in[r]++;
            }
            toposort();
            long long sum = 0,sum1 = 0;
            int count = 0;
            for(int i = 1; i<=p; i++)
            {
                if(ans[i] == 0&&!vis[i])
                {
                    sum1 = v[i];
                    count = 1;
                    dfs(i,count,sum1);
                    if(count%2 == 1) sum += sum1;
                }
            }
            printf("%I64d
    ",sum);
    
        }
        return 0;
    }
    /*
    312
    7 10
    1 20 300 400 500 1000 5000
    1 2
    1 3
    1 4
    2 3
    2 4
    3 4
    5 6
    6 7
    5 7
    3 6
    
    2 1
    10 20
    1 2
    
    3 2
    10 100 1000
    1 2
    1 3
    
    3 1
    10 100 1000
    1 2
    */
  • 相关阅读:
    css3 径向渐变
    进度条-线性渐变
    echars 图表提示框自定义显示
    Android Ndef Message解析
    android 应用程序记录AAR
    android的nfc卡模拟开发
    《NFC开发实战详解》笔记
    1、Altium Designer 入门
    Stm32之通用定时器复习
    external与static的用法
  • 原文地址:https://www.cnblogs.com/littlepear/p/5668026.html
Copyright © 2011-2022 走看看