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 ...
随机推荐
- 【ORA】ORA-39002,ORA-39070,ORA-29283, ORA-06512,ORA-29283解决办法
今天使用IMPDP导入的时候报了一个错误 ORA-39002: invalid operation ORA-39070: Unable to open the log file. ORA-2928 ...
- 【Oracle】userenv()函数介绍分析
说到这个userenv()使用起来还是很有用的 参数 功能 CLINET_INFO 返回最高可达64个字节存储的用户会话信 ...
- Apache目录详解
Apache的主要目录和配置文件理解 参考链接:http://httpd.apache.org/docs/2.4/misc/security_tips.html 一.Apache主要配置文件注释(演示 ...
- 如何查看U盘的VID和PID
1.将USB插入电脑 2.右键单击[此电脑],选择[管理] 3.在弹出的对话框中选择[设备管理器],选择[USB大容量存储设备] 4.右键单击[USB大容量存储设备],选择[属性],在弹出的对话框中选 ...
- WTM5.0发布,全面支持.net5
WTM5.0是WTM框架开源2年以来最大的一次升级,全面支持.net5,大幅重构了底层代码,针对广大用户提出的封装过度,不够灵活,性能不高等问题进行了彻底的修改. 这次升级使WTM继续保持开箱即用,高 ...
- 5V 升压 8.4V,5V 转 8.4V 做两节锂电池充电芯片
5V 升压 8.4V SOT23-6 封装的六脚升压 IC PW5300 是一颗 DC-DC 异步整流升压转换器芯片,输入电压范围 2.6V-5.5V.最高输出 电压 12V, PW5300 是一种电 ...
- 05. struts2中为Action属性注入值
概述 struts2为Action中的属性提供了依赖注入功能 在struts2的配置文件中,我们可以很方便地为Action中的属性注入值.注意:属性必须提供get,set方法. 配置 <acti ...
- mysql(连接查询和数据库设计)
--创建学生表 create table students ( id int unsigned not null auto_increment primary key, name varchar(20 ...
- SuperUpdate.sh 一键更换Linux软件源脚本
一.前言 有时候会遇到 Linux 的源更新速度非常的缓慢,特别是在国内使用默认的源,因为国内的网络环境,经常会出现无法更新,更新缓慢的情况.在这种情况下,更换一个更适合或者说更近,更快的软件源,会为 ...
- 不支持的字符集 (在类路径中添加 orai18n.jar): ZHS16GBK
不支持的字符集 (在类路径中添加 orai18n.jar): ZHS16GBK 报错图示: 报错内容: Exception in thread "main" java.sql.SQ ...