Network (tree dp) Luo Valley 2899

f [i] [0] represents the father Shou

f [i] [1] represents keep their

f [i] [2] represents the son Shou

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define MN 15500
#define re register int
using namespace std;
int f[MN][3];
int cnt,head[MN];
int n,m,k,a[MN];
int vis[MN];
struct child{
    int v,nxt;
}e[2*MN];

void add(int u,int v){
    e[++cnt].v=v;
    e[cnt].nxt=head[u];
    head[u]=cnt;
}
void dp(int u,int fa){
//f[u][0]=f[u][2]=a[u];
//f[u][1]=0;
int d=0x3f3f3f3f;
for(re i=head[u];i;i=e[i].nxt){
int v=e[i].v;
if(v==fa)continue;
dp(v,u);
f[u][0]+=min(f[v][1],f[v][2]);
f[u][1]+=min(f[v][1],f[v][2]);
f[u][2] + = min (F [V] [ 0 ], min (F [V] [ . 1 ], F [V] [ 2 ])); 
D = min (D, F [V] [ 2 ] -min (F [V] [ 2 ], F [V] [ . 1 ]));
 // F [U] [2] -min (F [V] [. 1], F [V] [2]) represents the sum of the other son . 
} 
F [U] [ . 1 ] + = D; F [U] [ 2 ] ++ ; 
} 
int main () { 
    Scanf ( " % D " , & n-);
 for (I = Re . 1 ; I <= n- - . 1 ; I ++ ) {
     int A1, A2, A3, A4; 
    Scanf ( " % D% D " , & A1, &a2);
    add(a1,a2);
    add(a2,a1);
}
    dp(1,0);
    printf("%d\n",min(f[1][1],f[1][2]));
return 0;
}

 

Guess you like

Origin www.cnblogs.com/zw130-lzr-blogs/p/11220336.html