HDU 5438 Ponds

Ponds

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


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
 

Recommend
hujie   |   We have carefully selected several similar problems for you:   6242  6241  6240  6239  6238 
 

题意:
n个点,m条边,n个权,m条双向路。删去所有只与一个点连接的点,求剩下的所有奇数点数的环的权的和。
解题思路:
首先删点用拓扑排序,依次把所有度为1的点删去,剩下环,环中的每个点都与两个或两个以上的点连接着在。然后枚举每个点进行BFS,把奇数点数的环的权值加起来就行了。注意int会炸。
附上代码
#include<stdio.h>
#include<iostream>
#include<vector>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn = 1e4+5;

bool flag[maxn];
int n,m,num,val[maxn],du[maxn];
long long ans;
vector<int> v[maxn];

void top()
{
    queue<int> q;
    for(int i=1; i<=n; i++)
        if(du[i]==1||du[i]==0)
            q.push(i);
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        flag[x]=1;
        for(int i=0;i<v[x].size();i++)
        {
            int xx=v[x][i];
            if(!flag[xx])
            {
                du[xx]--;
                if(du[xx]==1||du[xx]==0)
                {
                    q.push(xx);
                }
            }
        }

    }
}
void bfs(int s)
{
    queue<int> q;
    q.push(s);
    flag[s]=1;
    num++;
    ans+=val[s];
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        for(int i=0; i<v[x].size(); i++)
        {
            int xx=v[x][i];
            if(!flag[xx])
            {
                ans+=val[xx];
                num++;
                flag[xx]=1;
                q.push(xx);
            }
        }
    }
}
int main()
{
    //freopen("in.txt","r",stdin);
    int t;
    cin>>t;
    while(t--)
    {
        for(int i=1; i<=n; i++)
            v[i].clear();
        memset(flag,0,sizeof(flag));
        memset(du,0,sizeof(du));
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; i++)
            scanf("%d",&val[i]);
        int a,b;
        for(int i=0; i<m; i++)
        {
            scanf("%d%d",&a,&b);
            if(a==b) continue;
            v[a].push_back(b);
            v[b].push_back(a);
            du[a]++;
            du[b]++;
        }
        top();
        long long sum=0;
        for(int i=1; i<=n; i++)
        {
            num=0;
            ans=0;
            if(!flag[i]) bfs(i);
            if(num%2) sum+=ans;
        }
        printf("%lld\n",sum);
    }
    return 0;
}
代码写的很乱。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值