zoukankan      html  css  js  c++  java
  • 2015长春网络赛 —— B. Ponds (拓扑排序删点+DFS)

    题目描述

    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(1≤T≤30) 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(1≤p≤104) which represents the number of ponds she owns, and the other is the number m(1≤m≤105) which represents the number of pipes.
    The next line contains p numbers v1,…,vp, where vi(1≤vi≤108) 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.

    Samples
    Input Copy
    1
    7 7
    1 2 3 4 5 6 7
    1 4
    1 5
    4 5
    2 3
    2 6
    3 6
    2 7
    Output
    21
    Source
    2015 ACM/ICPC Asia Regional Changchun Online

    题意:

    n点m边无向图,问将度数<2的点删除后,剩余的由奇数个点构成的连通块的权值之和。

    思路:

    拓扑排序删点,然后DFS。

    代码:

    #pragma GCC optimize(3)
    #pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
    #pragma GCC optimize(2)
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<ll,ll>PLL;
    typedef pair<int,int>PII;
    typedef pair<double,double>PDD;
    #define I_int ll
    inline ll read()
    {
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    char F[200];
    inline void out(I_int x) {
        if (x == 0) return (void) (putchar('0'));
        I_int tmp = x > 0 ? x : -x;
        if (x < 0) putchar('-');
        int cnt = 0;
        while (tmp > 0) {
            F[cnt++] = tmp % 10 + '0';
            tmp /= 10;
        }
        while (cnt > 0) putchar(F[--cnt]);
        //cout<<" ";
    }
    ll ksm(ll a,ll b,ll p){ll res=1;while(b){if(b&1)res=res*a%p;a=a*a%p;b>>=1;}return res;}
    const int inf=0x3f3f3f3f,mod=1e9+7;
    const ll INF = 0x3f3f3f3f3f3f3f3f;
    const int maxn=1e5+7,maxm=3e5+7,N=1e6+7;
    const double PI = atan(1.0)*4;
    
    int h[N],e[N],ne[N],idx,ans=N,n,m;
    int d[N];
    int val[maxn];
    queue<int>q;
    bool vis[maxn];
    
    void init(){
        idx=0;
        memset(h,-1,sizeof h);
        memset(vis,0,sizeof vis);
        memset(d,0,sizeof d);
    }
    
    void add(int a,int b){
        e[idx]=b,ne[idx]=h[a],h[a]=idx++;
    }
    
    void topsort(){
        for(int i=1;i<=n;i++)
            if(d[i]<2) q.push(i),vis[i]=1;
        while(!q.empty()){
            int t=q.front();q.pop();
            for(int i=h[t];~i;i=ne[i]){
                int j=e[i];
                if(vis[j]) continue;
                if(--d[j]<2) q.push(j),vis[j]=1;
            }
        }
    }
    
    ll siz=0,sum=0;
    
    void dfs(int u){
        siz++;
        vis[u]=1;
        sum+=val[u];
        for(int i=h[u];~i;i=ne[i]){
            int j=e[i];
            if(vis[j]) continue;
            dfs(j);
        }
    }
    
    int main(){
        int T=read();
        while(T--){
            init();
            n=read(),m=read();
            for(int i=1;i<=n;i++) val[i]=read();
            while(m--){
                int u=read(),v=read();
                add(u,v);add(v,u);
                d[u]++;d[v]++;
            }
            topsort();
            ll res=0;
            siz=sum=0;
            for(int i=1;i<=n;i++)
                if(!vis[i]){
                    siz=sum=0;
                    dfs(i);
                    if(siz%2) res+=sum;
                }
            out(res);puts("");
            while(!q.empty()) q.pop();
        }
        return 0;
    }
    
    
  • 相关阅读:
    java.lang.IllegalStateException: getOutputStream() has already been called for this response
    html5之table嵌入form表单布局(务必注意:table标签必须在form表单内部,不能再form表单外部!)
    两张图搞清楚Eclipse上的Web项目目录
    使用VS 2019发布.net core程序并部署到IIS的最新教程
    国外便宜虚拟主机有哪些?
    怎样收用电汇来收adsense的广告费呢
    程序员同性交友网站github在国内不能访问了
    直接引用MrAdvice.dll文件不能实现AOP拦截,教你1分钟解决这个问题
    VS2019打包WPF安装程序最新教程
    通过VS2017发布.net core程序并使用Web 部署到远程服务器最新教程
  • 原文地址:https://www.cnblogs.com/OvOq/p/14853117.html
Copyright © 2011-2022 走看看