HDU 5438 Ponds(拓扑排序 + DFS)

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

【思路分析】
   题意是有n个泳池,一共有m条通道将其中的泳池连接起来,现在需要拆除那些相连最多一个的泳池,求剩下连通块中泳池为奇数个的这些连通块的权值之和。抽象出来就是删去无向图中度为0或者1的结点,再求剩下的连通块中结点为奇数个连通块的权值。
   先用拓扑排序删去度为0或者1的结点,再用DFS对每个连通块的结点遍历计数即可。

代码如下:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxm = 1e5 + 5;
const int maxn = 1e4 + 5;
int n,m,cnt,num;
int head[maxn],deg[maxn];//deg为结点的度数
int u,v;
bool visited[maxn];
ll tmp,ans,val[maxn];
struct Edge
{
    int to,next;
}edge[maxm];
queue <int> q;

void addEdge(int u,int v)
{

    edge[cnt].to = v;
    edge[cnt].next = head[u];
    head[u] = cnt++;
}
void init()
{
    while(!q.empty())
    {
        q.pop();
    }
    cnt = ans = 0;
    memset(head,-1,sizeof(head));
    memset(deg,0,sizeof(deg));
    memset(visited,false,sizeof(visited));

    scanf("%d %d",&n,&m);
    for(int i = 1;i <= n;i++)
    {
        scanf("%lld",&val[i]);
    }
    for(int i = 1;i <= m;i++)
    {
        scanf("%d %d",&u,&v);
        addEdge(u,v);
        addEdge(v,u);
        deg[u]++;
        deg[v]++;
    }

}
void topoSort()//删点
{
    for(int i = 1;i <= n;i++)
    {
        if(deg[i] == 0)
        {
            visited[i] = true;//直接删去独立结点
        }
        if(deg[i] == 1)//度为1的结点入队,因为删去该结点后其连接的结点度数可能变为0或1
        {
            visited[i] = true;
            q.push(i);
        }
    }
    while(!q.empty())
    {
        int temp = q.front();
        q.pop();
        for(int i = head[temp];i != -1;i = edge[i].next)
        {
            int v = edge[i].to;//temp相邻的结点
            deg[v]--;
            if(deg[v] == 0)
            {
                visited[v] = true;
            }
            if(deg[v] == 1)
            {
                visited[v] = true;
                q.push(v);
            }
        }
    }
}
void dfs(int u,int des)//计数遍历回环图,des记录终点(也就是一开始的起点)
{
    visited[u] = true;
    num++;
    tmp += val[u];
    for(int i = head[u];i != -1;i = edge[i].next)
    {
        int v = edge[i].to;
        if(!visited[v] && v != des)
        {
            dfs(v,u);
        }
    }
    return;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        init();
        topoSort();
        for(int i = 1;i <= n;i++)
        {
            if(!visited[i])
            {
                num = 0;
                tmp = 0;
                dfs(i,0);
                if(num & 1)
                {
                    ans += tmp;
                }
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值