hdu5438 Ponds dfs 2015changchun网络赛

Ponds

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

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

Source

2015 ACM/ICPC Asia Regional Changchun Online

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
using namespace std;
int p, m;
const int M = 100005;
const int N = 10005;
typedef long long ll;
struct edge
{
    int v, to;
    edge() { };
    edge(int v, int to): v(v), to(to) {};
} e[M];

int head[N], flag[N], val[N], in[N], vis[N], tot;
queue<int> q;

void init()
{
    memset(head, -1, sizeof head);
    memset(flag, 0, sizeof flag);
    memset(vis, 0, sizeof vis);
    memset(in, 0, sizeof in);
    tot = 0;
    while(!q.empty()) q.pop();
}

void addedge(int u, int v)
{
    e[tot] = edge(v, head[u]);
    head[u] = tot++;
}

void dfs(int u)
{
    for(int i = head[u]; i != -1; i = e[i].to)
    {
        int v = e[i].v;
        if(in[v] == 0) continue;
        if(flag[v]) continue;
        in[v]--;
        in[u]--;
        if(in[v] == 1)
        {
            q.push(v);
            flag[v] = 1;
        }
    }
}

void pre()
{
    for(int i = 1; i <= p; ++i)
    {
        if(in[i] == 1)
        {
            flag[i] = 1;
            q.push(i);
        }
    }

    while(!q.empty())
    {
        int f = q.front();
        q.pop();
        dfs(f);
    }
}

int cnt;
ll sum;
void calc(int u)
{
    cnt++;
    vis[u] = 1;
    sum += val[u];
    for(int i = head[u]; i != -1; i = e[i].to)
    {
        int v = e[i].v;
        if(vis[v]) continue;
        if(flag[v]) continue;

        calc(v);

    }
}

int main()
{
    int _;
    scanf("%d", &_);
    while(_ --)
    {
        scanf("%d%d", &p, &m);
        int u, v;
        for(int i = 1; i <= p; ++i) scanf("%d", &val[i]);
        init();
        for(int i = 0; i < m; ++i)
        {
            scanf("%d%d", &u, &v);
            in[u]++;
            in[v]++;
            addedge(u, v);
            addedge(v, u);
        }
        pre();
        ll ans = 0;
        for(int i = 1; i <= p; ++i) if(!flag[i] && !vis[i]) {
                cnt = 0;
                sum = 0;
                calc(i);
                if((cnt & 1) && cnt !=1)  ans += sum;
            }
        printf("%lld\n", ans);
    }
    return 0;
}

  

时间: 2024-10-21 01:18:17

hdu5438 Ponds dfs 2015changchun网络赛的相关文章

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

题目链接: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, so

hdu 5438 Ponds(长春网络赛 拓扑+bfs)

题目链接: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): 2237    Accepted Submission(s): 707 Problem Description Betty owns a lot of ponds, som

2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛

Banana Bananas are the favoured food of monkeys. In the forest, there is a Banana Company that provides bananas from different places. The company has two lists. The first list records the types of bananas preferred by different monkeys, and the seco

HDU 5024 Wang Xifeng&#39;s Little Plot(2014广州网络赛1003)

写了1h的DFS,简直被自己的代码吓哭了..不过起码还是思路清晰,QUQ~ 说一下题意吧: 题意是求一条最长路,最多能经过一次转弯,并且其角度只能为90度. 拿第一个样例来说:(0,1)->(1,2)->[转弯](2,1) ,所以答案是3. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5024 代码如下: #include<iostream> #include<cstdio> #include<cstring>

2016 CCPC 网络赛 B 高斯消元 C 树形dp(待补) G 状压dp+容斥(待补) H 计算几何

2016 CCPC 网络赛 A - A water problem 水题,但读题有个坑,输入数字长度很大.. B - Zhu and 772002 题意:给出n个数(给出的每个数的质因子最大不超过2000),选出多个数相乘得b.问有多少种选法让b 为完全平方数. tags:高斯消元,求异或方程组解的个数.   好题 每个数先素数分解开.  对于2000以内的每个素数p[i],这n个数有奇数个p[i]则系数为1,偶数个则系数为0,最后n个数的p[i]系数异或和都要为0才会使得最后的积为完全平方数.

HDU - 4734 F(x) (2013成都网络赛,数位DP)

题意:求0-B的满足<=F[A]的所有可能 思路:数位DP,记忆化搜索 #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; int A, B; int dp[20][200000]; int bit[20]; int dfs(int cur, int num, int flag) { if (cur == -

HDU 5024 Wang Xifeng&#39;s Little Plot(广州网络赛C题)

HDU 5024 Wang Xifeng's Little Plot 题目链接 思路:先利用记忆化搜索预处理出每个结点对应8个方向最远能走多远,然后枚举拐点记录最大值即可 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int d[8][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}, {-1, 1}, {1,

HDU 5024 (广州网络赛) Wang Xifeng&#39;s Little Plot 记忆化搜索+枚举

Problem Description <Dream of the Red Chamber>(also <The Story of the Stone>) is one of the Four Great Classical Novels of Chinese literature, and it is commonly regarded as the best one. This novel was created in Qing Dynasty, by Cao Xueqin.

2019-ACM-ICPC-南京区网络赛-D. Robots-DAG图上概率动态规划

2019-ACM-ICPC-南京区网络赛-D. Robots-DAG图上概率动态规划 [Problem Description] ? 有向无环图中,有个机器人从\(1\)号节点出发,每天等概率的走到下一个节点或者停在当前节点,并且第\(i\)天消耗\(i\)的耐久度.求它到达\(n\)号节点时期望消耗的耐久度是多少? ? 题目保证只有一个入度为\(0\)的节点,只有一个出度为\(0\)的节点. [Solution] ? 概率\(dp\). ? 假设每天消耗\(1\)点耐久度.定义\(dp[u]\