HDU 5438 Ponds(dfs)——2015 ACM/ICPC Asia Regional Changchun Online

Ponds

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)


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

Source
2015 ACM/ICPC Asia Regional Changchun Online
 
/*********************************************************************/

题意:给你p个池塘以及每个池塘的价值,再给你m条边(即池塘ai与池塘bi之间连接的管道暂且称为边),问在移除度小于2(即最终留下的池塘的度均要大于等于2)的池塘之后,所有回路中有奇数个池塘的回路的价值之和是多少

这题描述起来可能会有些拗口,所以,我们暂且拿样例来说明一下


按照样例将图画出来,就是上图这个样子,可以移除的池塘只有7,而剩下的则是1~6这6个池塘,它们组成两个回路,每个回路的池塘数为3,是奇数,故符合要求,所以总和为1+2+3+4+5+6=21

解题思路:其实我们按照题目说的来就可以了,先将度小于2的池塘移除,这一步利用dfs可以做到,遍历p个点,将一开始度就小于2的池塘移除,这样就会导致与该池塘相邻的池塘度都减少1,所以要用到dfs删点

而删完点之后,则仍旧遍历未删除的点,再一次dfs记录各自回路中点的个数及池塘价值之和,以确定是否要加入最终结果中

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
using namespace std;
const int N = 10005;
const int inf = 1000000000;
const int mod = 2009;
struct node
{
    int to,next;
}e[20*N];
int s[N],w[N],h[N],k,cnt;
__int64 sum,ans;
bool v[N];
void add_edge(int u,int v)
{
    e[k].to=v;
    e[k].next=h[u];
    h[u]=k++;
}
void del(int u)
{
    for(int i=h[u];i+1;i=e[i].next)
        if(!v[e[i].to])
        {
            if(w[e[i].to]==2)
            {
                w[e[i].to]=0;
                v[e[i].to]=true;
                del(e[i].to);
                return ;
            }
            else if(w[e[i].to]==1)
            {
                v[e[i].to]=true;
                w[e[i].to]=0;
                return ;
            }
            else
                w[e[i].to]--;
        }
}
void dfs(int u)
{
    sum+=s[u];
    cnt++;v[u]=true;
    for(int i=h[u];i+1;i=e[i].next)
        if(!v[e[i].to])
            dfs(e[i].to);
}
int main()
{
    int t,p,m,i,a,b;
    scanf("%d",&t);
    while(t--)
    {
        memset(w,0,sizeof(w));
        memset(h,-1,sizeof(h));
        memset(v,false,sizeof(v));
        ans=0;
        scanf("%d%d",&p,&m);
        for(i=1;i<=p;i++)
            scanf("%d",&s[i]);
        for(i=k=0;i<m;i++)
        {
            scanf("%d%d",&a,&b);
            add_edge(a,b);
            add_edge(b,a);
            w[a]++;w[b]++;
        }
        for(i=1;i<=p;i++)
            if(w[i]<2)
            {
                v[i]=true;
                if(w[i]==1)
                    del(i);
            }
        for(i=1;i<=p;i++)
            if(!v[i])
            {
                cnt=0;sum=0;
                dfs(i);//printf("%d###%d\n",i,cnt);
                if(cnt&1)
                    ans+=sum;
            }
        printf("%I64d\n",ans);
    }
    return 0;
}
菜鸟成长记


你可能感兴趣的:(算法,ACM,DFS)