Surface
By the structure of the tree we can know, eventually either a (longest) chain is not finished, or go some point finally walked the longest chain. Why always say the longest chain, because in the process of this walk in the tree (and finally do not require return) in addition to a chain will be walked two times, obviously we greedy to take the longest chain to go once.
1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 Const intn= the;6 intN,m,t1,t2,cnt,dps,ans;7 intp[n],noww[2*n],goal[2*N];8 voidLinkintFintt)9 {Tennoww[++cnt]=P[f]; Onegoal[cnt]=t,p[f]=CNT; A } - voidDFS (intNdeintFthintDTH) - { thedps=Max (dps,dth); - for(intI=p[nde];i;i=Noww[i]) - if(goal[i]!=fth) DFS (goal[i],nde,dth+1); - } + intMain () - { +scanf"%d%d",&n,&m); A for(intI=1; i<n;i++) at { -scanf"%d%d", &t1,&t2), t1++,t2++; - Link (t1,t2), link (t2,t1); - } -DFS (1,0,1), Ans=min (dps,m+1), n-=dps,m-=dps-1; -printf"%d", Ans+min (n, (m+1)/2)); in return 0; -}
View Code
Problem solving: Cqoi 2017 small Q board