hdu5438 Ponds

本文介绍了一种使用拓扑排序算法解决特殊图论问题的方法:即如何合理移除部分连接较少的池塘,最终计算剩余奇数个池塘组成的连通区域的价值总和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


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
 
这题可以用拓扑排序做,先删除所有能删除的点,然后再一遍dfs就行了。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 10040
int value[maxn],du[maxn],vis[maxn],n;
ll sum1,num1;

struct edge{
    int to,next,len;
}e[200050];
int first[maxn];
int q[1111111];

void topu()
{
    int i,j,front,rear,x,xx,v;
    front=1;rear=0;
    for(i=1;i<=n;i++){
        if(du[i]<=1){
            rear++;q[rear]=i;
            vis[i]=1;
        }
    }
    while(front<=rear){
        x=q[front];
        front++;
        for(i=first[x];i!=-1;i=e[i].next){
            v=e[i].to;
            if(vis[v])continue;
            du[v]--;
            if(du[v]<=1){
                rear++;
                q[rear]=v;
                vis[v]=1;
            }
        }
    }
}

void dfs(int root)
{
    int i,j,v;
    vis[root]=1;
    num1++;
    sum1+=value[root];
    for(i=first[root];i!=-1;i=e[i].next){
        v=e[i].to;
        if(!vis[v]){
            dfs(v);
        }
    }
}



int main()
{
    int m,i,j,T,tot,c,d;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(i=1;i<=n;i++){
            scanf("%d",&value[i]);
        }
        tot=0;
        memset(first,-1,sizeof(first));
        memset(du,0,sizeof(du));
        memset(vis,0,sizeof(vis));
        for(i=1;i<=m;i++){
            scanf("%d%d",&c,&d);
            du[c]++;
            du[d]++;
            tot++;
            e[tot].next=first[c];e[tot].to=d;
            first[c]=tot;

            tot++;
            e[tot].next=first[d];e[tot].to=c;
            first[d]=tot;

        }
        topu();
        ll sum=0;
        for(i=1;i<=n;i++){
            if(vis[i])continue;
            sum1=0;
            num1=0;
            dfs(i);
            if(num1&1)sum+=sum1;
        }
        printf("%lld\n",sum);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值