0
点赞
收藏
分享

微信扫一扫

HDU 5437 Ponds(DFS)


Ponds


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



Problem Description


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


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


 



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<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<stack>
using namespace std;
struct node
{
    int u;
    int v;
    int next;
} edge[1000000];
int n,m;
int cnt;
int head[1000000];
__int64 a[100000];
int vis[100000];
__int64 sum[100000];
int in[100000];
__int64 sum1[100000];

void add(int u,int v)
{
    edge[cnt].u=u;
    edge[cnt].v=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
void dfs(int u)
{
    vis[u]=1;
    sum[u]=1;
    sum1[u]=a[u];
    for(int i=head[u]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].v;
        if(vis[v]==0)
        {
            dfs(v);
            sum[u]+=sum[v];
            sum1[u]+=sum1[v];
        }
    }
}
void dfs1(int u)
{
    vis[u]=1;
    for(int i=head[u]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].v;
        if(vis[v]==0)
        {
            in[v]--;
            if(in[v]==1||in[v]==0)
            {
                dfs1(v);
            }
        }
    }
}
int main()
{
    int u,v,T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        memset(head,-1,sizeof(head));
        memset(a,0,sizeof(a));
        memset(sum,0,sizeof(sum));
        memset(sum1,0,sizeof(sum1));
        memset(vis,0,sizeof(vis));
        memset(in,0,sizeof(in));
        cnt=0;
        for(int i=1; i<=n; i++)
        {
            scanf("%I64d",&a[i]);
        }
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d",&u,&v);
            if(head[u]!=-1&&edge[head[u]].v==v)
            {
                continue;
            }
            if(u==v)
            {
              in[u]++;
              continue;
            }
            in[u]++;
            in[v]++;
            add(u,v);
            add(v,u);
        }
        for(int i=1; i<=n; i++)
        {
            if(vis[i]==0)
            {
                if(in[i]==1||in[i]==0)
                {
                    dfs1(i);
                }
            }
        }
        __int64 ans=0;
        for(int i=1; i<=n; i++)
        {
            if(!vis[i])
            {
                dfs(i);
                if(sum[i]%2==1)
                {
                    ans+=sum1[i];
                }
            }
        }
        printf("%I64d\n",ans);
    }
    return 0;
}




举报

相关推荐

0 条评论