Ponds(图论胡搞)

Ponds

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

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

题源:2015亚洲赛区长春网赛
题目解析:题目给出一张图,可能不连通。筛选出只与一个节点相连的节点,并从图中删除节点。最后求剩下节点构成图中含有奇数个节点的环中的所有节点的权值之和。
比赛这题过的很尴尬,直接导致这场比赛的失利,太水了!

AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define SIZE 10050

using namespace std;
struct node
{
    int v;
};
vector<int >q[SIZE];
int n,m;
int no[SIZE];
int d[SIZE],vis[SIZE];
long long num,ans,cnt;
//int flag[SIZE];
void work();
void delet(int );
void init()
{
    memset(d,0,sizeof(d));
    for(int i = 0 ; i <= n ; i ++)
        q[i].clear();
    memset(no,0,sizeof(no));
    memset(vis,0,sizeof(vis));
    cnt = num = ans = 0;
}
void input()
{
    scanf("%d%d",&n,&m);
    for(int i = 1 ; i <= n ; i ++)
    {
        scanf("%d",&no[i]);
    }
    int u,v;
    for(int i = 0 ; i < m ; i ++)
    {
        scanf("%d%d",&u,&v);
        q[u].push_back(v);
        q[v].push_back(u);
        d[u] ++, d[v] ++;
    }
}
void delet(int i)
{
    d[i] --;
    vis[i] = 1;
}
void work(int p)
{
    delet(p);
    for(int i = 0 ; i <= (int)q[p].size()-1 ; i++)
    {
        int j = q[p][i];
        if(vis[j])continue;
        d[j]--;
        if(d[j] <= 1)
            work(j);
    }
}
void dfs(int i)
{
    for(int j = 0 ; j <= (int)q[i].size()-1 ; j ++)
    {
        int x = q[i][j];
        if(!vis[x])
        {
            num += no[x];
            vis[x] = 1;
            cnt ++;
            dfs(x);
        }
    }
}

int main()
{
   // freopen("in.txt","r",stdin);
    int t ;
    scanf("%d",&t );
    while(t --)
    {
        init();
        input();
        for(int i = 1 ; i <= n ; i ++)
        {
            if(d[i]<=1  && vis[i] == 0)work(i);
        }
       /* for(int i = 1; i <= n ; i ++)
        {
            printf("%d:%d\n",i,d[i]);
        }*/
        for(int i = 1 ; i <= n ; i ++)
        {
            num = cnt = 0;
            if(!vis[i])
            {
                num += no[i];
                cnt ++;
                vis[i] = 1;
                dfs(i);
            }
            if(cnt%2)
            {
                ans += num ;
            }
        }
        printf("%I64d\n",ans);

    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值