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.对于网络赛的群殴模式,想竞争现 ...
随机推荐
- 【processing】小代码
今天无意间发现的processing 很有兴趣 实现很简洁 void setup(){ } void draw(){ background(); && mouseY > heig ...
- jquery 建议编辑器
用谷歌搜索找了很久,发现所有的插件都是功能太复杂,不是我想要的.所以,我决定我自己来实现需要的编辑功能.刚开始我觉得应该要花费很多的时间,因为我想象内容编辑功能应该是很复杂的. 但事实证明,它是如此简 ...
- chrome shortcutkey
按下Shift并点击链接 – 在新窗口打开链接. Ctrl+ – 切换到最后一个标签. Ctrl+Shift+V – 将剪切板中的内容无格式粘贴(举个例子,将你从网页中复制的HTML格式内容粘贴为纯文 ...
- eclipse maven tomcat7 热部署
.配置tomcat a.配置jdk b.CATALINA_HOME=c:\tomcat CATALINA_BASE=c:\tomcat .tomcat配置密码 C:\Program Files\oth ...
- iOS中常见的一些宏
原文链接 1.处理NSLog事件(开发者模式打印,发布者模式不打印) #ifdef DEBUG #define NSLog(FORMAT, ...) fprintf(stderr,"%s:% ...
- spring 定时任务
http://zywang.iteye.com/blog/949123 xml 需要根据情况酌情删减
- java.util.ConcurrentModificationException
遍历 List 的时候将 item remove 掉会抛出此异常
- Android WebView 支持H5的定位Js
//启用数据库 webSettings.setDatabaseEnabled(true); String dir = this.getApplicationContext().getDir(" ...
- JavaScript基础——使用运算符
JavaScript运算符允许你改变一个变量的值.你已经熟悉了用于赋值给变量的=运算符.JavaScript提供了几种不同的运算符,它们可以划分为两大类:算数运算符和赋值运算符. 1.算数运算符 你可 ...
- redis 常用命令
查看redis信息和状态: > info redis下,数据库是由一个整数索引标识,而不是由一个数据库名称.默认情况下,一个客户端连接到数据库0.redis配置文件中下面的参数来控制数据库总数: ...