1#include <cstdio>2#include <iostream>3 #defineN 100094 #defineINF 1000005 using namespacestd;6 intb[n],cnt,n,m,c[n],f[n][2],head[n],next[2*n],u[2*N];7 voidJiaintA1,intA2)8 {9cnt++;Tennext[cnt]=HEAD[A1]; Onehead[a1]=CNT; Au[cnt]=A2; - return; - } the voiddpintA1) - { -b[a1]=1; - if(a1<=m) + { -f[a1][c[a1]]=1; +f[a1][c[a1]^1]=inf; A return; at } - Else -f[a1][0]=f[a1][1]=1; - for(intI=head[a1];i;i=Next[i]) - if(!B[u[i]]) - { in DP (U[i]); -f[a1][0]+=min (f[u[i]][1],f[u[i]][0]-1); tof[a1][1]+=min (f[u[i]][1]-1, f[u[i]][0]); + } - return; the } * intMain () $ {Panax Notoginsengscanf"%d%d",&n,&m); - for(intI=1; i<=m;i++) thescanf"%d",&c[i]); + for(intI=1; i<n;i++) A { the inta1,a2; +scanf"%d%d",&a1,&A2); - Jia (A1,A2); $ Jia (a2,a1); $ } - DP (n); -printf"%d\n", Min (f[n][0],f[n][1])); the return 0; -}
Tree DP Any feasible node to root the answer is the same, f[i][0] means I dianran white, 1 dyed black, so if I dianran white his child nodes dyed white nodes will not be dyed.
Bzoj 1304: [CQOI2009] coloring of leaves