School OJ on the problem, just beginning to do when thought is the road SB problem 10 minutes can finish.
Then I was ready to knock and find out that I was an SB.
At first I thought it was a very naked TREEDP, then only set two states, but how to think how wrong. The complexity seems to explode. To the left son right brother seems unable to transfer at all.
Search the puzzle, found not to change to the left son right brother, two states to change to three states on the line
$f [node][0]$ the subtree in the $node$ node is overwritten and $node$ is established
$f [node][1]$ the subtree in the $node$ node is overwritten and $node$ is not established
$f [node][2]$ subtree on $node$ node are overwritten but $node$ not overwritten
Then the state transfer equations for $f[node][0]$ and $f[node][2]$ can be well written out.
$f [Node][0]=\sum min (f[son][0],f[son][1],f[son][2]) +1$
$f [Node][2]=\sum f[son][1]$
$f [node][1]$ is relatively troublesome, the state transfer equation is not so well written. To put it simply, $\sum min (f[son][1],f[son][0]) $ But the limit exists, there must be a $son$ status of $0$, so you need to add some small processing to the code.
It should be noted that the state here must be $0$ $son$ is not the lowest value, but the difference between the state and the $1$ is the largest.
The code implementation also has some details, not much to say.
1 //OJ 19462 //by Cydiater3 //2016.9.184#include <iostream>5#include <cstdio>6#include <cstring>7#include <string>8#include <algorithm>9#include <queue>Ten#include <map> One#include <ctime> A#include <cmath> -#include <cstdlib> -#include <iomanip> the using namespacestd; - #definell Long Long - #defineUp (I,j,n) for (int i=j;i<=n;i++) - #defineDown (i,j,n) for (int i=j;i>=n;i--) + Const intmaxn=1e6+5;; - Const intoo=10005; + inline ll read () { A CharCh=getchar (); ll x=0, f=1; at while(ch>'9'|| ch<'0'){if(ch=='-') f=-1; ch=GetChar ();} - while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} - returnx*F; - } -ll n,f[maxn][3],link[maxn],len=0; - structedge{ in ll Y,next; - }E[MAXN]; to namespacesolution{ +InlinevoidInsertintXintY) {e[++len].next=link[x]; link[x]=len;e[len].y=y;} - voidinit () { then=read (); *Up (I,2, N) { $ intX=read (), y=read ();Panax Notoginseng Insert (x, y); - Insert (y,x); the } + } A voidTREEDP (intNodeintFA) { thef[node][0]=1; ll sum=0; + for(intI=link[node];i;i=e[i].next)if(e[i].y!=FA) { - TREEDP (e[i].y,node); $f[node][2]+=f[e[i].y][1]; $f[node][0]+=min (f[e[i].y][0],min (f[e[i].y][1],f[e[i].y][2])); -Sum+=min (f[e[i].y][1],f[e[i].y][0]); - } thef[node][1]=Oo; - for(intI=link[node];i;i=e[i].next)if(e[i].y!=FA) {Wuyif[node][1]=min (f[node][1],sum-min (f[e[i].y][1],f[e[i].y][0]) +f[e[i].y][0]); the } - } Wu voidoutput () { -Cout<<min (f[1][0],f[1][1]) <<Endl; About } $ } - intMain () { - //freopen ("input.in", "R", stdin); - using namespacesolution; A init (); +TREEDP (1,0); the output (); - return 0; $}
View Code
POJ3659 [Usaco2008jan_gold] Telephone network