当前位置: 首页 > 编程日记 > 正文

hdu 5438 Ponds 拓扑排序

Ponds

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1001&cid=621

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

HINT

题意

给你一个图,然后要求把度数小于2的点全部删去,然后问你奇数集合的点权和是多少

注意,你删去了一个点之后,可能会使得一些点又变成了度数小于2的

题解:

用类似拓扑排序的思想去做就ok啦

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>using namespace std;const int N=200100;
long long a[N],ans;
int n,m,T,cnt,ok[N],vis[N],pre[N],nxt[N],to[N],tot[N],col;
vector<int> s[N];
queue<int> q;void dfs(int x,int fa)
{s[col].push_back(x);ok[x]=0;for(int p=pre[x];p!=-1;p=nxt[p]){if((!vis[p])||(!ok[to[p]])) continue;if(p==(fa^1)) continue;dfs(to[p],p);}
}
void makeedge(int x,int y)
{to[cnt]=y;nxt[cnt]=pre[x];pre[x]=cnt++;to[cnt]=x;nxt[cnt]=pre[y];pre[y]=cnt++;
}int main()
{scanf("%d",&T);while(T--){memset(tot,0,sizeof(tot));memset(pre,-1,sizeof(pre));memset(ok,1,sizeof(ok));memset(vis,1,sizeof(vis));ans=0LL;cnt=0;scanf("%d%d",&n,&m);for(int i=1;i<=n;i++){scanf("%I64d",&a[i]);}for(int i=0;i<m;i++){int x,y;scanf("%d%d",&x,&y);tot[x]++;tot[y]++;makeedge(x,y);}while(!q.empty()) q.pop();for(int i=1;i<=n;i++){if(tot[i]<2){q.push(i);}}while(!q.empty()){int x=q.front();q.pop();ok[x]=0;for(int p=pre[x];p!=-1;p=nxt[p]){vis[p]=0;tot[x]--;tot[to[p]]--;if(ok[to[p]]&&tot[to[p]]<2){q.push(to[p]);}}}col=0;for(int i=1;i<=n;i++){col++;s[col].clear();if(ok[i]){dfs(i,cnt+10);if(s[col].size()%2==1){for(int j=0;j<s[col].size();j++){ans+=a[s[col][j]];}}}}printf("%I64d\n",ans);}return 0;
}

转载于:https://www.cnblogs.com/qscqesze/p/4805256.html

相关文章:

CentOS6安装nodejs

Nodejs是JavaScript的一种运行环境&#xff0c;是一个服务端的JavaScript解释器。 NPM是Nodejs的包管理器。 Nodejs包含npm&#xff0c;所以安装完nodejs后npm默认也被安装。 安装步骤&#xff1a; # /usr/local/srcwget http://nodejs.org/dist/v6.7.0/node-v6.7.0-linux-…

Codeforces Round #FF 446 C. DZY Loves Fibonacci Numbers

參考&#xff1a;http://www.cnblogs.com/chanme/p/3843859.html 然后我看到在别人的AC的方法里还有这么一种神方法&#xff0c;他预先设定了一个阈值K,当当前的更新操作数j<K的时候&#xff0c;它就用一个类似于树状数组段更的方法&#xff0c;用一个 d数组去存内容&#x…

Java 基本概念

Java 基本概念 1. Java 语言的优点? 简单、高效 Java 语言与 C 类似&#xff0c;如果用户了解 C 和面向对象的概念&#xff0c;就可以很快编写出 Java 程序&#xff1b;此外&#xff0c;Java 又不同于诸如 C 语言提供的各种各样的方法&#xff0c;它只提供了基本的方法&#x…

list_for_each_safe

list_for_each_safeBidirect-list list_for_each_safe().https://biscuitos.github.io/blog/LIST_list_for_each_safe/

oracle恢复是怎么看进度,Oracle中查看慢查询进度的脚本分享

Oracle一个大事务的sql往往不知道运行到了哪里,可以使用如下sql查看执行进度。代码如下:404_6set linesize 400;H_404_6set pagesize 400;H_404_6col sql_text format a100;H_404_6col opname format a15;H_404_6SELECT se.sid,H_404_6opname,H_404_6TRUNC (sofar / totalwork …

第三周 9.13-9.19

9.13 长春OL。 9.14-9.15 什么都没干。 9.16-9.17 补题。 9.18 什么都没干。 9.19 沈阳OL。 本周就是什么都没干。转载于:https://www.cnblogs.com/Aguin/p/4805509.html

vue-devTools插件安装流程

vue-devTools插件安装流程 本文主要介绍 vue的调试工具 vue-devtools 的安装和使用 工欲善其事, 必先利其器, 快快一起来用vue-devtools来调试开发你的vue项目吧 安装: 1.到github下载&#xff1a;&#xff08;下载一定要记得是master环境的代码&#xff0c;默认克隆后进入…

基于ipfire的open***功能--client to net (Roadwarrior)配置(一)

Client-to-Net configuration (Roadwarrior)全局配置第一步应该是生成服务证书来激活ipfire上的open***。完成这个后&#xff0c;全局配置就可以使用了。为了激活open***所需的接口&#xff0c;open***服务监听的地方&#xff0c;你需要勾选界面里的方框。如何勾选&#xff0c;…

oracle update from多表性能优化一例

这几天测试java内存数据库&#xff0c;和oracle比较时发下一个update from语句很慢&#xff0c;如下&#xff1a; update business_newset fare1_balance_ratio (select BALANCE_RATIO from bfare2where bfare2.exchange_type business_new.exchange_type andbfa…

Sorry, Sarah

Sorry, Sarah

C#中Winform程序中如何实现多维表头【不通过第三方报表程序】

问题&#xff1a;C#中Winform程序中如何实现多维表头。 在网上搜了很多方法&#xff0c;大多数方法对于我这种新手&#xff0c;看的都不是很懂。最后在新浪博客看到了一篇比较易懂的文章&#xff1a;【DataGridView二维表头与合并单元格】 大体的思路如下&#xff1a; 1.新建一…

斗地主发牌编程PHP,JAVA代码之斗地主发牌详解

package com.oracle.demo01;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.Map;public class Doudizhu {public static void main(String[] args) {//1.创建扑克牌MapMap pookernew HashMap();//创建所有key所在的容器A…

2022-2028年中国自动化设备市场研究及前瞻分析报告

【报告类型】产业研究 【报告价格】4500起 【出版时间】即时更新&#xff08;交付时间约3个工作日&#xff09; 【发布机构】智研瞻产业研究院 【报告格式】PDF版 本报告介绍了中国自动化设备行业市场行业相关概述、中国自动化设备行业市场行业运行环境、分析了中国自动化…

转发:某些函数需要将其一个或多个实参连同类型不变地转发给其他函数

16.47 编写你自己版本的翻转函数&#xff0c;通过调用接受左值和右值引用参数的函数来测试它。 #include<iostream> #include<string> #include<utility> using namespace std;template <typename T> int compare(const T &a ,const T &b) {if…

pycharm远程调试或运行代码

第一步&#xff1a;开始 第二步&#xff1a;设置远程服务器 第三步&#xff0c;查看 第四步&#xff0c;选择解释器&#xff0c;和指定文件映射路径&#xff08;相对上一步指定的相对路径&#xff09; 转载于:https://www.cnblogs.com/jeshy/p/11182359.html

LTE随机接入过程

随机接入的基本流程 Msg3和Msg4只有基于竞争的随机接入才存在&#xff0c;之所以叫Msg3/Msg4是因为不同的随机接入情况&#xff0c;Msg3/Msg4的消息不相同(本文稍后介绍)。 下图中的参数<ra-ResponseWindowSize>和<mac-ContentionResolutionTimer>来自SIB2中的rach…

workday与oracle,workingday与workday的区别 – 手机爱问

2005-04-11for的用法&#xff26;&#xff2f;&#xff32;到底应该怎么用&#xff1f;对于for的用法的确很多&#xff0c;可用作介词和连词&#xff0c;介词用法尤为丰富。以下详细列出了用法和句例&#xff0c;供你参考。for 1 preposition1used to say who is intended to g…

OGRE 2.1 Windows 编译

版权所有&#xff0c;转载请注明链接 OGRE 2.1 Windows 编译 环境&#xff1a;  Windows 7 64Bit  Visual Studio 2012  OGRE 2.1  CMake 2.8.12.1 OGRE&#xff1a;  OGRE官方推出了最新的OGRE2.1版本&#xff0c;链接地址&#xff1a;    https://bitbucket.or…

IDEA集成Docker插件实现一键自动打包部署微服务项目

一. 前言 大家在自己玩微服务项目的时候&#xff0c;动辄十几个服务&#xff0c;每次修改逐一部署繁琐不说也会浪费越来越多时间&#xff0c;所以本篇整理通过一次性配置实现一键部署微服务&#xff0c;实现真正所谓的一劳永逸。 二. 配置服务器 1. Docker安装 服务器需要安…

PHP的学习--PHP的引用

引用是什么 在 PHP 中引用意味着用不同的名字访问同一个变量内容。这并不像 C 的指针&#xff0c;替代的是&#xff0c;引用是符号表别名。注意在 PHP 中&#xff0c;变量名和变量内容是不一样的&#xff0c;因此同样的内容可以有不同的名字。最接近的比喻是 Unix 的文件名和文…

谈一谈浏览器解析CSS选择器的过程【前端每日一题-6】

谈一谈浏览器解析CSS选择器的过程&#xff1f; 这是一道发散题&#xff0c;可以根据自己的理解自行解答。 在开始前&#xff0c;我们必须了解一个真相&#xff1a;为什么排版引擎解析 CSS 选择器时一定要从右往左解析&#xff1f; 简单的来说&#xff1a;浏览器从右到左进行查找…

LTE: MIB和SIB,小区选择和重选规则

LTE 中MIB/SIB内容可以参考&#xff1a;https://blog.csdn.net/wowricky/article/details/51348613 MIB/SIB的详细内容参考下面两张图 MIB,SIB1,SIB2 可以关注下小区选择的参数&#xff0c;用特殊颜色表示 36.304 - 5.2.3.2 Cell Selection Criterion S准则&#xff0c;需要…

linux 生成dll文件,Linux和Windows平台 动态库.so和.dll文件的生成

Linux动态库的生成1、 纯cpp文件打包动态库将所有cpp文件和所需要的头文件放在同一文件夹&#xff0c;然后执行下面命令gcc -shared - fpic *.c -o xxx.so&#xff1b;g -stdc17 - fpic *.cpp -o xxx.so&#xff1b;[C17标准&#xff0c;需要高版本gcc&#xff0c;本人采用gcc …

Form表单提交前进行JS验证的3种方式

1. 提交按钮的onclick事件中验证 <script type"text/javascript"> function check(form) { return true; }</script> <form> <input type"submit" name"submit1" value"登陆" οnc…

2022-2028年中国椎间孔镜行业市场研究及前瞻分析报告

【报告类型】产业研究 【报告价格】4500起 【出版时间】即时更新&#xff08;交付时间约3个工作日&#xff09; 【发布机构】智研瞻产业研究院 【报告格式】PDF版 本报告介绍了中国椎间孔镜行业市场行业相关概述、中国椎间孔镜行业市场行业运行环境、分析了中国椎间孔镜行…

mysql 错误:1166 解决办法

原因&#xff1a;检查字段里面是不是有空格&#xff0c;去掉就可以了转载于:https://www.cnblogs.com/zhizhan/p/3950453.html

优先级队列(小顶堆)的dijkstra算法

php实现迪杰斯特拉算法&#xff0c;并由小顶堆优化 1 <?php2 3 class DEdge4 {5 public $nextIndex, $length;6 7 public function __construct($nextIndex, $length)8 {9 $this->nextIndex $nextIndex;10 $this->length $length;11 …

室内设计木地板材质合集包 Arroway – Design Craft Vol.4

室内设计木地板材质合集包 Arroway – Design Craft Vol.4 室内设计木地板材质合集包 Arroway – Design Craft Vol.4 阿洛维——设计工艺第四卷 大小&#xff1a;20G 信息: 云桥网络 平台获取素材&#xff01; 36种单板纹理 纹理包括漫反射、法线、凹凸、反射率、环境遮挡…

linux下有关phy的命令,linux – 如何为Debian安装b43-lpphy-installer?

b43-lpphy-installer是Ubuntu的包的名称,而不是Debian的包.你可以在jessie(Debian 8)中使用命令安装它&#xff1a;sudo apt-get install firmware-b43-installer通过内核版本,您似乎正在使用Debian 8.要了解有关debian软件包的详细信息,您可以按名称或文件搜索软件包&#xff…

Idea SpringBoot 基于 Docker容器环境进行远程调试

远程服务环境要求 对启动的jar服务命令进行修改&#xff0c;改成远程调试模式启动 eg: java -jar -agentlib:jdwptransportdt_socket,servery,suspendn,address18761 app.jar此命令特别之处是 关注监听端口&#xff1a;address18761&#xff0c;这端口号随性定义。 -agentl…