Uva 10859-placing lampposts Tree-shaped dp+ maintains another optimal value in the case of a value optimal

Source: Internet
Author: User
Tags comparison min

Test instructions

There is a forest. Lights can be placed on each point. After you put the light on. With the connected side is illuminated ... Ask the minimum number of lights to make all sides have lights. Ensure that the lamp is the smallest case. The maximum number of edges that are simultaneously illuminated by two sides ...



Exercises

The main idea is to update the minimum light ... When updating the minimum lamp is the same for a long time. Update the largest two-edged photo ... You can take more than a few values: Update together ... Fully OK .... Here is a more ingenious way of thinking about the Lrj great God. This idea can also be used in a class of problems ...

Consider the need to guarantee a minimal case ... b min ... So it is in the process of State transfer to ensure that a is the dominant position. Only when a is equal. b Just play a role ... This can be reminiscent of a two-digit comparison: First is higher than the highest bit. When the highest bit is unequal: How the low-level transformation can not affect the comparison results ...

Then choose a number M that is as large as possible but not int,long long. Let's be within the scope of the topic. b How many: are not up to M. That means the value is a*m+b in the state.

Output answer when ... P/m for the first optimal ... P%m to ensure the second optimal under the first optimal


Program:

#include <iostream> #include <stdio.h> #include <string.h> #include <cmath> #include <queue > #include <stack> #include <set> #include <algorithm> #define LL long long #define OO 1000000007 #defi  
NE pi ACOs ( -1.0) #define MAXN 1005 #define M, using namespace std;  
int n,a[maxn],dp[maxn][2];
Vector<int> T[MAXN];
BOOL USED[MAXN];
       void Dfs (int x) {used[x]=true;
       int i,y,m=t[x].size ();
       dp[x][0]=0,dp[x][1]=m+1;
              for (i=0;i<m;i++) {y=t[x][i];
              if (Used[y]) continue;
              DFS (y);
              DP[X][0]+=DP[Y][1];   
                  if (dp[y][0]+1>dp[y][1]-1) dp[x][1]+=dp[y][1]-1;      
       else dp[x][1]+=dp[y][0]+1;
} return; 
       } int main () {int i,t,m,ans;
       scanf ("%d", &t);
              while (t--) {scanf ("%d%d", &n,&m); 
              for (i=0;i<n;i++) t[i].clear (); for (i=0;i<m;i++) {int x, y;
                     scanf ("%d%d", &x,&y);
                     T[x].push_back (y);
              T[y].push_back (x);
              } memset (used,false,sizeof (used));
              ans=0;
                       for (i=0;i<n;i++) if (!used[i]) {DFS (i);
                       dp[i][1]--;
                Ans+=min (dp[i][0],dp[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.