hdu 5438 Ponds(并查集/拓扑排列)

Ponds

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

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


把度为一的鱼塘都删去,求剩下连通分支数为奇数的池塘的权值和


#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>

using namespace std;
#define ll long long
#define N 10005
#define M 100005
int t, n, m, x, y;
ll sum;
int d[N], p[N];
ll val[N];
vector<int>ve[N];

int fnd( int x ) { return p[x] = p[x] == x ? x : fnd( p[x] ); }
void unit( int x, int y )
{
    int u = fnd( x );
    int v = fnd( y );//printf( "%d %d %d %d\n", x, y, u, v );
    if( u != v ){
        p[u] = v;
    }//printf( "%d %d %d %d\n", x, y, w[u], w[v] );
}
void change()///
{
    int flg = 0;
    for( int i = 1 ; i <= n ; i ++ ){
        if( d[i] == 1 ){
            d[i] = 0;
            for( int j = 0 ; j < ve[i].size() ; j ++ ){
                int k = ve[i][j];
                d[k] --;
            }
            flg ++;
        }
    }
    if( flg ) change();///flg
}
int main()
{
    freopen( "in.txt", "r", stdin );
    scanf( "%d", &t );
    while( t -- ){
        for( int i = 0 ; i <= N ; i ++ ) p[i] = i;///N
        for( int i = 0 ; i < N ; i ++ ) ve[i].clear();
        memset( d, 0, sizeof( d ));///度
        ///memset( val, 0, sizeof( val ));///权值

        scanf( "%d%d", &n, &m );
        for( int i = 1 ; i <= n ; i ++ ){
            scanf( "%lld", &val[i] );///
        }
        for( int i = 0 ; i < m ; i ++ ){
            scanf( "%d%d", &x, &y );
            ve[x].push_back( y );
            ve[y].push_back( x );
            d[x] ++;
            d[y] ++;
            unit( x, y );
        }
        change();
        vector<int>num[N];
        for( int i = 1 ; i <= n ; i ++ ){
            int u = fnd( i );
            if( d[i] > 1 ) num[u].push_back( i );///>1
        }
        sum = 0;
        for( int i = 1 ; i <= n ; i ++ ){
            if( num[i].size() & 1 )
                for( int j = 0 ; j < num[i].size() ; j ++ ){
                    sum += val[num[i][j]];
                }
        }
        printf( "%lld\n", sum );
    }
    return 0;
}
#include <iostream>///depend on -
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>

using namespace std;
#define ll long long
#define N 10005
#define M 100005
ll ans, sum;
int t, n, m, u, v, cnt, num;
int a[N], d[N], flg[N], head[N], val[N];

struct node{
    int now, next;
}nod[M];

void topo()
{
    queue<int>qu;
    for( int i = 1 ; i <= n ; i ++ ){
        if( d[i] < 2 ){
            d[i] = -6;
            qu.push( i );
        }
    }
    while( !qu.empty() ){///
        int k = qu.front();
        qu.pop();
        flg[k] = 1;///
        for( int i = head[k] ; i != -1 ; i = nod[i].next ){///
            int nd = nod[i].now;
            if( d[nd] == -6 ) continue;
            d[nd] --;
            if( d[nd] < 2 && d[nd] != -6 ){
                d[nd] = -6;
                qu.push( nd );
            }
        }
    }
}
void dfs( int x )
{
    flg[x] = 1;
    cnt ++;
    ans += val[x];
    for( int i = head[x] ; i != -1 ; i = nod[i].next ){///
        int k = nod[i].now;
        if( flg[k] ) continue;///
        dfs( k );
    }
}
void add( int x, int y )///___
{
    nod[num].now = y;
    nod[num].next = head[x];
    head[x] = num ++;
}
int main()
{
    freopen( "in.txt", "r", stdin );
    scanf( "%d", &t );
    while( t -- ){
        num = 0;
        memset( d, 0, sizeof( d ));
        memset( flg, 0, sizeof( flg ));
        memset( head, -1, sizeof( head ));
        scanf( "%d%d", &n, &m );
        for( int i = 1 ; i <= n ; i ++ ){
            scanf( "%d", &val[i] );
        }
        for( int i = 0 ; i < m ; i ++ ){
            scanf( "%d%d", &u, &v );
            add( u, v );
            add( v, u );
            d[u] ++;
            d[v] ++;
        }
        topo();
        sum = 0;
        for( int i = 1 ; i <= n ; i ++ ){
            if( !flg[i] ){
                cnt = 0;
                ans = 0;
                dfs( i );
                if( cnt & 1 ) sum += ans;
            }
        }
        printf( "%lld\n", sum );
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值