HDU 5438 Ponds【拓扑排序+BFS】

版权声明:转载什么的好说,附上友链就ojek了,同为咸鱼,一起学习。 https://blog.csdn.net/sodacoco/article/details/86586739

题目:

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

题目大意:

       有P池塘,M条管道,每两个池塘最多只有一条边相连,每一个池塘都有一个V值;不断移除连接管道的数目小于2的池塘,直到不能移除为止,问,最终由奇数个节点组成的连通分量的价值总和为多少。

解题思路:

        拓扑排序:https://blog.csdn.net/sodacoco/article/details/86586881

        一直删除连接管道数目小于2的池塘,类似于拓扑排序的删除选项,可以用相同的思路,删去度数为零的点或者为1的点,由拓扑排序的思路可以知道,这样的删除最后如果剩余元素,那么图中存在“环”,我们就需要寻找元素数目为奇数的环,计算他们的权值和,用BFS来实现。

实现代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e4+100;

vector<int>G[maxn];
ll sum1;
int n,m,count_,vis[maxn],degree[maxn],num[maxn];

void init(int n){
    for(int i=1;i<=n;i++)   G[i].clear();
    
    memset(vis,0,sizeof(vis));
    memset(degree,0,sizeof(degree));
}

void topcode(){
    queue<int>Q;
    for(int i=1;i<=n;i++)
        if(degree[i]<=1){
            vis[i]=1;
            Q.push(i);
        }

    while(!Q.empty()){
        int u=Q.front();
        Q.pop();
        for(int i=0;i<G[u].size();i++){
            int v=G[u][i];
            if(!vis[v]){
                degree[v]--;
                if(degree[v]<=1){
                    vis[v]=1;
                    Q.push(v);
                }
            }
        }
    }
}

void dfs(int u){
    vis[u]=1;
    for(int i=0;i<G[u].size();i++){
        int v=G[u][i];
        if(vis[v]==0){
            sum1+=num[v];
            count_++;
            dfs(v);
        }
    }
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        init(n);
        
        for(int i=1;i<=n;i++)
            scanf("%d",&num[i]);
        int u,v;
        for(int i=0;i<m;i++){
            scanf("%d%d",&u,&v);
            G[u].push_back(v);
            G[v].push_back(u);
            degree[u]++;
            degree[v]++;
        }
        topcode();
        
        ll sum=0;
        for(int i=1;i<=n;i++){
            if(vis[i]==0){
                sum1=num[i];
                count_=1;
                dfs(i);
                if(count_&1)
                    sum+=sum1;
            }
        }
        printf("%I64d\n",sum);
    }
    return 0;
}
扫描二维码关注公众号,回复: 5022567 查看本文章

猜你喜欢

转载自blog.csdn.net/sodacoco/article/details/86586739