hdu 5438 Ponds 拓扑排序 + 并查集

本文介绍了一种解决特定图论问题的方法,该问题涉及在不断移除叶节点的情况下计算剩余顶点数量为奇数的连通子图的价值总和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

分析:先删掉0和1的 就是拓扑了,然后算出每个联通分量的总值和个数和,再加上就行,这个拓扑因为不是有向图,是无向图,所以边的两头都要++,然后拓扑的时候要删边的话,不用两头都删,因为你可以用一个标记数组vis 标记哪些已经不要了的,你都不要了,你还删它干啥。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int vis[10100],val[10100];
vector<int> v[10100];
queue<int> Q;
int in[10100];
int f[10100],sum[10100];
pair<int,int> pp[100100];
ll mon[101010];


int find(int x)
{
    return x==f[x]?x:f[x]=find(f[x]);
}

void un(int a,int b)
{
    int f1=find(a);
    int f2=find(b);
    if(f1!=f2)
    {
        f[f2]=f1;
        sum[f1]+=sum[f2];
        mon[f1]+=mon[f2];
        sum[f2]=mon[f2]=0;
    }
}



int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        while(!Q.empty()) Q.pop();
        memset(in,0,sizeof(in));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&val[i]);
            vis[i]=0;
            sum[i]=1,f[i]=i;
            mon[i]=val[i];
            v[i].clear();
        }
        for(int i=1;i<=m;i++)
        {
            int c,d;
            scanf("%d%d",&c,&d);
            pp[i]=make_pair(c,d);
            v[c].push_back(d);
            v[d].push_back(c);
            in[c]++,in[d]++;
        }
        for(int i=1;i<=n;i++)
        {
            if(in[i]==0||in[i]==1)
                Q.push(i);
        }
        while(!Q.empty())
        {
            int top=Q.front();
            vis[top]=1;
            Q.pop();
            for(int i=0;i<v[top].size();i++)
            {
                in[v[top][i]]--;
                if(!vis[v[top][i]]&&(in[v[top][i]]==0||in[v[top][i]]==1))
                    Q.push(v[top][i]);
            }
        }
        long long res=0;
        for(int i=1;i<=m;i++)
        {
            if((!vis[pp[i].first])&&(!vis[pp[i].second]))
                un(pp[i].first,pp[i].second);
        }

        for(int i=1;i<=n;i++){
            if(i==f[i]&&(sum[i]&1)&&!vis[i])
            {
                res+=mon[i];
            }
        }
        printf("%lld\n",res );
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值