• HDU 5438 Ponds


    Ponds

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


    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
     
    Source
    题目大意就是 给出每个点的价值和图 然后删除依次删除读数小于2的顶点,最后剩下的奇数点的联通分量的价值和。
    题目的意思第一次没看懂,wa了好几发.
    比如这组数据
    1
    8 8
    1 1 1 1 1 1 1 1
    1 2
    2 3
    3 1
    4 5
    5 6
    6 7
    7 4
    7 8
     
    答案是 3
    拓扑排序的思想+dfs判断奇数顶点数的联通块
    /* ***********************************************
    Author        :pk28
    Created Time  :2015/9/13 19:18:07
    File Name     :4.cpp
    ************************************************ */
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <stdio.h>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <iomanip>
    #include <list>
    #include <deque>
    #include <stack>
    #define ull unsigned long long
    #define ll long long
    #define mod 90001
    #define INF 0x3f3f3f3f
    #define maxn 10005
    #define cle(a) memset(a,0,sizeof(a))
    const ull inf = 1LL << 61;
    const double eps=1e-5;
    using namespace std;
    
    bool cmp(int a,int b){
        return a>b;
    }
    int n,m;
    struct node{
        int v,next;
    
    }edge[maxn*20];
    
    int l,pre[maxn];
    int w[maxn];
    int du[maxn];
    int vis[maxn];
    
    void add(int u,int v){
        edge[l].v=v;
        edge[l].next=pre[u];
        pre[u]=l++;
    }
    queue<int>q;
    void init(){
        memset(pre,-1,sizeof pre);
        cle(du);
        l=0;
        cle(vis);
        cle(w);
        while(!q.empty())q.pop();
    }
    ll sum,ans,cnt;
    void dfs(int u){
        for(int i=pre[u];i+1;i=edge[i].next){
            int v=edge[i].v;
            if(!vis[v]){
                vis[v]=1;
                cnt++;
                sum+=w[v];
                dfs(v);
            }
        }
    }
    int main()
    {
        #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        #endif
        //freopen("out.txt","w",stdout);
        int t,x,y;
        cin>>t;
        while(t--){
            init();
            scanf("%d %d",&n,&m);
            for(int i=1;i<=n;i++)scanf("%d",&w[i]);
            for(int i=1;i<=m;i++){
                scanf("%d%d",&x,&y);
                add(x,y);
                add(y,x);
                du[x]++;
                du[y]++;
            }
            for(int i=1;i<=n;i++){
                if(du[i]<2){
                    q.push(i);
                }
            }
            while(!q.empty()){
                int tmp=q.front();
                vis[tmp]=1;
                q.pop();
                for(int i=pre[tmp];i+1;i=edge[i].next){
                     int v=edge[i].v;
                    du[v]--;
                    if(du[v]<2&&!vis[v])q.push(v);
                }
            }
            
            sum=0;cnt=0;
            ans=0;
            for(int i=1;i<=n;i++){
                if(!vis[i]){
                    dfs(i);
                    if(cnt&1) ans+=sum;
                    cnt=0;
                    sum=0;
                }
            }
            printf("%I64d
    ",ans);
        }
        return 0;
    }
     
     

     

  • 相关阅读:
    不知道下一步该怎么走
    我这里下雪了
    第一次用到锁,解决多线程访问的问题
    这2年
    《软件需求》阅读笔记06和jupyterLab学习第二天
    《软件需求》阅读笔记05和jupyterLab学习第一天
    《软件需求》阅读笔记04和anaconda的安装和配置
    《软件需求》阅读笔记03和mapreduce数据清洗
    day10 echart图表联动+周总结+《软件需求》阅读笔记02
    day9 完成统计图表和《软件需求》阅读笔记01
  • 原文地址:https://www.cnblogs.com/pk28/p/4805426.html
Copyright © 2020-2023  润新知