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.对于网络赛的群殴模式,想竞争现 ...
随机推荐
- 【数据结构】红黑树 C语言代码
连看带写花了三天,中途被指针引用搞得晕晕乎乎的. 插入和删除的调整过程没有看原理,只看了方法,直接照着写的. 看了两份资料,一份是算法导论第12-13章, 另一份是网上的资料http://blog.c ...
- HDU 4950 Monster
题目链接 题意:有一个怪物的血量为h,人攻击怪物,每回合可以杀掉a滴血,再回b滴血,k个回合之后人会休息一回合,即人不攻击而怪物回b滴血,问能否杀死.翻译过来就是给定一个数h,每轮可以先减a再加b,k ...
- java”伪“批量上传
jsp页面代码 <form method="post" action="" enctype="multipart/form-data" ...
- Java容器题库
一. 填空题 Java集合框架提供了一套性能优良.使用方便的接口和类,包括Collection和Map两大类,它们都位于 java.util 包中 队列和堆栈有些相似,不同之处在于栈是先进后 ...
- 多线程编程1 - NSThread
每个iOS应用程序都有个专门用来更新显示UI界面.处理用户的触摸事件的主线程,因此不能将其他太耗时的操作放在主线程中执行,不然会造成主线程堵塞(出现卡机现象),带来极坏的用户体验.一般的解决方案就是将 ...
- NYOJ题目198数数
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsYAAAK1CAIAAABEvL+NAAAgAElEQVR4nO3drXLkurvv8X0T4bmQYF
- PHP中的常用魔术方法
魔术方法: 是指某些情况下,会自动调用的方法,称为魔术方法 php面向对象中,提供了这几个魔术方法,他们的特点都是 以双下划线__开头的 __construct() 构造方法 __destruct( ...
- jq 全选和反选以及判断那条被选中
<body><div><input type="checkbox" id="a" />全选</div><d ...
- 微信支付 - V3退款
退款问题: 1.证书加载不进去,出现"内部错误" 解决:在iis中找到对应的应用连接池,右键高级设置,找到"加载用户配置文件"改为true. 2.需 ...
- WMI测试
https://social.msdn.microsoft.com/Forums/windowshardware/zh-CN/c5af7959-95d3-4e1b-ab40-96a2a31c2af2/ ...