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 ...
随机推荐
- PC端的混合应用通讯问题
exe使用C#开发,内嵌HTML页面HTML页面与exe程序的通讯方式可以使用以下方式: HTML通知exe:C#有个titlechange事件,可以监听内部HTML的title,那么HTML就可以通 ...
- (转) 解决ssh的"Write failed: Broken pipe"问题
解决ssh的"Write failed: Broken pipe"问题 问题场景 服务器环境:阿里云 Linux CentOS 主机 客户端:Mac OSX Terminal ...
- 论文笔记之:Pedestrian Detection aided by Deep Learning Semantic Tasks
Pedestrian Detection aided by Deep Learning Semantic Tasks CVPR 2015 本文考虑将语义任务(即:行人属性和场景属性)和行人检测相结合, ...
- Docker系列之(二):使用Mesos管理Docker集群(Mesos + Marathon + Chronos + Docker)
1. Mesos简介 1.1 Mesos Apache Mesos 是一个分布式系统的管理软件,对集群的资源进行分配和管理. Mesos主要由以下几部分组成: Master: 管理各Slave节点 S ...
- 【Unity3D技巧】一个简单的Unity-UI框架的实现
如何使用 请直接导入UnityUIFramework这个UnityPackage,然后进入名为Test的Scene即可开始体验各种特性,Enjoy!你可以通过访问我的Github进行查阅和下载. Vi ...
- Comparison of B-Tree and Hash Indexes
Understanding the B-tree and hash data structures can help predict how different queries perform on ...
- “锁定”语句 lock(C# 参考)
此文章由人工翻译. 将光标移到文章的句子上,以查看原文. 更多信息. 译文 原文 "锁定"语句(C# 参考) 其他版本 <?XML:NAMESPACE PREFIX = &q ...
- 027. asp.net中数据绑定控件之 GridView控件
GridView控件支持下面的功能: 绑定至数据源控件, 如SqlDataSource 内置排序功能 内置更新和删除功能 内置分页功能 内置行选择功能 可以编程方式访问GridView对象模型以动态设 ...
- unity, 查看build版log文件
http://blog.theknightsofunity.com/accessing-unity-game-logs/
- 【转】 C#程序以管理员权限运行
C#程序以管理员权限运行在Vista 和 Windows 7 及更新版本的操作系统,增加了 UAC(用户账户控制) 的安全机制,如果 UAC 被打开,用户即使以管理员权限登录,其应用程序默认情况下也无 ...