2015ACM/ICPC Asia Regional Changchun Online /HDU 5438 图

                                               Ponds

                                  Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
                                       

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边,每次删除其连通边数小于2的点,直到不能删除,问你最后所剩点的价值为多少
//1085422276
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#include<bitset>
#include<set>
#include<vector>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,127,sizeof(a));
#define memfy(a) memset(a,-1,sizeof(a))
#define TS printf("111111\n");
#define FOR(i,a,b) for( int i=a;i<=b;i++)
#define FORJ(i,a,b) for(int i=a;i>=b;i--)
#define READ(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define mod 1000000007
#define maxn 1000006
inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
//****************************************

int n,m;
ll sum;
int s,vis[maxn*2],v[maxn],out[maxn];
vector<int >G[maxn];
void init()
{
    FOR(i,1,n)G[i].clear();
    mem(vis);
    mem(out);
}
void dfs(int x)
{
    vis[x]=1;
    s++;
    sum+=v[x];
    for(int i=0;i<G[x].size();i++)
    {
        if(!vis[G[x][i]])
        {
            dfs(G[x][i]);
        }
    }
}
int main()
{
    int T=read();
    while(T--)
    {
        init();
        scanf("%d%d",&n,&m);
        FOR(i,1,n)
        {
            scanf("%d",&v[i]);
        }
        int a,b;
        FOR(i,1,m)
        {
            scanf("%d%d",&a,&b);
            G[a].push_back(b);
            G[b].push_back(a);
            out[a]++;
            out[b]++;
        }
        ll ans=0;
        queue<int >q;
        FOR(i,1,n)
        {
            if(out[i]<=1)q.push(i);
            ans+=v[i];
        }
        while(!q.empty())
        {
            int k=q.front();
            q.pop();
            vis[k]=1;
            ans-=v[k];
            for(int i=0;i<G[k].size();i++)
            {
                out[G[k][i]]--;
               if(out[G[k][i]]<=1&&!vis[G[k][i]])q.push(G[k][i]);
            }
        }
       FOR(i,1,n)
       {
           sum=0; s=0;
           if(!vis[i])dfs(i);
           if(s%2==0)ans-=sum;
       }
       cout<<ans<<endl;
    }
    return 0;
}
代码

 

转载于:https://www.cnblogs.com/zxhl/p/4805292.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值