hdu 5438 Ponds dfs
Time Limit: 1500/1000 MS (Java/Others)
Memory Limit: 131072/131072 K (Java/Others)
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> using namespace std; #define ll long long
#define push_back pb const int maxp=1e4+;
const int maxm=1e5+; struct Edge
{
int to,next;
};
Edge edge[maxm<<]; int head[maxp];
int tot;
bool vis[maxp];
int in[maxp];
ll v[maxp]; void addedge(int u,int v)
{
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
} ll solve(int ,int );
void dfs(int );
void dfs1(int ,int &,ll &); int main()
{
int test;
scanf("%d",&test);
while(test--){
tot=;
memset(head,-,sizeof head);
memset(in,,sizeof in); int p,m;
scanf("%d %d",&p,&m);
for(int i=;i<=p;i++)
scanf("%I64d",&v[i]);
int u,v;
for(int i=;i<m;i++){
scanf("%d %d",&u,&v);
addedge(u,v);
addedge(v,u);
in[u]++;
in[v]++;
} printf("%I64d\n",solve(p,m));
}
return ;
} ll solve(int p,int m)
{
memset(vis,false,sizeof vis); for(int i=;i<=p;i++){
if(!vis[i]&&in[i]<){
in[i]--;
dfs(i);
}
} ll ret=;
memset(vis,false,sizeof vis);
int num;
ll sum;
for(int i=;i<=p;i++){
if(!vis[i]&&in[i]>){
num=;
sum=;
dfs1(i,num,sum);
if(num%)
ret+=sum;
}
}
return ret;
} void dfs(int u)
{
vis[u]=true;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].to;
if(vis[v])
continue;
in[v]--;
if(in[v]<){
in[v]--;
dfs(v);
}
}
} void dfs1(int u,int &num,ll &sum)
{
vis[u]=true;
num++;
sum+=v[u];
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].to;
if(vis[v]||in[v]<)
continue;
dfs1(v,num,sum);
}
}
hdu 5438 Ponds dfs的更多相关文章
- HDU 5438 Ponds dfs模拟
2015 ACM/ICPC Asia Regional Changchun Online 题意:n个池塘,删掉度数小于2的池塘,输出池塘数为奇数的连通块的池塘容量之和. 思路:两个dfs模拟就行了 # ...
- hdu 5438 Ponds(长春网络赛 拓扑+bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5438 Ponds Time Limit: 1500/1000 MS (Java/Others) ...
- HDU 5438 Ponds (DFS,并查集)
题意:给定一个图,然后让你把边数为1的结点删除,然后求连通块结点数为奇的权值和. 析:这个题要注意,如果删除一些结点后,又形成了新的边数为1的结点,也应该要删除,这是坑,其他的,先用并查集判一下环,然 ...
- hdu 5438 Ponds 拓扑排序
Ponds Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/contests/contest_showproblem ...
- HDU 5438 Ponds
Ponds Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Sub ...
- HDU - 5438 Ponds(拓扑排序删点+并查集判断连通分量)
题目: 给出一个无向图,将图中度数小于等于1的点删掉,并删掉与他相连的点,直到不能在删为止,然后判断图中的各个连通分量,如果这个连通分量里边的点的个数是奇数,就把这些点的权值求和. 思路: 先用拓扑排 ...
- hdu5438 Ponds[DFS,STL vector二维数组]
目录 题目地址 题干 代码和解释 参考 题目地址 hdu5438 题干 代码和解释 解答本题时参考了一篇代码较短的博客,比较有意思,使用了STL vector二维数组. 可以结合下面的示例代码理解: ...
- HDU.5692 Snacks ( DFS序 线段树维护最大值 )
HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...
- HDU 5438 拓扑排序+DFS
Ponds Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Sub ...
随机推荐
- mysql数据库主从复制部署笔记
主从复制是mysql中数据库实时同步的一个常用做法了,今天我来给各位介绍一下关于mysql数据库主从复制部署一个过程,希望此例子对各位同学参考参考. 数据库主从复制原理: 数据库的主从复制就是从mas ...
- css float对于之后布局的影响
后面的元素不浮动,即便设置了宽度,表面上只占了一定的宽度,但实际上占了全屏.(所以设置了overflow之后,并且之后的div设置了宽度,再设置margin-left可能不起作用). 高度对浮动的影响 ...
- 委托,C#本身的委托(Action Func)
1.Action 分为带泛型的和不带泛型的,带泛型可传入任何类型的参数. 格式如下: using System; using System.Collections.Generic; using Sys ...
- opsview
nagios,cacti,opsview,prtg,zabbix http://www.opsview.com 1.需要注册一个账号,createyuan#sohu.com
- unity, 同步物体坐标一定要在LateUpdate中进行
设a为主动物体,b为跟随物体. 如果a,b都在同一个Update里更新坐标,那么两者自然是同步的. 如果a在a.Update里更新位置,而b在b.Update里将自己的位置更新为与a相同,那就会有误差 ...
- OpenJudge计算概论-校门外的树
/*======================================================================== 校门外的树 总时间限制: 1000ms 内存限制: ...
- CSQuery 简单测试
1. 使用环境 vs2010 ,csquery 2. 测试的内容 学习了解csquery 3. 代码 添加csquery 的就不用写了 测试的例子是获取我的博客的信息,使用webbrowser 控件 ...
- Linux-LNMP LAMP LNMPA
这个脚本是使用shell编写,为了快速在生产环境上部署lnmp/lamp/lnmpa(Linux.Nginx/Tengine.MySQL/MariaDB/Percona.PHP),适用于CentOS ...
- 【转】C#综合揭秘——通过修改注册表建立Windows自定义协议
引言 本文主要介绍注册表的概念与其相关根项的功能,以及浏览器如何通过连接调用自定义协议并与客户端进行数据通信.文中讲及如何通过C#程序.手动修改.安装项目等不同方式对注册表进行修改.其中通过安装项目对 ...
- Worktile 团队协同办公工具
Worktile是一个团队协同办公工具,通过简单的协作.沟通和分享,实现团队交互与任务管理的轻松协作.工作随身带,多平台.云数据,随时随地与团队一起工作,项目.任务.文件.讨论.文档.事件.活动流.通 ...