51Nod 1378 Jacket Master's rage

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.