hdu 5483 拓扑排序+DFS计算权值

题目链接:

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): 5795    Accepted Submission(s): 1603


 

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(1≤T≤30) 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(1≤p≤104) which represents the number of ponds she owns, and the other is the number m(1≤m≤105) which represents the number of pipes.

The next line contains p numbers v1,...,vp, where vi(1≤vi≤108) 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条边的图,每个点有个权值,现在你要删除所有度<2的点,删除一个点后与他连接的边也删除,问最后不能删除后的图当中,每个奇数点数的连通图的权值和

题目思路:

记录度数<2的点,删除点的同时判断是否另一个端是否度数<2,一直删除到没有度数<2,然后DFS搜索联通块个数为奇数的权值和

This is the code

//给你一个图,然后要求把度数小于2的点全部删去,然后问你奇数集合的点权和是多少

//注意,你删去了一个点之后,可能会使得一些点又变成了度数小于2的
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x3f3f3f3f      //1061109567
#define LL_INF 0x3f3f3f3f3f3f3f3f //4557430888798830399
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。

const int Maxn=200100;

struct Edge
{
    int to;
    int next;
}edge[Maxn];

LL a[Maxn],ans;
int head[Maxn];
int tol;
int vis[Maxn];
int deg[Maxn];//每个点的度数
int ok[Maxn];//表示是否删过点
int col;//联通块
vector<int> s[Maxn];//联通块
queue<int> q;

void init()
{
    memset(deg,0,sizeof(deg));
    memset(head,-1,sizeof(head));
    memset(ok,1,sizeof(ok));
    memset(vis,1,sizeof(vis));
    ans=0LL;
    tol=0;
    col=0;
    while(!q.empty())
        q.pop();
}

void dfs(int u,int fa)
{
    s[col].push_back(u);
    ok[u]=0;
    for(int i=head[u]; i!=-1; i=edge[i].next)
    {
        if((!vis[i]) || (!ok[edge[i].to]))
            continue;
        if(i==(fa^1))
            continue;
        dfs(edge[i].to,i);
    }
}

void addEdge(int u,int v)
{
    edge[tol].to=v;
    edge[tol].next=head[u];
    head[u]=tol++;

    edge[tol].to=u;
    edge[tol].next=head[v];
    head[v]=tol++;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        init();
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; i++)
            scanf("%lld",&a[i]);
        for(int i=0; i<m; i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            deg[u]++;
            deg[v]++;
            addEdge(u,v);
        }
        for(int i=1; i<=n; i++)//寻找度数小于2的点
            if(deg[i]<2)
                q.push(i);
        while(!q.empty())
        {
            int u=q.front();
            q.pop();
            ok[u]=0;
            for(int i=head[u]; i!=-1; i=edge[i].next)
            {
                vis[i]=0;
                deg[u]--;
                deg[edge[i].to]--;
                if(ok[edge[i].to]&&deg[edge[i].to]<2)//表示度数小于2并且没有删过
                    q.push(edge[i].to);
            }
        }
        for(int i=1; i<=n; i++)
        {
            col++;
            s[col].clear();
            if(ok[i])
            {
                dfs(i,-1);
                if(s[col].size()%2==1)
                    for(int j=0; j<s[col].size(); j++)
                        ans+=a[s[col][j]];
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值