zoukankan      html  css  js  c++  java
  • 2015 ACM/ICPC Asia Regional Changchun Online Pro 1002 Ponds(拓扑排序+并查集)

    Ponds

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


    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
     
    先把边存起来,度计算出来,按照拓扑序删点(单点要注意,这里wa了一次),然后扫描一遍边,有删掉的点就忽略,用并查集维护连通分量的结点数和总权值。
     
    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 1e4+5,maxm = 2e5+10;
    int n,m;
    int val[maxn];
    int head[maxn],nxt[maxm],to[maxm];
    int deg[maxn],ecnt;
    bool rmvd[maxn];
    
    void addEdge(int u,int v)
    {
        to[ecnt] = v;
        nxt[ecnt] = head[u];
        head[u] = ecnt++;
        deg[u]++;
    }
    
    void topo()
    {
        queue<int> q;
        for(int i = 1; i <= n; i++){
            if(deg[i] <= 1){
                rmvd[i] = true;
                q.push(i);
            }
        }
        while(q.size()){
            int u = q.front(); q.pop();
            for(int i = head[u]; ~i; i = nxt[i]){
                int v = to[i];
                if(!rmvd[v] && --deg[v] == 1){
                    q.push(v); rmvd[v] = true;
                }
            }
        }
    }
    long long sum[maxn];
    int pa[maxn],cnt[maxn];
    int fdst(int x) { return x==pa[x]?x:pa[x]=fdst(pa[x]); }
    
    int main()
    {
        //freopen("in.txt","r",stdin);
        int T; scanf("%d",&T);
        while(T--){
            scanf("%d%d",&n,&m);
            for(int i = 1; i <= n; i++) scanf("%d",val+i);
            memset(head,-1,sizeof(head));
            memset(deg,0,sizeof(deg));
            memset(rmvd,0,sizeof(rmvd));
            ecnt = 0;
            for(int i = 0; i < m; i++){
                int u,v; scanf("%d%d",&u,&v);
                addEdge(u,v); addEdge(v,u);
            }
            topo();
            for(int i = 1; i <= n; i++) pa[i] = i,sum[i] = val[i],cnt[i] = 1;
            for(int i = 0,M = 2*m; i < M; i += 2){
                int u = to[i], v = to[i^1];
                if(!rmvd[u] && !rmvd[v]){
                    int a = fdst(u), b = fdst(v);
                    if(a != b){
                        pa[a] = b;
                        sum[b] += sum[a];
                        cnt[b] += cnt[a];
                    }
                }
            }
            long long ans = 0;
            for(int i = 1; i <= n ;i++){
                int f = fdst(i);
                if(!rmvd[f]){
                    if(cnt[f]&1){
                        ans += sum[f];
                    }
                    rmvd[f] = true;
                }
            }
            printf("%I64d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    [快捷键的使用] IntelliJ IDEA 将数据库里面的表转化为对象
    Mybatis事物浅谈
    CentOS7系统局域网内配置本地yum源解决cannot find a valid baseurl for repo
    CentOS6.5系统解决中文乱码问题
    tomcat启动报错:Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
    zabbix3.4.8中提示host [4gronghe_110] not found
    CentOS7.6系统安装详解(含真机装系统的采坑之旅)!
    windows server 2012 R2修改默认远程端口
    两个div并排,右边div固定宽度,左边宽度自适应
    两个div并排,左边div固定宽度,右边宽度自适应
  • 原文地址:https://www.cnblogs.com/jerryRey/p/4804911.html
Copyright © 2011-2022 走看看