Ponds HDU - 5438 (拓扑排序)

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 vv.

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

The next line contains pp numbers v1,…,vpv1,…,vp, where vi(1≤vi≤108)vi(1≤vi≤108) indicating the value of pond ii.

Each of the last mm lines contain two numbers aa and bb, which indicates that pond aa and pond bb 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

后来想到了拓扑排序,一个一个删掉吧,简单题

#include<bits/stdc++.h>
#define INF 1000000000
#define N 10004
using namespace std;
int n,m;
bool vis[N];
int a[N];
int d[N];
vector<int> g[N];
long long ans;
long long temp;
int num;
bool mark[N];
void dfs(int u)
{
    num++;
    temp+=a[u];
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if(mark[v])
            continue;
        if(!vis[v])
        {
            vis[v]=true;
            dfs(v);
        }
    }
}
queue<int> que;
void process()
{
    for(int i=1;i<=n;i++)
    {
        if(d[i]<2)
        {
           que.push(i);
            mark[i]=true;
        }
    }
    while(!que.empty())
    {
        int u=que.front();
        que.pop();
        for(int i=0;i<g[u].size();i++)
        {
            int v=g[u][i];
            d[v]--;
            if(d[v]==1&&!mark[v])
            {
                //cout<<v<<endl;
                que.push(v);
                mark[v]=true;
            }
        }
    }
}
void init()
{
    for(int i=1;i<=n;i++)
        g[i].clear();
    memset(d,0,sizeof(d));
    memset(vis,false,sizeof(vis));
    memset(mark,false,sizeof(mark));
}
int main()
{
    int x,y,w;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ans=0;
        scanf("%d%d",&n,&m);
        init();
        for(int i=1;i<=n;i++)
            scanf("%d",a+i);
        while(m--)
        {
            scanf("%d%d",&x,&y);
            g[x].push_back(y);
            g[y].push_back(x);
            d[x]++;
            d[y]++;
        }

        process();

        for(int i=1;i<=n;i++)
        {
            if(!vis[i]&&!mark[i])
            {
                vis[i]=true;
                temp=0;
                num=0;
                dfs(i);
                if(num&1)
                    ans+=temp;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值