Time Limit: 1500/1000 MS (Java/Others)    

Memory Limit: 131072/131072 K (Java/Others)

Problem Description
Betty owns a lot of ponds, some of them are connected with other ponds by pipes, and there will not be more than one pipe between two ponds. Each pond has a value v.

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

 
Input
The first line of input will contain a number T(1≤T≤30) which is the number of test cases.

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.

 
Output
For each test case, output the sum of the value of all connected components consisting of odd number of ponds after removing all the ponds connected with less than two pipes.
 
Sample Input
1
7 7
1 2 3 4 5 6 7
1 4
1 5
4 5
2 3
2 6
3 6
2 7
 
 
Sample Output
21
 
Source
 
 
 
 
一个图,无向边,每一个节点有一个权值
若某一个节点的相连的边长数<=1,则这个节点可以去掉
主人公会继续去掉节点直到所有节点都不可以去掉为止
 
现在剩下的节点是一个个的连通分量,
主人公想知道所有所在连通分量的节点数为奇数的节点的权值总和
 
简单题
 
记录每一个节点的入度
1.第1次dfs,去掉可以去的节点
2.第2次dfs,找出哪些连通分量的节点数为奇数,并把这些节点的总权值加入到ret
 
 
 #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的更多相关文章

  1. HDU 5438 Ponds dfs模拟

    2015 ACM/ICPC Asia Regional Changchun Online 题意:n个池塘,删掉度数小于2的池塘,输出池塘数为奇数的连通块的池塘容量之和. 思路:两个dfs模拟就行了 # ...

  2. hdu 5438 Ponds(长春网络赛 拓扑+bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5438 Ponds Time Limit: 1500/1000 MS (Java/Others)     ...

  3. HDU 5438 Ponds (DFS,并查集)

    题意:给定一个图,然后让你把边数为1的结点删除,然后求连通块结点数为奇的权值和. 析:这个题要注意,如果删除一些结点后,又形成了新的边数为1的结点,也应该要删除,这是坑,其他的,先用并查集判一下环,然 ...

  4. hdu 5438 Ponds 拓扑排序

    Ponds Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/contests/contest_showproblem ...

  5. HDU 5438 Ponds

    Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Sub ...

  6. HDU - 5438 Ponds(拓扑排序删点+并查集判断连通分量)

    题目: 给出一个无向图,将图中度数小于等于1的点删掉,并删掉与他相连的点,直到不能在删为止,然后判断图中的各个连通分量,如果这个连通分量里边的点的个数是奇数,就把这些点的权值求和. 思路: 先用拓扑排 ...

  7. hdu5438 Ponds[DFS,STL vector二维数组]

    目录 题目地址 题干 代码和解释 参考 题目地址 hdu5438 题干 代码和解释 解答本题时参考了一篇代码较短的博客,比较有意思,使用了STL vector二维数组. 可以结合下面的示例代码理解: ...

  8. HDU.5692 Snacks ( DFS序 线段树维护最大值 )

    HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...

  9. HDU 5438 拓扑排序+DFS

    Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Sub ...

随机推荐

  1. mysql数据库主从复制部署笔记

    主从复制是mysql中数据库实时同步的一个常用做法了,今天我来给各位介绍一下关于mysql数据库主从复制部署一个过程,希望此例子对各位同学参考参考. 数据库主从复制原理: 数据库的主从复制就是从mas ...

  2. css float对于之后布局的影响

    后面的元素不浮动,即便设置了宽度,表面上只占了一定的宽度,但实际上占了全屏.(所以设置了overflow之后,并且之后的div设置了宽度,再设置margin-left可能不起作用). 高度对浮动的影响 ...

  3. 委托,C#本身的委托(Action Func)

    1.Action 分为带泛型的和不带泛型的,带泛型可传入任何类型的参数. 格式如下: using System; using System.Collections.Generic; using Sys ...

  4. opsview

    nagios,cacti,opsview,prtg,zabbix http://www.opsview.com 1.需要注册一个账号,createyuan#sohu.com

  5. unity, 同步物体坐标一定要在LateUpdate中进行

    设a为主动物体,b为跟随物体. 如果a,b都在同一个Update里更新坐标,那么两者自然是同步的. 如果a在a.Update里更新位置,而b在b.Update里将自己的位置更新为与a相同,那就会有误差 ...

  6. OpenJudge计算概论-校门外的树

    /*======================================================================== 校门外的树 总时间限制: 1000ms 内存限制: ...

  7. CSQuery 简单测试

    1. 使用环境 vs2010  ,csquery 2. 测试的内容 学习了解csquery 3. 代码 添加csquery 的就不用写了 测试的例子是获取我的博客的信息,使用webbrowser 控件 ...

  8. Linux-LNMP LAMP LNMPA

    这个脚本是使用shell编写,为了快速在生产环境上部署lnmp/lamp/lnmpa(Linux.Nginx/Tengine.MySQL/MariaDB/Percona.PHP),适用于CentOS ...

  9. 【转】C#综合揭秘——通过修改注册表建立Windows自定义协议

    引言 本文主要介绍注册表的概念与其相关根项的功能,以及浏览器如何通过连接调用自定义协议并与客户端进行数据通信.文中讲及如何通过C#程序.手动修改.安装项目等不同方式对注册表进行修改.其中通过安装项目对 ...

  10. Worktile 团队协同办公工具

    Worktile是一个团队协同办公工具,通过简单的协作.沟通和分享,实现团队交互与任务管理的轻松协作.工作随身带,多平台.云数据,随时随地与团队一起工作,项目.任务.文件.讨论.文档.事件.活动流.通 ...