zoukankan      html  css  js  c++  java
  • Ponds

    Ponds

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


    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

     题意:有很多池塘,有的两个池塘间有管道连着。经费问题,要删除一些就连着一个管道的池塘。注意删除一个池塘之后,原本可能不用删除的,可能需要删除。4连着两个池塘2和3,2被删除后,4也要删除,因为连着4的管道由之前的2变成1个了。问剩下的在一个联通块里边池塘个数是奇数的,池塘价值之和。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<queue>
     5 #include<stack>
     6 #include<vector>
     7 #include<algorithm>
     8 
     9 using namespace std;
    10 
    11 #define maxn 10007
    12 
    13 vector<vector<int> > G;
    14 int used[maxn];
    15 int a[maxn*10], b[maxn*10];
    16 int f[maxn], v[maxn], sum[maxn], r[maxn];
    17 long long ans;
    18 
    19 int found(int x)
    20 {
    21     if(f[x] != x)
    22         f[x] = found(f[x]);
    23     return f[x];
    24 }
    25 
    26 int main()
    27 {
    28     int c, n, m;
    29     scanf("%d", &c);
    30     queue<int> Q;
    31 
    32     while(c--)
    33     {
    34         ans = 0;
    35         G.resize(maxn+2);
    36         G.clear();
    37         memset(used, 0, sizeof(used));
    38         memset(sum, 0, sizeof(sum));
    39         memset(a, 0, sizeof(a));
    40         memset(b, 0, sizeof(b));
    41 
    42         scanf("%d%d", &n, &m);
    43         for(int i = 1; i <= n; i++)
    44         {
    45             f[i] = i;
    46             r[i] = 0;
    47             scanf("%d", &v[i]);
    48         }
    49 
    50         for(int i = 0; i < m; i++)
    51         {
    52             scanf("%d%d", &a[i], &b[i]);
    53             r[a[i]]++, r[b[i]]++;
    54             G[a[i]].push_back(b[i]);
    55             G[b[i]].push_back(a[i]);
    56         }
    57         for(int i = 1; i <= n; i++)
    58             if(r[i] < 2)
    59                 Q.push(i);
    60 
    61         while(Q.size())
    62         {
    63             int u = Q.front();
    64             Q.pop();
    65             used[u] = 1;
    66             int len = G[u].size();
    67             for(int i = 0; i < len; i++)
    68             {
    69                 int v = G[u][i];
    70                 r[v]--;
    71                 if(r[v] < 2 && !used[v])
    72                     Q.push(v);
    73             }
    74         }
    75         for(int i = 0; i < m; i++)
    76         {
    77             if(!used[a[i]] && !used[b[i]])
    78             {
    79                 int u = found(a[i]);
    80                 int v = found(b[i]);
    81                 if(u != v)
    82                     f[v] = u;
    83             }
    84         }
    85         for(int i = 1; i <= n; i++)
    86             if(!used[i])
    87                 sum[found(i)]++;
    88 
    89         for(int i = 1; i <= n; i++)
    90         {
    91             if(!used[i] && sum[f[i]] % 2)
    92                 ans += v[i];
    93         }
    94         printf("%I64d
    ", ans);
    95     }
    96     return 0;
    97 }
  • 相关阅读:
    前端提示“页面正在载入”的功能实现
    JSON那些事
    如何让nodejs同步操作
    nodejs的一些局限
    《javascript高级程序设计》读书笔记——作用域
    svn教程
    从雷军谈小米中的一些思考
    云Vps安全设置
    不同服务器数据库之间的数据操作
    C#语言的Excel文件导入导出
  • 原文地址:https://www.cnblogs.com/Tinamei/p/4810881.html
Copyright © 2011-2022 走看看