• hdu5438 Ponds


    Ponds

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


    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

     先说题意吧 :

    有n个 池塘 通过m个水管连通  (2个池塘之间只有一个水管) 现在要以走一些池塘(条件是这个池塘只连接了一个水管) 一直移到不能够再移除为止
    求在剩下的池塘中 由奇数个池塘组成的池塘的价值的和
     
    思路 : 去掉这些度数小于2的点后剩下的就都是强连通图了
     找度数大于1的数量为奇数的强联通分量的和(奇数 !!!   这道题没有自环和重边) 
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <cstdlib>
     4 #include <iostream>
     5 #include <algorithm>
     6 using namespace std;
     7 const int oo = 0x3f3f3f3f;
     8 const int N = 100005;
     9 typedef long long LL;
    10 struct da
    11 {
    12     int u, v, next;
    13 }as[N*4];
    14 int head[N], cnt, n, m, vis[N], in[N];
    15 /**< in记录每个点的度数  vis标记这个点是否处理过 val记录每个池塘的价值 */
    16 LL val[N];
    17 void add(int u, int v)
    18 {
    19     as[cnt].v = v;
    20     as[cnt].next = head[u];
    21     head[u] = cnt++;
    22 }
    23 void init()
    24 {
    25     memset(head, -1, sizeof(head));
    26     memset(in, 0, sizeof(in));
    27     cnt = 0;
    28 }
    29 void dfs(int u)/**< 将度数小于2的点以及这个点能到达的边的度数都减去1 */
    30 {
    31     vis[u] = 1;in[u]--;
    32     for(int i = head[u]; i != -1; i = as[i].next)
    33     {
    34         int v = as[i].v;
    35         if(vis[v]) continue;/**< 因为是无向图 有1-2 就有2-1 避免通一条边多次处理 */
    36         in[v]--;
    37         if(in[v] < 2)/**< 如果这个点的度数小于2 处理这个点的连边*/
    38            dfs(v);
    39     }
    40 }
    41 void dfs1(int u, LL &num, LL &sum)/**< 强连通的池塘的价值和 */
    42 {
    43     vis[u] = 1;
    44     num++; sum += val[u];
    45     for(int i = head[u]; i != -1; i = as[i].next)
    46     {
    47         int v = as[i].v;
    48         if(vis[v]) continue;
    49         if(in[v] < 2) continue;
    50         dfs1(v, num, sum);
    51     }
    52 }
    53 LL solve()
    54 {
    55     int i;
    56     memset(vis, 0, sizeof(vis));
    57     for(i = 1; i <= n; i++)
    58     {
    59         if(vis[i] == 0 && in[i] < 2)/**< 如果这个点没有遍历过 并且度数小于2 对他的连边处理 */
    60             dfs(i);
    61     }
    62     LL sum=0, ans = 0, QQ = 0;
    63     memset(vis, 0, sizeof(vis));
    64     for(i = 1; i <= n; i++)
    65     {
    66         QQ = 0, sum = 0;
    67         if(vis[i] == 0 && in[i] >= 2)
    68         {
    69            dfs1(i, sum, QQ);
    70            if(sum%2)ans += QQ;/**< 找出池塘个数为奇数的和 */
    71         }
    72     }
    73     return ans;
    74 }
    75 int main()
    76 {
    77     int T, i;
    78     scanf("%d", &T);
    79     while(T--)
    80     {
    81         init();
    82         scanf("%d %d", &n, &m);
    83         for(i = 1; i <= n; i++)
    84             scanf("%I64d", &val[i]);
    85         while(m--)
    86         {
    87             int a, b;
    88             scanf("%d %d", &a, &b);
    89             if(a == b)continue;
    90             add(a, b);
    91             add(b, a);
    92             in[a]++;
    93             in[b]++;
    94         }
    95         LL ans = solve();
    96         printf("%I64d
    ", ans);
    97     }
    98     return 0;
    99 }

     数据:

    8 8
    1 1 1 1 1 1 1 1
    1 2
    2 3
    3 4
    1 4
    5 6
    6 7
    5 7
    7 8

    3

    ——————————————————————————————————————————————————————

    7 8

    1 2 3 4 5 6 7
    1 2
    1 3
    2 4
    2 5
    4 5
    3 6
    3 7
    6 7

    28

    ————————————————————————————————————-

    3 2
    1 1 1
    1 2
    2 3

    0

    ————————————————————————————----————————————————————

    7 7
    1 1 1 1 1 1 1
    1 2
    1 3
    1 4
    1 5
    2 6
    2 7
    6 7

    3

    ——————————————————————————————————————————————————————————
    5 6
    1 1 1 1 1
    1 2
    1 3
    1 4
    1 5
    2 3
    4 5

    5

    7 8
    1 1 1 1 1 1 1
    1 2
    1 5
    2 3
    2 4
    3 4
    5 6
    5 7
    6 7

    7

  • 相关阅读:
    python-函数(下):递归、高阶函数
    Spring Hystrix 原理与使用详解
    Jmeter 添加kafka支持
    dnspod-sr 高性能DNS 服务器软件
    Jupyter精选资源合集
    spring 跨域CORS Filter
    TensorFlow 基础准备指导
    TortoiseGit SSH-key 免用户名密码验证
    node.js、npm 升级操作详解
    Maven -DskipTests和-Dmaven.test.skip=true的区别
  • 原文地址:https://www.cnblogs.com/PersistFaith/p/4808084.html
Copyright © 2020-2023  润新知