HDU5438 Ponds 并查集+拓扑排序思想

http://acm.hdu.edu.cn/showproblem.php?pid=5438

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<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
typedef long long ll;
struct edge
{
    int v,next;
} edge[maxn*2];
int t,head[maxn];
int indeg[maxn],vis[maxn];
int x[maxn],y[maxn],father[maxn],sum[maxn];
ll val[maxn];
void inti()
{
    t=0;
    memset(head,-1,sizeof(head));
    memset(indeg,0,sizeof(indeg));
    memset(vis,0,sizeof(vis));
}
void add(int u,int v)
{
    edge[t].v=v;
    edge[t].next=head[u];
    head[u]=t++;
    indeg[v]++;
}
int que[maxn],b1,b2;
//拓扑排序;
void tuopu(int n)
{
    b1=b2=0;
    for(int i=1; i<=n; i++)
    {
        if(indeg[i]<=1)
        {
            vis[i]=1;//单独的一个点;
            que[b2++]=i;
        }
    }
    while(b1!=b2)
    {
        int temp=que[b1++];
        for(int i=head[temp]; i>-1; i=edge[i].next)
        {
            int to=edge[i].v;
            if(vis[to]==0)
            {
                indeg[to]--;
                if(indeg[to]==1)
                {
                    que[b2++]=to;
                    vis[to]=1;
                }
            }
        }
    }
}
int find(int x)
{
    if(x==father[x]) return x;
    return father[x]=find(father[x]);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,m;
        inti();
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; i++)
        {
            scanf("%lld",&val[i]);
            father[i]=i;//为并查集作铺垫;
            sum[i]=1;//每个点都包含它本身
        }
        for(int i=0; i<m; i++)
        {
            scanf("%d%d",&x[i],&y[i]);
            add(x[i],y[i]);//加点;
            add(y[i],x[i]);//看成无向图;
        }
        tuopu(n);//进行拓扑排序;
        for(int i=0; i<m; i++)
        {
            if(vis[x[i]]==0&&vis[y[i]]==0)
            {
                int fx=find(x[i]);
                int fy=find(y[i]);
                if(fx!=fy)
                {
                    father[fx]=fy;//把fy当做父节点;
                    val[fy]+=val[fx];//加上子节点的val值
                    sum[fy]+=sum[fx];//便于看联通快数目是奇数偶数
                }
            }
        }
        ll res=0;
        for(int i=1; i<=n; i++)
        {
            if(father[i]==i&&vis[i]==0&&sum[i]&1) res=res+val[i];
            //是父节点且vis[i]=0并且所包含的个数是奇数,则累加;
        }
        printf("%I64d\n",res);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lanshan1111/article/details/86585000
今日推荐