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
#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;
}

  

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

  1. hdu5438 Ponds[DFS,STL vector二维数组]

    目录 题目地址 题干 代码和解释 参考 题目地址 hdu5438 题干 代码和解释 解答本题时参考了一篇代码较短的博客,比较有意思,使用了STL vector二维数组. 可以结合下面的示例代码理解: ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5438 Ponds Time Limit: 1500/1000 MS (Java/Others)     ...

  3. HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)

    HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...

  4. 2018 ICPC 沈阳网络赛

    2018 ICPC 沈阳网络赛 Call of Accepted 题目描述:求一个算式的最大值与最小值. solution 按普通算式计算方法做,只不过要同时记住最大值和最小值而已. Convex H ...

  5. HDU 4751 Divide Groups (2013南京网络赛1004题,判断二分图)

    Divide Groups Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  6. HDU 4738 Caocao's Bridges (2013杭州网络赛1001题,连通图,求桥)

    Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

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

    F(x) Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  8. 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 xor (根号分治)

    xor There is a tree with nn nodes. For each node, there is an integer value a_ia​i​​, (1 \le a_i \le ...

  9. 2012年长春网络赛(hdu命题)

    为迎接9月14号hdu命题的长春网络赛 ACM弱校的弱菜,苦逼的在机房(感谢有你)呻吟几声: 1.对于本次网络赛,本校一共6名正式队员,训练靠的是完全的自主学习意识 2.对于网络赛的群殴模式,想竞争现 ...

随机推荐

  1. 【linux】进程不要开太多,否则系统会卡死!

    今天在跑一个任务,大概像下面这样 python task.py -s input/ input文件夹下有两百多个文件,比如1.txt, 2.txt等等,task.py会顺序读取并做操作. 我想,这不是 ...

  2. 中等难度SQL语句(存储过程,分页,拼接字段、游标,日期类型转换,动态行转列,视图)汇总

    一.创建存储过程 if Exists(select name from sysobjects where NAME = 'sp1LoginUser' and type='P')drop procedu ...

  3. LeetCode 409 Longest Palindrome

    Problem: Given a string which consists of lowercase or uppercase letters, find the length of the lon ...

  4. CocoaPods安装教程

    Code4App 原创文章.转载请注明出处:http://code4app.com/article/cocoapods-install-usage CocoaPods是什么? 当你开发iOS应 用时, ...

  5. JS图表插件(柱形图、饼状图、折线图)

    http://www.open-open.com/lib/view/open1406378625726.html

  6. jquery this 与javascript的this

    <div class="list"> <table> <thead> <tr> <th width="110&quo ...

  7. Javascript中最常用的55个经典技巧(转)

    1. oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键 <table border oncontextmenu ...

  8. C# 泛型约束

    一.泛型简介1.1泛型通过使用泛型,可以创建这样的类.接口和方法,它们以一种类型安全的工作方式操作各种数据.本质上,术语“泛型”指的是“参数化类型”(parameterized types).参数化类 ...

  9. JNDI 和JDBC的区别

    1.JNDI 和JDBC的区别和联系.两者都是API,是一个标准.并不是什么产品或方法.JDBC 全称:Java Database Connectivity 以一种统一的方式来对各种各样的数据库进行存 ...

  10. php 分页

    分页类 <?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 private ...