【图论拓扑】Ponds

传送门:点击这里

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 .
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 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 which represents the number of ponds she owns, and the other is the number which represents the number of pipes.

The next line contains numbers , where indicating the value of pond .

Each of the last lines contain two numbers and , which indicates that pond and pond 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
题目大意:有一些池塘,每一个池塘都有一个价值,现在想删除一些池塘。

有如下删除条件:一个池塘有两个管道连接的不可以删除。即删除入度为0或者1的点。

求最后剩下的为奇数环的池塘的价值。

解题思路:用拓扑将所有入度为0和1的点都可以删掉,直到删完为止,对每个没被访问过的点进行搜索。判断环中是否为奇数个池塘。如果可以就return和,否则就不加,return 0即可。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>

using namespace std;
#define ll long long
const int N=10010;

ll p,m,v[N],vis[N],indir[N];
vector<ll>G[N];

ll bfs(ll x)
{
    queue<ll>q;
    q.push(x);
    vis[x]=1;
    ll k=0;
    ll sum=v[x];
    while (!q.empty())
    {

        int s=q.front();
        q.pop(); //cout<<s<<endl;
        k++;
        for (int i=0; i<G[s].size(); i++)
        {
            if (!vis[G[s][i]]&&indir[G[s][i]]>=2)//删掉的点不可以加进来
            {
                sum+=v[G[s][i]];
                q.push(G[s][i]);
                vis[G[s][i]]=1;
            }
        }
    }
    if (k>=3&&k%2==1)
        return sum;
    else
        return 0;
}

int main()
{
    int T,a,b;
    scanf("%d",&T);
    while (T--)
    {
        scanf("%lld%lld",&p,&m);
        memset(vis,0,sizeof(vis));
        memset(G,0,sizeof(G));
        memset(indir,0,sizeof(indir));
        for (int i=1; i<=p; i++)
        {
            scanf("%lld",&v[i]);
        }
        for (int i=1; i<=p; i++)
            G[i].clear();
        for (int i=1; i<=m; i++)
        {
            scanf("%d%d",&a,&b);
            G[a].push_back(b);//将b放在a队列的最后一个
            G[b].push_back(a);
            indir[a]++;
            indir[b]++;
        }
        int j;
        for (int i=1; i<=p; i++)
        {
            for ( j=1; j<=p; j++)
            {
                if (indir[j]==0||indir[j]==1)
                {
                    break;
                }
            }
            if (j>p)
                break;
            indir[j]=-1;
            for (int k=0; k<G[j].size(); k++)
            {
                indir[G[j][k]]--;
            }
        }
        ll ans=0;
        for (int i=1; i<=p; i++)//搜遍所有的环
            if (!vis[i]&&indir[i]>=2)
                ans+=bfs(i);
        cout<<ans<<endl;
    }
    return 0;
}

 

发表评论

电子邮件地址不会被公开。