Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 2677    Accepted Submission(s): 850


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
 
这题可以用拓扑排序做,先删除所有能删除的点,然后再一遍dfs就行了。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 10040
int value[maxn],du[maxn],vis[maxn],n;
ll sum1,num1; struct edge{
int to,next,len;
}e[200050];
int first[maxn];
int q[1111111]; void topu()
{
int i,j,front,rear,x,xx,v;
front=1;rear=0;
for(i=1;i<=n;i++){
if(du[i]<=1){
rear++;q[rear]=i;
vis[i]=1;
}
}
while(front<=rear){
x=q[front];
front++;
for(i=first[x];i!=-1;i=e[i].next){
v=e[i].to;
if(vis[v])continue;
du[v]--;
if(du[v]<=1){
rear++;
q[rear]=v;
vis[v]=1;
}
}
}
} void dfs(int root)
{
int i,j,v;
vis[root]=1;
num1++;
sum1+=value[root];
for(i=first[root];i!=-1;i=e[i].next){
v=e[i].to;
if(!vis[v]){
dfs(v);
}
}
} int main()
{
int m,i,j,T,tot,c,d;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++){
scanf("%d",&value[i]);
}
tot=0;
memset(first,-1,sizeof(first));
memset(du,0,sizeof(du));
memset(vis,0,sizeof(vis));
for(i=1;i<=m;i++){
scanf("%d%d",&c,&d);
du[c]++;
du[d]++;
tot++;
e[tot].next=first[c];e[tot].to=d;
first[c]=tot; tot++;
e[tot].next=first[d];e[tot].to=c;
first[d]=tot; }
topu();
ll sum=0;
for(i=1;i<=n;i++){
if(vis[i])continue;
sum1=0;
num1=0;
dfs(i);
if(num1&1)sum+=sum1;
}
printf("%lld\n",sum);
}
return 0;
}

hdu5438 Ponds的更多相关文章

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

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

  2. hdu5438 Ponds dfs 2015changchun网络赛

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

  3. HDU5438:Ponds(拓扑排序)

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

  4. hdu 5438 Ponds dfs

    Time Limit: 1500/1000 MS (Java/Others)     Memory Limit: 131072/131072 K (Java/Others) Problem Descr ...

  5. hdu 5438 Ponds 拓扑排序

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

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

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

  7. 2015 ACM/ICPC Asia Regional Changchun Online Pro 1002 Ponds(拓扑排序+并查集)

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

  8. HDU 5438 Ponds

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

  9. Ponds

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

随机推荐

  1. 剑指offer 面试题3:数组中重复的数字

    题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...

  2. 天梯赛练习 L3-011 直捣黄龙 (30分) dijkstra + dfs

    题目分析: 本题我有两种思路,一种是只依靠dijkstra算法,在dijkstra部分直接判断所有的情况,以局部最优解得到全局最优解,另一种是dijkstra + dfs,先计算出最短距离以及每个点的 ...

  3. js的函数-function

    function函数 function的英文是[功能],[数] 函数:职责:盛大的集会的意思 在js里,就是函数的意思.在Java里叫做方法. 定义函数 function fun(参数){ //函数体 ...

  4. L(kali)A(apache)M(mysql)P(php)环境+wordpress站点搭建

    一:LAMP环境配置 首先LAMP(linux+apache+mysql+php)即为本次搭建网站所需的环境,由于本次使用的debian衍生版kali版本自带lamp,因此只要在服务器上启动相应服务既 ...

  5. 【Web】block、inline、inline-block元素与background属性概述(案例实现社交账号注册按钮效果)

    步骤三:社交账号注册按钮效果 文章目录 步骤三:社交账号注册按钮效果 案例的演示与分析 CSS属性与HTML标签 块级元素 内联元素 行内块级元素 CSS的display属性 CSS中的背景图片属性 ...

  6. 为什么会有 AtomicReference ?

    我把自己以往的文章汇总成为了 Github ,欢迎各位大佬 star https://github.com/crisxuan/bestJavaer 我们之前了解过了 AtomicInteger.Ato ...

  7. MySQL中UPDATE语句里SET后使用AND的执行过程和结果分析

    使用SQL中的UPDATE关键字更新多个字段值时,SET后面的更新字段应该使用逗号而不能用AND.虽然用AND不会报错,但会使更新结果错误,下面我将通过场景来分析当我们使用AND时SQL的执行过程和为 ...

  8. InnoDB的主键选择与插入优化

    索引的存放方式MyISAM和InnoDB存储引擎在MySQL中,不同存储引擎对索引的实现方式是不同的,总结下MyISAM和InnoDB两个存储引擎的索引实现方式.MyISAM引擎使用B+Tree作为索 ...

  9. postgres多知识点综合案例

    使用到的知识点: 1.使用with临时存储sql语句,格式[with as xxx(), as xxx2() ]以减少代码: 2.使用round()取小数点后几位: 3.使用to_char()将时间格 ...

  10. SRE SLO On-Call 流程机制 系统稳定性

    开篇词|SRE是解决系统稳定性问题的灵丹妙药吗? https://time.geekbang.org/column/article/212686 这两年,近距离地接触了很多不同类型.不同规模的企业 I ...