本文出自 http://blog.csdn.net/shuangde800
题目大意
给你一个n个点m条边的无向无环图,在尽量少的节点上放灯,使得所有边都被照亮。每盏灯将照亮以它为一个端点的所有边。
在灯的总数最小的前提下,被两盏灯同时被照亮的边数应该尽量大。
思路
这是LRJ《训练指南》上的例题。
这题教会了我一个很有用的技巧:有两个所求的值要优化,比如让a尽量小,b也尽量小
那么可以转化为让
M*a+b尽量小,其中M应该是一个比“a的最大值和b的最小值之差”还要大的数
最终的答案为ans/M, ans%M
回到这题,要求放的灯总数最小,被两盏灯同时照亮的边数尽量大。
因为每条边要么被一盏灯照亮,要么被两盏灯照亮,所以可以转换为:
求:放的灯总数量最少,被一盏灯照亮的边数尽量少。
就可以变成球 M*a+b 的最小值,a为放置的灯数量,b为被一盏灯照的边数
f[u][1]表示u点放灯时的整个子树最小值
f[u][0]表示u点不放灯时的整个子树最小值
如果u放,那么u个子结点可以选择放,也可以不放,选择其中较小的值。如果选的是不照,就要增加一条只有一个灯照的边
如果u不放,那么其子结点就必须选择要放,而且每条边都只有一个灯照
下面的我的代码和书上实现的不一样,更简洁
代码
<script src="https://code.csdn.net/snippets/563.js" type="text/javascript"></script>
分享到:
相关推荐
"Placing const in Declarations by Dan Saks"这篇文章很可能是深入探讨了如何在声明中有效地使用`const`。 1. `const`的基本概念: - `const`关键字用来定义一个只读变量,一旦赋值后就不能再次更改。 - 它可以...
【Android-Img-Placing-Game】是一款专为儿童设计的图像拖曳游戏,通过使用Android Studio开发,采用Java编程语言实现。这款游戏旨在提高孩子们的认知能力和手眼协调能力,通过将图片拖放到对应的背景位置来完成游戏...
在国际贸易中,英语函电是买卖双方沟通的重要工具,特别是在订单的放置和执行过程中。本章节将探讨"外贸英语函电"中关于"下单与执行订单"的关键知识点。 首先,我们要理解"还盘"(Counter-Offer)的概念。...
这可能涉及到构建地理位置索引,如Quadtree或K-D树,以有效地查找邻近的服务器。 3. **Java编程**:标签中提到的Java语言,意味着实现这个算法将使用Java。Java是一种跨平台的面向对象编程语言,适合开发大规模...
compatibility (EMC), using a single set of standards and placing a single mark on the products to allow them to be sold around the world. Unfortunately, that aspiration will not become reality any ...
3.5.6 Placing Decoupling Capacitors 3.5.7 布局前操作 3.5.8 Post-Placement Operations 3.5.9 Crossprobing 第4章 临时封装 4.1 概述 4.2 建立临时表贴封装 4.3 Creating Temporary Leaded Packages 第5章 电压...
(~6% increase) and placing an order (~10% increase). Furthermore, we find interesting heterogeneous treatment effects that align with social learning mechanism: the effect of the integrated Facebook ...
If we would represent the same field placing the hint numbers described above, we would end up with: *100 2210 1*10 1110 As you may have already noticed, each square may have at most 8 adjacent ...
n queen problems- c program for placing queens in a chess board
The third objective is to present design methodologies for efficiently placing on-chip decoupling capacitors in nanoscale integrated circuits. Finally, the fourth objective is to suggest novel ...
PWB provides you with a comprehensive set of tools for placing and editing objects.
This project explains an industrial process that involves picking and placing a material/product on a conveyor and a pusher pushing it to a box at a specified destination detected by a proximity ...
The Point-Feature Cartographic Label Placement (PFCLP) problem consists of placing text labels to point features on a map avoiding overlaps to improve map visualization. This paper presents a ...
The placing on the boards is slightly different around the TC3X7 itself dependent of the space (socket need more space and has through hole), but the most components are on the same location. All ...
Linqer is a software application which can be used in order ... Moreover, by placing the last mentioned files to an external data device (e.g. pen drive), you can use Linqer on any PC you can connect to.
I am placing xgboost in a directory called xgboost_install_dir but this can be anything. git clone https://github.com/dmlc/xgboost.git xgboost_install_dir copy libxgboost.dll into the xgboost_...
Placing special emphasis on creating cost-effective solutions based on real-world scenarios, the authors show how to enhance and manage the features of Access 2013 and cover critical changes that may...
Version: 2.9.0 (2018-04-03) Keil.STM32F2xx_DFP....Placing Event Recorder into non-initialized memory area. Using ARM.CMSIS-Driver.2.2.0.pack and ARM.CMSIS.5.3.0.pack. Added Network SNMP_Agent example.
I am placing xgboost in a directory called xgboost_install_dir but this can be anything. 1. git clone https://github.com/dmlc/xgboost.git xgboost_install_dir 2. copy libxgboost.dll into the ...