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;
    }
  • 相关阅读:
    音频电路设计中的基本知识(-)
    Usart的单线半双工模式(stm32F10x系列)
    RTS与CTS的含义
    NetBIOS与Winsock编程接口
    debian下使用gitosis+gitweb搭建SSH认证的git服务器
    解决:无法将“Add-Migration”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次
    Windows Azure Storage Client Library 2.0 入门
    Windows Azure Table Storage 解决 Guid 查询问题
    EF 报【序列包含一个以上的元素】解决办法
    javascript技巧大全套
  • 原文地址:https://www.cnblogs.com/jerryRey/p/4804911.html
Copyright © 2011-2022 走看看