HDU 5438 Ponds

22 篇文章 0 订阅
17 篇文章 0 订阅

Ponds

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


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

简单的搜索题目。每次将度为1的池塘删掉,问最后剩余的池塘中,相连池塘数为奇数的权值和。

每次对度为1的点进行搜索,将对应边删除并将相连点的度减一,最后遍历池塘,对度不为0的池塘进行搜索并记录结果。


#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;

struct edge
{
    int v;
    int next;
}e[200010];

int d[10010];

struct node
{
    int u;
    int w;
    friend bool operator < (node a ,node b)
    {
        return d[a.u]>d[b.u];
    }
}N[10010];

int head[10010];
int tol,n,m;
long long ans;
bool vis[200010],flag[10010];

void addedge(int u,int v)
{
    e[tol].v=v;
    e[tol].next=head[u]++;
    head[u]=tol++;
    e[tol].v=u;
    e[tol].next=head[v]++;
    head[v]=tol++;
    return ;
}

long long bfs(int st,int sum)
{
    long long num=N[st-1].w;
    queue<int >q;
    q.push(st);
    //cout<<st<<" ";
    while(q.size())
    {
        st=q.front();
        q.pop();
        for(int i=head[st];i!=-1;i=e[i].next)
        {
            if(!vis[i]&&!flag[e[i].v])
            {
                //cout<<e[i].v<<"*";
                sum++;
                num+=N[e[i].v-1].w;
                q.push(e[i].v);
                vis[i]=true ;
                vis[i^1]=true ;
                flag[e[i].v]=true ;
            }
        }
    }
    if(sum&1)
    return num;
    else return 0;
}

void DFS(int st)
{
    for(int i=head[st];i!=-1;i=e[i].next)
    {
        if(!vis[i])
        {
            vis[i]=true ;
            vis[i^1]=true ;
            d[st]--;
            d[e[i].v]--;
            if(d[e[i].v]==1)
            DFS(e[i].v);
            return ;
        }
    }
    return ;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
        scanf("%d",&N[i].w),d[i+1]=0,N[i].u=i+1;
        tol=0;
        ans=0;
        memset(head,-1,sizeof(head));
        while(m--)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            d[u]++;
            d[v]++;
            addedge(u,v);
        }
        memset(vis,false ,sizeof(vis));
        memset(flag,false ,sizeof(flag));
        for(int i=1;i<=n;i++)
        {
            if(d[i]==1)
            {
                DFS(i);
            }
        }
        for(int i=1;i<n;i++)
        {
            if(d[i]&&!flag[i])
            ans+=bfs(i,1);
        }
        cout<<ans<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值