HDU5438-搜索

Ponds

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


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条边的图,每个点有个权值,现在你要删除所有度<=1的点,删除一个点后与他连接的边也删除,问最后不能删除

后的图当中,每个奇数点数的连通图的权值和


题目思路:


对于删除度小于等于1的点,我们可以bfs来删除,首先建图时用个数数组in[i]记录与点i连接的边数,然后把度<=1的点入队,并且标记,然后bfs遍历图,当前点没有被标记时度数--,如果减后的度数为1就进队,标记,最后删除后再用一遍bfs遍历所有连通图,没有被标记的点的图,记录点数和权值,最后在加起来就是


AC代码:

#include<bits/stdc++.h>
using namespace std;

int in[10005],vis[10005];
long long v[10005];
vector<int>G[10005];
int n,m;
int num;
long long ans,res;

void bfs(int u)      //搜索连通图
{

    queue<int>q;
    q.push(u);
    vis[u] = 1;
    num++;
    res+=v[u];
    while(!q.empty())
    {
        u = q.front();
        q.pop();
        for(int i=0;i<G[u].size();i++)
        {
            int vv = G[u][i];
            if(vis[vv]==0)    //没有被标记的点
            {
                vis[vv] = 1;
                res+=v[vv];
                num++;
                q.push(vv);
            }
        }
    }

}

int main()
{
    int t;cin>>t;
    while(t--)
    {
        ans = 0;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)scanf("%lld",&v[i]),ans+=v[i],in[i] = 0,vis[i] = 0,G[i].clear();
        while(m--)
        {
            int u,v;scanf("%d%d",&u,&v);
            G[u].push_back(v);
            G[v].push_back(u);
            in[u]++;
            in[v]++;
        }
        queue<int>q;
        for(int i=1;i<=n;i++)
        {
            if(in[i]==0)ans-=v[i],vis[i] = 1;
            else if(in[i]==1)
            {
                q.push(i);
                vis[i] = 1;
            }
        }
        while(!q.empty())    //删点
        {
            int u = q.front();
            q.pop();
            for(int i=0;i<G[u].size();i++)
            {

                int vv = G[u][i];
                if(vis[vv]==0)
                {
                    in[vv]--;
                    if(in[vv]==1)
                    {
                        q.push(vv);
                        vis[vv] = 1;
                    }
                }
            }
        }
        ans = 0;
        for(int i=1;i<=n;i++)
        {
            if(vis[i]==0)
            {
                res = 0;
                num = 0;
                bfs(i);
                if(num%2==1)ans+=res;
            }
        }

        cout<<ans<<endl;
    }
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值