HDU 5438 Ponds(BFS+拓扑排序)【2015亚洲区长春站网赛B】

61 篇文章 0 订阅
36 篇文章 0 订阅

Ponds

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

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(1≤T≤30) 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(1≤p≤104) which represents the number of ponds she owns, and the other is the number m(1≤m≤105) which represents the number of pipes.

The next line contains p numbers v1,…,vp, where vi(1≤vi≤108) 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
2015 ACM/ICPC Asia Regional Changchun Online

拆除池塘的条件是:与该池塘相连的池塘的个数不能超过一个。
所以现在就得用拓扑排序把度数为0或一的池塘都拆了,但是为什么用拓扑排序呢,假如某个池塘u,它与池塘p,v都相连,这时假如p可以去除,那么p去除以后u也可以去除了,所以就用到拓扑排序了。
最后让成环换的池塘总价值,环有以下 要求:
1.环中池塘的个数大于等于3。
2.环中池塘的个数为奇数个。

下面是AC代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#define ll long long
using namespace std;
vector<ll>e[10005];

ll v[10005];
int deg[10005],vis[10005];

ll bfs(ll x)
{
    queue<ll>q;
    q.push(x);
    vis[x]=1;
    ll k=0;
    ll sum=v[x];
    while (!q.empty())
    {

        int s=q.front();
        q.pop(); //cout<<s<<endl;
        k++;
        for (int i=0; i<e[s].size(); i++)
        {
            if (!vis[e[s][i]]&&deg[e[s][i]]>=2)//删掉的点不可以加进来
            {
                sum+=v[e[s][i]];
                q.push(e[s][i]);
                vis[e[s][i]]=1;
            }
        }
    }
    if (k>=3&&k%2==1)
        return sum;
    else
        return 0;
}

int main()
{
    int t,n,m;
    scanf("%d",&t);
    while(t--)
    {
        memset(deg,0,sizeof(deg));
        memset(e,0,sizeof(e));
        memset(vis,0,sizeof(vis));
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; i++)
        {
            scanf("%lld",&v[i]);
        }
        int x,y;
        for(int i=1; i<=n; i++)
        {
            e[i].clear();
        }
        for(int i=0; i<m; i++)
        {
            scanf("%d%d",&x,&y);
            e[x].push_back(y);
            e[y].push_back(x);
            deg[x]++;
            deg[y]++;
        }
        int j;
        for(int i=1; i<=n; i++)
        {
            for(j=1; j<=n; j++)
            {
                if(deg[j]==0||deg[j]==1)
                {
                    break;
                }
            }
            if(j>n)break;
            deg[j]=-1;
            for(int k=0; k<e[j].size(); k++)
            {
                deg[e[j][k]]--;
            }
        }
        ll sum=0;
        for(int i=1; i<=n; i++)
        {
            if(!vis[i]&&deg[i]>=2)
            {
                sum+=bfs(i);
            }
        }
        printf("%lld\n",sum);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值