https://www.zybuluo.com/ysner/note/1307594 Problem surface
Poke me.
Analytical
First brain pumping, state transfer by default directly in the sub-node staining ...
In fact, this problem is still very simple.
Set \ (dp[0/1/2][u]\) indicates that the leaf nodes in the subtree also require a color \ (0/1\), or none of them need a color.
Then the direct transfer is right.
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath > #include <algorithm> #define LL Long long#define re register#define il inline#define fp (i,a,b) for (re int i=a;i&l t;=b;i++) #define FQ (I,A,B) for (re int i=a;i>=b;i--) using namespace Std;const int N=1e4+100;int n,m,h[n],cnt,w[n],dp[ 3][n];struct edge{int to,nxt;} E[n<<1];il void Add (re int u,re int v) {e[++cnt]= (Edge) {v,h[u]};h[u]=cnt;} Il ll gi () {re ll x=0,t=1; Re char Ch=getchar (); while (ch!= '-' && (ch< ' 0 ' | | Ch> ' 9 ')) Ch=getchar (); if (ch== '-') T=-1,ch=getchar (); while (ch>= ' 0 ' &&ch<= ' 9 ') X=x*10+ch-48,ch=getchar (); return x*t;} Il void dfs (re int u,re int fa) {if (u<=m) dp[2][u]=1,dp[!w[u]][u]=1e5; for (re int i=h[u];i+1;i=e[i].nxt) {re int v=e[i].to; if (V==FA) continue; DFS (V,U); Dp[0][u]+=min (Dp[0][v],dp[2][v]); Dp[1][u]+=min (Dp[1][v],dp[2][v]); DP[2][U]+=DP[2][V]; } dp[2][u]=min (Dp[2][u],min (dp[1][U],dp[0][u]) +1);} int main () {memset (h,-1,sizeof (h)); N=gi (); M=gi (); FP (i,1,m) W[i]=gi (); FP (i,1,n-1) {re int u=gi (), V=gi (); Add (u,v); add (V,u); } dfs (n,0); printf ("%d\n", Dp[2][n]); return 0;}
[CQOI2009] coloring of leaves