Description
A tree that can be dyed, the dots that are dyed can be controlled with its distance of not more than \ (K\) All points, asked to control the whole tree at least a few points to dye.
Sol
Greedy.
Record the deepest non-staining point and the lightest stain point, judge whether it can be completed in the sub-tree, can not be assigned to the maximum depth of non-staining point depths of +1, can be assigned to the dyeing point depth of +1.
Need a special sentence root.
Code
#include <cstdio> #include <vector> #include <ctime> #include <cstdlib> #include <iostream >using namespace Std;const int N = 100005;int n,k,ans;int f[n];vector<int> g[n];inline int in (int x=0,char ch=get char ()) {while (ch> ' 9 ' | | ch< ' 0 ') Ch=getchar (); while (ch>= ' 0 ' && ch<= ' 9 ') x=x*10+ch-' 0 ', Ch=getchar ( ); return x; }void dfs (int u,int fa) {int mx=0,mi=0;for (int i=0,v;i<g[u].size (); i++) if ((V=g[u][i])!=fa) {DFS (v,u); Mx=max (mx,f[v ]), Mi=min (Mi,f[v]);} if (Mx+1 > K) f[u]=-k,ans++;else if (mx+mi+1>0) F[u]=mx+1;else f[u]=mi+1;} int main () {n=in (), K=in (), for (int i=1,u,v;i<n;i++) u=in () +1,v=in () +1,g[u].push_back (v), G[v].push_back (U);D FS (1, -1); if (f[1]>0) Ans++;cout<<ans<<endl;return 0;}
51Nod 1378 Jacket Master's rage