• HDU 5438 Ponds


    Ponds

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


    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(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.
     
    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

    解题:拓扑排序

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 const int maxn = 210000;
     5 struct arc{
     6     int to,next;
     7     arc(int x = 0,int y = -1){
     8         to = x;
     9         next = y;
    10     }
    11 }e[600000];
    12 LL a[maxn];
    13 int tot,n,m,head[maxn],du[maxn];
    14 void add(int u,int v){
    15     e[tot] = arc(v,head[u]);
    16     head[u] = tot++;
    17 }
    18 queue<int>q;
    19 bool del[maxn];
    20 void topsort(){
    21     memset(del,false,sizeof del);
    22     while(!q.empty()) q.pop();
    23     for(int i = 1; i <= n; ++i)
    24     if(du[i] <= 1) {q.push(i);del[i] = true;}
    25     while(!q.empty()){
    26         int u = q.front();
    27         q.pop();
    28         for(int i = head[u]; ~i; i = e[i].next){
    29            if(!del[e[i].to] && --du[e[i].to] <= 1){
    30                 del[e[i].to] = true;
    31                 q.push(e[i].to);
    32             }
    33         }
    34     }
    35 }
    36 LL bfs(int u){
    37     while(!q.empty()) q.pop();
    38     q.push(u);
    39     bool odd = false;
    40     LL ret = 0;
    41     del[u] = true;
    42     int cnt = 0;
    43     while(!q.empty()){
    44         u = q.front();
    45         q.pop();
    46         cnt++;
    47         ret += a[u];
    48         for(int i = head[u]; ~i; i = e[i].next){
    49             if(del[e[i].to]) continue;
    50             del[e[i].to] = true;
    51             q.push(e[i].to);
    52         }
    53     }
    54     return (cnt&1)?ret:0;
    55 }
    56 int main(){
    57     int kase,u,v;
    58     scanf("%d",&kase);
    59     while(kase--){
    60         scanf("%d%d",&n,&m);
    61         memset(head,-1,sizeof head);
    62         memset(du,0,sizeof du);
    63         tot = 0;
    64         for(int i = 1; i <= n; ++i)
    65             scanf("%I64d",a + i);
    66         for(int i = 0; i < m; ++i){
    67             scanf("%d%d",&u,&v);
    68             add(u,v);
    69             add(v,u);
    70             du[u] += 1;
    71             du[v] += 1;
    72         }
    73         topsort();
    74         LL ret = 0;
    75         for(int i = 1; i <= n; ++i)
    76             if(!del[i]) ret += bfs(i);
    77         printf("%I64d
    ",ret);
    78     }
    79     return 0;
    80 }
    View Code
  • 相关阅读:
    86. Partition List
    2. Add Two Numbers
    55. Jump Game
    70. Climbing Stairs
    53. Maximum Subarray
    64. Minimum Path Sum
    122. Best Time to Buy and Sell Stock II
    以场景为中心的产品设计方法
    那些产品经理犯过最大的错
    Axure教程:如何使用动态面板?动态面板功能详解
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4804883.html
Copyright © 2020-2023  润新知