HDU 5438 Ponds (DFS + 并查集)

本文介绍了一道算法题目,主要内容是如何通过移除特定条件下的节点来计算剩余连通块中节点价值的奇数和。利用DFS进行节点移除,并采用并查集确定最终的连通组件。

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

Ponds

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


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
 


大体题意:
告诉你n 个池塘,有些池塘是想连接的,他要删除一些池塘,删除原则是这个池塘连接的其他池塘必须小于2个,直到不能删为止,问最后连通池塘是奇数的连通的权值之和!

思路:
用个数组记录连接的个数,用dfs删除点,在用并查集求连通个数,遍历连通块求出是奇数的权值之和即可!
详细见代码:
/************************************************
┆  ┏┓   ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃       ┃ ┆
┆┃   ━   ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃       ┃ ┆
┆┃   ┻   ┃ ┆
┆┗━┓    ┏━┛ ┆
┆  ┃    ┃  ┆      
┆  ┃    ┗━━━┓ ┆
┆  ┃  AC代马   ┣┓┆
┆  ┃           ┏┛┆
┆  ┗┓┓┏━┳┓┏┛ ┆
┆   ┃┫┫ ┃┫┫ ┆
┆   ┗┻┛ ┗┻┛ ┆
************************************************ */

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
#include<cstdlib>
#include<stack>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>

#define fout freopen("out.txt","w","stdout");
using namespace std;
const int maxn = 10000 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int mod = 1e9+7;
typedef long long ll;
typedef unsigned long long llu;

int gcd(int a,int b){ return !b?a:gcd(b,a%b); }
ll my_pow(ll a,ll n){
    ll ans = 1;
    while(n){
        if (n & 1)
            ans = (ans % mod * a % mod) % mod;
        n/=2;
        a = (a%mod*a%mod)%mod;
    }
    return ans;
}

vector<int>g[maxn];
vector<pair<int,int> >bing[maxn];
int n,m;
ll sum = 0ll;
int vis[maxn];
int a[maxn];
int lenn[maxn];
int fa[maxn];
void dfs(int k){
    sum -= a[k];
    vis[k] = 1;
    //lenn[k]--;
//    cout << k << endl;
    int len = g[k].size();
    for (int i = 0; i <len; ++i){
        int u = g[k][i];
        if (vis[u])continue;
        lenn[u]--;
        if (lenn[u] <= 1)dfs(u);
    }
}
int find(int x){
    return fa[x] == x ? x : find(fa[x]);
}
void add(int x,int y){
    int xx = find(x);
    int yy = find(y);
    if (xx != yy){
        if (xx > yy)swap(xx,yy);
        fa[yy] = xx;
    }
}

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d %d",&n,&m);
        for (int i = 1; i <= n; ++i){
            g[i].clear();
            vis[i] = 0;
            fa[i] = i;
            bing[i].clear();
        }
        memset(lenn,0,sizeof lenn);
       // memset(vis,0,sizeof vis);
        sum = 0ll;
        for (int i = 1; i <= n; ++i)scanf("%d",&a[i]),sum += a[i];
//        cout << sum<<endl;
        for (int i = 0; i < m; ++i){
            int u,v;
            scanf("%d %d",&u,&v);
            add(u,v);
            g[u].push_back(v);
            g[v].push_back(u);
            lenn[u]++;
            lenn[v]++;
        }
//        cout << "hehe" << lenn[2] << endl;
        for (int i = 1; i <= n; ++i){
            if (lenn[i] <= 1 && !vis[i]){
                dfs(i);
                i = 0;
            }
        }
        sum = 0ll;
        for (int i = 1; i <= n; ++i){
            if (vis[i])continue;
            int faa = find(i);
//            cout << faa << endl;
            bing[faa].push_back(make_pair(i,a[i]));
        }
        for (int i = 1; i <= n; ++i){
            int len = bing[i].size();
            if (len > 0 && len % 2 == 1){
                for (int j = 0; j < len; ++j){
                    sum += bing[i][j].second;
                }
//                sum += a[i];
            }
        }
//        cout << bing[1].size() << endl;
        printf("%I64d\n",sum);
    }


    return 0;
}
/*
3 2
1 2 3
1 2
2 3
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值