Transmission Door
Solution
A very simple tree-shaped dpqwq, transfer relationship: Father dyed his son without dyeing
Just make sure the root is a simple tree dp, and the root can take a non-leaf node
We can discuss the situation and find out that the answer won't change.
Code
by Menteur_hxy#include <cmath> #include <vector> #include <cstdio> #include <cstdlib># Include <cstring> #include <iostream> #include <algorithm> #define RE register#define Fo (i,a,b) for (re int i= (a), _= (b), i<=_;i++) #define RO (i,a,b) for (Re int i= (b), _= (a); i>=_;i--) using namespace Std;inline int Read () { int X=0,f=1;char C=getchar (); while (!isdigit (c)) {if (c== '-') F=-f;c=getchar ();} while (IsDigit (c)) x= (x<<1) + (x<<3) +c-48,c=getchar (); return x*f;} const int N=1e4+10,inf=0x3f3f3f3f;int n,m;int f[n][2],col[n];vector <int> v[n];void dfs (int u,int pre) {f[u][0]= F[u][1]=1; if (u<=m) F[u][!col[u]]=inf; int siz=v[u].size (), V; Fo (I,0,siz-1) if ((v=v[u][i))!=pre) {DFS (v,u); F[u][1]+=min (F[v][1]-1,f[v][0]); F[u][0]+=min (f[v][0]-1,f[v][1]); }}int Main () {N=read (), M=read ();//due to custom N,m interchange Fo (i,1,m) col[i]=read (); Fo (i,1,n-1) {int a=read (), B=read (); V[a].push_back (b); V[B]. push_back (a); } dfs (m+1,0); printf ("%d", min (f[m+1][0],f[m+1][1])); return 0;}
[luogu3155 CQOI2009] coloring of leaves (tree dp)