zoukankan      html  css  js  c++  java
  • hdu5438 Ponds

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


    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
     
    这题可以用拓扑排序做,先删除所有能删除的点,然后再一遍dfs就行了。
    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    #define inf 99999999
    #define pi acos(-1.0)
    #define maxn 10040
    int value[maxn],du[maxn],vis[maxn],n;
    ll sum1,num1;
    
    struct edge{
        int to,next,len;
    }e[200050];
    int first[maxn];
    int q[1111111];
    
    void topu()
    {
        int i,j,front,rear,x,xx,v;
        front=1;rear=0;
        for(i=1;i<=n;i++){
            if(du[i]<=1){
                rear++;q[rear]=i;
                vis[i]=1;
            }
        }
        while(front<=rear){
            x=q[front];
            front++;
            for(i=first[x];i!=-1;i=e[i].next){
                v=e[i].to;
                if(vis[v])continue;
                du[v]--;
                if(du[v]<=1){
                    rear++;
                    q[rear]=v;
                    vis[v]=1;
                }
            }
        }
    }
    
    void dfs(int root)
    {
        int i,j,v;
        vis[root]=1;
        num1++;
        sum1+=value[root];
        for(i=first[root];i!=-1;i=e[i].next){
            v=e[i].to;
            if(!vis[v]){
                dfs(v);
            }
        }
    }
    
    
    
    int main()
    {
        int m,i,j,T,tot,c,d;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%d",&n,&m);
            for(i=1;i<=n;i++){
                scanf("%d",&value[i]);
            }
            tot=0;
            memset(first,-1,sizeof(first));
            memset(du,0,sizeof(du));
            memset(vis,0,sizeof(vis));
            for(i=1;i<=m;i++){
                scanf("%d%d",&c,&d);
                du[c]++;
                du[d]++;
                tot++;
                e[tot].next=first[c];e[tot].to=d;
                first[c]=tot;
    
                tot++;
                e[tot].next=first[d];e[tot].to=c;
                first[d]=tot;
    
            }
            topu();
            ll sum=0;
            for(i=1;i<=n;i++){
                if(vis[i])continue;
                sum1=0;
                num1=0;
                dfs(i);
                if(num1&1)sum+=sum1;
            }
            printf("%lld
    ",sum);
        }
        return 0;
    }
    


  • 相关阅读:
    UI自动化测试入门一:Python3+Selenium环境搭建
    Linux下限制进程的CPU利用率
    Linux下模拟CPU占用100%
    Java接口自动化——OkHttp框架
    Java接口自动化——Before/After注解、Parameters注解和DataProvider注解
    Java接口自动化——TestNG快速开始
    《重构 改善既有代码的设计》读书笔记-坏代码的味道
    linux-脚本问题汇总
    RocketMq源码学习-消息存储
    linux-文本处理-grep
  • 原文地址:https://www.cnblogs.com/herumw/p/9464592.html
Copyright © 2011-2022 走看看