Question: Click to open the link
Theme
Give you a n-point m-side undirected acyclic graph, light on as few nodes as possible, so that all sides are illuminated. Each light will illuminate all edges with it as an endpoint.
When the total number of lights is the smallest, the number of edges simultaneously illuminated by two lights should be as large as possible.
Ideas
This is an example of LRJ getting started classic.
This topic taught me a very useful technique: There are two values to be optimized, for example, to make a as small as possible, B as small as possible
It can be converted to making M * a + B as small as possible, where M should be a larger number than "the difference between the maximum value of a and the minimum value of B"
The final answer is ans/M, ans % M.
Back to this question, the minimum number of lights is required, and the number of sides simultaneously illuminated by two lights is as large as possible.
Because each side is either illuminated by a light or illuminated by two lights, it can be converted:
The total number of lights is the minimum, and the number of sides illuminated by a lamp is as small as possible.
It can be changed to the minimum value of the Ball M * a + B. a indicates the number of lights placed, and B indicates the number of edges illuminated by a lamp.
F [u] [1] indicates the minimum value of the entire subtree when the light is switched on at the u point.
F [u] [0] indicates the minimum value of the entire subtree when the lamp is not switched on at the u point.
If u is placed, you can select or not to place the u subnode, and select a smaller value. If you choose not to take a picture, you need to add an edge with only one light.
If u is not placed, the child node must be placed, and each side has only one light.
The following code is different from what is implemented in the book, which is more concise.
Code/** ============================================ ====== * This is a solution for ACM/ICPC problem ** @ source: uva-10859 Placing Lampposts * @ type: Tree dp * @ author: shuangde * @ blog: blog.csdn.net/shuangde800 * @ email: zengshuangde@gmail.com * =================================================== =====*/# include <iostream> # include <cstdio> # include <algorithm> # include <vector> # include <queue> # include <cmath> # include <cstring> using Namespace std; typedef long int64; const int INF = 0x3f3f3f3f; const int MAXN = 1010; vector <int> adj [MAXN]; bool vis [MAXN]; int n, m; int f [MAXN] [2]; const int M = 2000; void dfs (int u) {vis [u] = true; f [u] [0] = 0; f [u] [1] = M; for (int I = 0; I <adj [u]. size (); ++ I) {int v = adj [u] [I]; if (vis [v]) continue; dfs (v ); f [u] [0] + = f [v] [1] + 1; if (f [v] [0] <f [v] [1]) {f [u] [1] + = f [v] [0] + 1;} else {f [u] [1] + = f [v] [1] ;}} int main () {int nCase; scanf ("% d", & nCase); while (nCase --) {scanf ("% d", & n, & m); for (int I = 0; I <n; ++ I) adj [I]. clear (); for (int I = 0; I <m; ++ I) {int u, v; scanf ("% d", & u, & v); adj [u]. push_back (v); adj [v]. push_back (u);} memset (vis, 0, sizeof (vis); int ans = 0; for (int I = 0; I <n; ++ I) if (! Vis [I]) {dfs (I); ans + = min (f [I] [0], f [I] [1]);} printf ("% d \ n", ans/M, m-(ans % M), ans % M);} return 0 ;}