0w1

HR Far Vertices ( Greedy )

https://www.hackerrank.com/challenges/far-vertices
Let's find out all pairs of ( u, v ) where dis( u, v ) > K, put them into a set, and change the question to: "How many nodes should be removed, so that u and v do not coexist in any pairs ( u, v ) in the set?". Not quite sure how to solve this generalised question, but thinking the tree given, we can somehow prove that keep deleting the most frequent node in the set will do for the original problem.
Something a little technical to know about is that when deleting elements while iterating through a set, some extra care is needed. In C++11, erase() function will return the iterator position right after the one that is deleted, so we might want to catch it instead of missing something or whatever. See this:
c++ - Deleting elements from STL set while iterating - Stack Overflow

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100 + 2;

int N, K;
vector< int > G[ MAXN ];

int dis( int u, int g, int fa ){
    for( int v : G[ u ] ){
        if( v == fa ) continue;
        if( v == g ) return 1;
        int d = dis( v, g, u );
        if( d != -1 ) return d + 1;
    }
    return -1;
}

int main(){
    scanf( "%d%d", &N, &K );
    for( int i = 0; i < N - 1; ++i ){
        int u, v; scanf( "%d%d", &u, &v );
        G[ u ].push_back( v );
        G[ v ].push_back( u );
    }
    set< pair< int, int > > bag;
    for( int u = 1; u <= N; ++u )
        for( int v = u + 1; v <= N; ++v )
            if( dis( u, v, -1 ) > K )
                bag.insert( { u, v } );
    int ans = 0;
    while( !bag.empty() ){
        ++ans;
        vector< int > cnt( N + 1 );
        for( pair< int, int > p : bag )
            ++ cnt[ p.first ],
            ++ cnt[ p.second ];
        int maxv = -1, id;
        for( int u = 1; u <= N; ++u )
            if( maxv < cnt[ u ] )
                maxv = cnt[ u ],
                id = u;
        for( auto it = bag.begin(); it != bag.end(); ){
            if( it->first == id || it->second == id )
                it = bag.erase( it );
            else
                ++it;
        }
/* BAD
        for( auto p : bag )
            if( p.first == id || p.second == id )
                bag.erase( p );
*/
    }
    printf( "%d\n", ans );
    return 0;
}