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
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
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.
#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网络赛的更多相关文章
- hdu5438 Ponds[DFS,STL vector二维数组]
目录 题目地址 题干 代码和解释 参考 题目地址 hdu5438 题干 代码和解释 解答本题时参考了一篇代码较短的博客,比较有意思,使用了STL vector二维数组. 可以结合下面的示例代码理解: ...
- hdu 5438 Ponds(长春网络赛 拓扑+bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5438 Ponds Time Limit: 1500/1000 MS (Java/Others) ...
- HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)
HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...
- 2018 ICPC 沈阳网络赛
2018 ICPC 沈阳网络赛 Call of Accepted 题目描述:求一个算式的最大值与最小值. solution 按普通算式计算方法做,只不过要同时记住最大值和最小值而已. Convex H ...
- HDU 4751 Divide Groups (2013南京网络赛1004题,判断二分图)
Divide Groups Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- HDU 4738 Caocao's Bridges (2013杭州网络赛1001题,连通图,求桥)
Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 4734 F(x) (2013成都网络赛,数位DP)
F(x) Time Limit: 1000/500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 xor (根号分治)
xor There is a tree with nn nodes. For each node, there is an integer value a_iai, (1 \le a_i \le ...
- 2012年长春网络赛(hdu命题)
为迎接9月14号hdu命题的长春网络赛 ACM弱校的弱菜,苦逼的在机房(感谢有你)呻吟几声: 1.对于本次网络赛,本校一共6名正式队员,训练靠的是完全的自主学习意识 2.对于网络赛的群殴模式,想竞争现 ...
随机推荐
- DB2中错误信息说明
DB2错误信息SQLCODE SQLSTATE (按sqlcode排序) .分类: db2数据库 2012-10-19 11:35 2942人阅读 评论(0) 收藏 举报 db2sql存储table数 ...
- Java实现文件复制的四种方式
背景:有很多的Java初学者对于文件复制的操作总是搞不懂,下面我将用4中方式实现指定文件的复制. 实现方式一:使用FileInputStream/FileOutputStream字节流进行文件的复制操 ...
- No space left on device you must specify the filesystem type--Linux重启挂在失败
在Linux中拷贝了一个文件比较大5G,直接提示:No SPace Left On Device,很明显是磁盘空间不够了,我因为是在虚拟机上面建的,直接右击虚拟机==>编辑设置 如图片1所示, ...
- max number of threads [1024] for user [lish] likely too low, increase to at least [2048]
# cat /etc/security/limits.d/-nproc.conf # Default limit for number of user's processes to prevent # ...
- CLR via C#(16)--泛型
泛型就像是一个模板,常常定义一些通用的算法,具体调用时再替换成实际的数据类型,提高了代码的可重用性. 一.初识泛型 1. 简单实例 以最常用的FCL中的泛型List<T >为例: stat ...
- js自定义延迟执行函数
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- Delphi的面向对象编程基础笔记
1.面向对象.一门面向对象的编程语言至少要实现以下三个OOP的概念 封装:把相关的数据和代码结合在一起,并隐藏细节.封装的好处是利用程序的模块化,并把代码和其他代码分开 继承:是指一个新的类能够从父类 ...
- Validform 学习笔记---基础知识整理
面对表单的验证,自己写大量的js毕竟不是一个明智的做法.不仅仅是代码很长而且不便于梳理.Validform就是一款开源的第三方验证js的控件,通过添加相应的js以及css能够有效的验证表单,维护起来也 ...
- Android 线程更新UI报错 : Can't create handler inside thread that has not called Looper.prepare()
MainActivity中有一个按钮,绑定了save方法 public void save(View view) { String title = titleText.getText().toStri ...
- .NET Framework 4 与 .NET Framework 4 Client Profile
今天碰到的一个问题和Client Profile相关的.问题是这样的:一个WPF工程,需要引用另外几个.NET的assembly, 在WPF工程中添加了对这几个assembly的引用,并在程序中可以添 ...