HDU 5438 Ponds

来源:互联网 发布:linux acpi 编辑:程序博客网 时间:2024/06/05 18:23

Ponds

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


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
17 71 2 3 4 5 6 71 41 54 52 32 63 62 7
 

Sample Output
21
 

Source
2015 ACM/ICPC Asia Regional Changchun Online

简单的搜索题目。每次将度为1的池塘删掉,问最后剩余的池塘中,相连池塘数为奇数的权值和。

每次对度为1的点进行搜索,将对应边删除并将相连点的度减一,最后遍历池塘,对度不为0的池塘进行搜索并记录结果。


#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespace std;struct edge{    int v;    int next;}e[200010];int d[10010];struct node{    int u;    int w;    friend bool operator < (node a ,node b)    {        return d[a.u]>d[b.u];    }}N[10010];int head[10010];int tol,n,m;long long ans;bool vis[200010],flag[10010];void addedge(int u,int v){    e[tol].v=v;    e[tol].next=head[u]++;    head[u]=tol++;    e[tol].v=u;    e[tol].next=head[v]++;    head[v]=tol++;    return ;}long long bfs(int st,int sum){    long long num=N[st-1].w;    queue<int >q;    q.push(st);    //cout<<st<<" ";    while(q.size())    {        st=q.front();        q.pop();        for(int i=head[st];i!=-1;i=e[i].next)        {            if(!vis[i]&&!flag[e[i].v])            {                //cout<<e[i].v<<"*";                sum++;                num+=N[e[i].v-1].w;                q.push(e[i].v);                vis[i]=true ;                vis[i^1]=true ;                flag[e[i].v]=true ;            }        }    }    if(sum&1)    return num;    else return 0;}void DFS(int st){    for(int i=head[st];i!=-1;i=e[i].next)    {        if(!vis[i])        {            vis[i]=true ;            vis[i^1]=true ;            d[st]--;            d[e[i].v]--;            if(d[e[i].v]==1)            DFS(e[i].v);            return ;        }    }    return ;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        for(int i=0;i<n;i++)        scanf("%d",&N[i].w),d[i+1]=0,N[i].u=i+1;        tol=0;        ans=0;        memset(head,-1,sizeof(head));        while(m--)        {            int u,v;            scanf("%d%d",&u,&v);            d[u]++;            d[v]++;            addedge(u,v);        }        memset(vis,false ,sizeof(vis));        memset(flag,false ,sizeof(flag));        for(int i=1;i<=n;i++)        {            if(d[i]==1)            {                DFS(i);            }        }        for(int i=1;i<n;i++)        {            if(d[i]&&!flag[i])            ans+=bfs(i,1);        }        cout<<ans<<endl;    }    return 0;}


0 0