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

题目大意:n个顶点m条边的无向图,每个顶点有一个权值,现在要删除所有的叶子节点,重复此操作直至形成的“森林”中不存在叶子节点,然后找出森林中顶点个数为奇数的树的权值的和。


分析:删除操作可以模拟拓扑过程,首先我们让度数为1的顶点进队,然后每出队一个顶点,就让该顶点相邻顶点的度数减1,碰到度数为1的顶点就入队,重复操作直至队列为空,这样我们就删除了所有的叶子节点,此时图可能变为了森林,然后只需在剩下的森林中找出符合条件的树即可,可以用并查集纪录每个树中顶点的个数和树的权值。要注意的是图不能用邻接矩阵来存储会内存超限,这里用的是前向星。

code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn = 10005;
int u[maxn],v[maxn];//记录每条边
int w[maxn],degree[maxn];//w记录每个点权值,degree记录每个点度数
int vis[maxn];
struct node{
    int to;
    int nex;
}edge[maxn*20];
int head[maxn],num;
void add(int u,int v){
    edge[num].to = v;
    edge[num].nex = head[u];
    head[u] = num++;
}
//并查集
int pre[maxn],cnt[maxn];//pre记录父亲,cnt记录个数
ll sum[maxn];//sum记录总权值
void init(int n){
    for(int i = 1; i <= n; i++){
        pre[i] = i;
        cnt[i] = 1;
        sum[i] = w[i];
    }
}
int Find(int x){
    if(x == pre[x])
        return x;
    else return pre[x] = Find(pre[x]);
}
void Union(int a,int b){
    int x = Find(a),y = Find(b);
    if(x != y){
        pre[y] = x;
        cnt[x] += cnt[y];
        sum[x] += sum[y];
    }
}
void Top(int n){
    queue<int> que;
    for(int i = 1; i <= n; i++){
        if(degree[i] <= 1){
            vis[i] = 1;
            que.push(i);
        }
    }
    while(!que.empty()){
        int tmp = que.front();
        que.pop();
        for(int i = head[tmp]; i != -1; i = edge[i].nex){
            int x = edge[i].to;
            if(!vis[x]){
                degree[x]--;
                if(degree[x] == 1){
                    vis[x] = 1;
                    que.push(x);
                }
            }
        }
    }
}
int main(){
    int t,m,n;
    scanf("%d",&t);
    while(t--){
        memset(vis,0,sizeof(vis));
        memset(head,-1,sizeof(head));
        memset(degree,0,sizeof(degree));
        num = 0;
        scanf("%d%d",&n,&m);
        for(int i = 1; i <= n; i++){
            scanf("%d",&w[i]);
        }
        for(int i = 0; i < m; i++){
            scanf("%d%d",&u[i],&v[i]);
            add(u[i],v[i]);
            add(v[i],u[i]);
            degree[u[i]]++;
            degree[v[i]]++;
        }
        Top(n);
        init(n);
        for(int i = 0; i < m; i++){
            if(!vis[u[i]] && !vis[v[i]])
                Union(u[i],v[i]);
        }
        ll ans = 0;
        for(int i = 1; i <= n; i++){
            if(i == pre[i] && !vis[i] && (cnt[i] & 1))
                ans += sum[i];
        }
        printf("%lld\n",ans);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值