hdu5438 Ponds
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2677 Accepted Submission(s): 850
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
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.
7 7
1 2 3 4 5 6 7
1 4
1 5
4 5
2 3
2 6
3 6
2 7
#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的更多相关文章
- hdu5438 Ponds[DFS,STL vector二维数组]
目录 题目地址 题干 代码和解释 参考 题目地址 hdu5438 题干 代码和解释 解答本题时参考了一篇代码较短的博客,比较有意思,使用了STL vector二维数组. 可以结合下面的示例代码理解: ...
- hdu5438 Ponds dfs 2015changchun网络赛
Ponds Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Sub ...
- HDU5438:Ponds(拓扑排序)
Ponds Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Sub ...
- hdu 5438 Ponds dfs
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem Descr ...
- hdu 5438 Ponds 拓扑排序
Ponds Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/contests/contest_showproblem ...
- hdu 5438 Ponds(长春网络赛 拓扑+bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5438 Ponds Time Limit: 1500/1000 MS (Java/Others) ...
- 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 ...
- HDU 5438 Ponds
Ponds Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Sub ...
- Ponds
Ponds Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Sub ...
随机推荐
- 详解Vue中的computed和watch
作者:小土豆 博客园:https://www.cnblogs.com/HouJiao/ 掘金:https://juejin.cn/user/2436173500265335 1. 前言 作为一名Vue ...
- .NET 5 程序高级调试-WinDbg
上周和大家分享了.NET 5开源工作流框架elsa,程序跑起来后,想看一下后台线程的执行情况.抓了个进程Dump后,使用WinDbg调试,加载SOS调试器扩展,结果无法正常使用了: 0:000> ...
- Kafka底层原理剖析(近万字建议收藏)
Kafka 简介 Apache Kafka 是一个分布式发布-订阅消息系统.是大数据领域消息队列中唯一的王者.最初由 linkedin 公司使用 scala 语言开发,在2010年贡献给了Apache ...
- char什么时候会用空格进行填充?
char什么时候会用空格进行填充?
- 【IMP】IMP导入表的时候,如果表存在怎么办
在imp导入的时候,如果表存在的话,会追加数据在表中, 所以如果不想追加在表中的话,需要将想导入的表truncate掉后,在imp SQL: truncate table TEST1; imp tes ...
- P2327 [SCOI2005]扫雷(递推)
题目链接: https://www.luogu.org/problemnew/show/P2327 题目描述 相信大家都玩过扫雷的游戏.那是在一个$n*m$的矩阵里面有一些雷,要你根据一些信息找出雷来 ...
- 开发进阶:Dotnet Core多路径异步终止
今天用一个简单例子说说异步的多路径终止.我尽可能写得容易理解吧,但今天的内容需要有一定的编程能力. 今天这个话题,来自于最近对gRPC的一些技术研究. 话题本身跟gRPC没有太大关系.应用中,我用 ...
- DataGridView控件使用Demo
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- scrapy框架的中间件
中间件的使用 作用:拦截所有的请求和响应 拦截请求:process_request拦截正常的请求,process_exception拦截异常的请求 篡改请求的头信息 def process_reque ...
- 小白都看得懂的Javadoc使用教程
Javadoc是什么 官方回答: Javadoc is a tool for generating API documentation in HTML format from doc comments ...