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.对于网络赛的群殴模式,想竞争现 ...
随机推荐
- 【编程题目】输入两个整数 n 和 m,从数列 1,2,3.......n 中 随意取几个数, 使其和等于 m ... ★
第 21 题(数组)2010 年中兴面试题编程求解:输入两个整数 n 和 m,从数列 1,2,3.......n 中 随意取几个数,使其和等于 m ,要求将其中所有的可能组合列出来. 我的思路: 从小 ...
- php安全编程: register_globals的安全性
register_globals?本身并非安全风险.但是,它为跟踪用户输入和确保应用程序安全增加了难度.为什么会这样? 因为如果打开 register_globals,在全局名称空间和 $_GET.$ ...
- (转)Delphi工程文件说明
1.DPR: Delphi Project文件,包含了Pascal代码.应用系统的工程文件2.PAS: Pascal文件,Pascal单元的源代码,可以是与窗体有关的单元或是独立的单元.3.DFM:D ...
- Yahoo!网站性能最佳体验的34条黄金守则(转载)
1. 尽量减少HTTP请求次数 终端用户响应的时间中,有80%用于下载各项内容.这部分时间包括下载页面中的图像.样式表.脚本.Flash等.通过减少页面中的元素可以减少HTTP请求的次数 ...
- 靶形数独(codevs 1174)
1174 靶形数独 2009年NOIP全国联赛提高组 时间限制: 4 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Descri ...
- TCP/IP五层模型
(2)TCP/IP五层模型的协议 应用层 传输层 网络层 数据链路层 物理层 物理层:中继器.集线器.还有我们通常说的双绞线也工作在物理层 数据链路层:网桥(现已很少使用).以太网交换机(二层 ...
- myaql常用函数
一.数学函数 abs(x) 返回x的绝对值 bin(x) 返回x的二进制(oct返回八进制,hex返回十六进制) ceiling(x) 返回大于x的最小整数值 exp(x) 返回值e( ...
- Android Programming: Pushing the Limits -- Chapter 1: Fine-Tuning Your Development Environment
ADB命令 Application Exerciser Monkey Gradle ProGuard 代码重用 版本控制 静态代码分析 代码重构 开发者模式 ADB命令: @.adb help:查 ...
- 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 # ...
- android 入门-布局
android:gravity 针对本view 的位置. android:layout_gravity 本view相对于父布局view的位置. android:layout_alignParentRi ...