Algorithm: UVA 10859 placing lampposts (tree DP)

Source: Internet
Author: User

The main effect of the topic

Give you a N-point m-Edge without-loop graph, put the lights on as few nodes as possible, so that all sides are illuminated. Each lamp will illuminate with it as an endpoint of all sides.

On the premise of the smallest number of lights, the number of edges illuminated by the two lamps should be as large as possible.

Ideas

This is the example in LRJ's training guide.

This question taught me a very useful technique: there are two values to be optimized, such as making a as small as possible, and B as small as possible.

Then it can be converted to make the m*a+b as small as possible, where M should be a larger number than "the maximum of a and the difference between the minimum value of B"

The final answer is ans/m, ans%m.

Back to this question, the total number of lights required to be the smallest, by the two lights at the same time illuminate the number of edges as large as possible.

Because each side is either illuminated by a lamp or illuminated by two lamps, it can be converted to:

Ask: Put the total number of lights at least, by a lamp to illuminate the number of edges as little as possible.

You can become the minimum value of the ball m*a+b, a for the number of lights placed, b for the number of sides by a lamp

F[U][1] represents the minimum value of the entire subtree when a U-point light is placed

F[u][0] represents the minimum value of the entire subtree when the U-point does not place the lamp

if u put, then you can choose to put the sub node, you can also do not put, select the smaller values. If you choose not to take photos, add a side with one light.

If you do not put, then its child nodes must choose to put, and each side has only one light photo

The following my code and the book on the implementation of a different, more concise

Code

/**========================================== * is a solution for ACM/ICPC problem * * @source: uva-10859 placin G 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 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%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%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%d%d\n", ans/m, M (ans%m),
ANS%M);
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.