码迷,mamicode.com
首页 > 编程语言 > 详细

hdu 5439 Ponds(长春网络赛——拓扑排序+搜索)

时间:2016-04-10 15:01:18      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5438

Ponds

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


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 valuev技术分享.

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 numberp(1p10技术分享4技术分享)技术分享 which represents the number of ponds she owns, and the other is the number m(1m10技术分享5技术分享)技术分享 which represents the number of pipes.

The next line contains p技术分享 numbers v技术分享1技术分享,...,v技术分享p技术分享技术分享, where v技术分享i技术分享(1v技术分享i技术分享10技术分享8技术分享)技术分享 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
 

Recommend
hujie   |   We have carefully selected several similar problems for you:  5659 5658 5657 5656 5655 
 
题目大意:给出一个无向图,每一个节点都有一定的权值,最后要求输出每一个奇数圈内的权值和。

解题思路:首先 你要考虑怎么,保证它是一个圈。这样就想到采用拓扑排序的方法,去掉所有入度<2的点。再就是判断这个圈是否含有奇数个点,采用深搜的方法将每个点做一次起始点,对其进行搜索计数!

详见代码。


#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>

using namespace std;

#define N 10010
#define ll long long

ll val[N],V;
int vis[N];//判断这个点有没有删掉
int Vis[N];//判断这个点有没有做为起点进行搜索过
int indir[N],k;
vector<int>G[N];

void dfs(int st)
{
    k++;
    V+=val[st];
    for (int i=0; i<G[st].size(); i++)
    {
        if (!Vis[G[st][i]]&&!vis[G[st][i]])
        {
            Vis[G[st][i]]=1;
            dfs(G[st][i]);
        }
    }
}

int main()
{
    int t,a,b;
    scanf("%d",&t);
    while (t--)
    {
        int n,m;
        memset(vis,0,sizeof(vis));
        memset(Vis,0,sizeof(Vis));
        memset(val,0,sizeof(val));
        memset(indir,0,sizeof(indir));
        scanf("%d%d",&n,&m);
        for (int i=1; i<=n; i++)
            G[i].clear();
        for (int i=1; i<=n; i++)
            scanf("%lld",&val[i]);
        for (int i=1; i<=m; i++)
        {
            scanf("%d%d",&a,&b);
            G[a].push_back(b);
            G[b].push_back(a);
            indir[a]++;
            indir[b]++;
        }
        while (1)
        {
            int i;
            for (i=1; i<=n; i++)
            {
                if (indir[i]<2&&!vis[i])
                {
                    vis[i]=1;
                    for (int j=0; j<G[i].size(); j++)
                    {
                        if (!vis[G[i][j]])
                            indir[G[i][j]]--;
                    }
                    break;
                }
            }
            if (i>n)
                break;
        }
        ll ans=0;
        for (int i=1; i<=n; i++)
        {
            if (!Vis[i]&&!vis[i])
            {
                k=0;
                V=0;
                Vis[i]=1;
                dfs(i);
                if (k%2==1)
                    ans+=V;
            }
        }
        printf ("%lld\n",ans);
    }
    return 0;
}



hdu 5439 Ponds(长春网络赛——拓扑排序+搜索)

标签:

原文地址:http://blog.csdn.net/qiqi_skystar/article/details/51104414

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!