Problem p04049 (C++)

Buggy submission (status: Runtime Error):
/*
 _    _    _______   _    _
| |  / /  |  _____| | |  / /
| | / /   | |       | | / /
| |/ /    | |_____  | |/ /
| |\ \    |  _____| | |\ \
| | \ \   | |       | | \ \
| |  \ \  | |_____  | |  \ \
|_|   \_\ |_______| |_|   \_\

*/
/*#include <iostream>
#include <algorithm>
#include <assert.h>*/
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef double ld;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;

#define F first
#define S secondcin
#define pb push_back
#define eb emplace_back
#define right(x) x << 1 | 1
#define left(x) x << 1
#define forn(x, a, b) for (int x = a; x <= b; ++x)
#define for1(x, a, b) for (int x = a; x >= b; --x)
#define mkp make_pair
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define y1 kekekek

const ll ool = 1e18 + 9;
const int oo = 1e9 + 9, base = 1e9 + 7;
const ld eps = 1e-7;
const int N = 2e3 + 6;

int n, K, cnt;
vector<int> g[N];

void dfs(int v, int par, int lvl) {
  if (lvl > K / 2)
    return;
  ++cnt;
  for (auto to : g[v]) {
    if (to == par)
      continue;
    dfs(to, v, lvl + 1);
  }
}

int main() {
  ios_base ::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  freopen("in", "r", stdin);

  cin >> n >> K;
  forn(i, 1, n - 1) {
    int x, y;
    cin >> x >> y;
    g[x].eb(y);
    g[y].eb(x);
  }

  int ans = n;
  forn(v, 1, n) {
    if (K & 1) {
      for (auto to : g[v]) {
        cnt = 0;
        dfs(to, v, 0);
        int res = cnt;
        cnt = 0;
        dfs(v, to, 0);
        res += cnt;
        ans = min(ans, n - res);
      }
    } else {
      cnt = 0;
      dfs(v, 0, 0);
      ans = min(ans, n - cnt);
    }
  }

  cout << ans << "\n";

  return 0;
}


Fixed submission:
/*
 _    _    _______   _    _
| |  / /  |  _____| | |  / /
| | / /   | |       | | / /
| |/ /    | |_____  | |/ /
| |\ \    |  _____| | |\ \
| | \ \   | |       | | \ \
| |  \ \  | |_____  | |  \ \
|_|   \_\ |_______| |_|   \_\

*/
/*#include <iostream>
#include <algorithm>
#include <assert.h>*/
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef double ld;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;

#define F first
#define S secondcin
#define pb push_back
#define eb emplace_back
#define right(x) x << 1 | 1
#define left(x) x << 1
#define forn(x, a, b) for (int x = a; x <= b; ++x)
#define for1(x, a, b) for (int x = a; x >= b; --x)
#define mkp make_pair
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define y1 kekekek

const ll ool = 1e18 + 9;
const int oo = 1e9 + 9, base = 1e9 + 7;
const ld eps = 1e-7;
const int N = 2e3 + 6;

int n, K, cnt;
vector<int> g[N];

void dfs(int v, int par, int lvl) {
  if (lvl > K / 2)
    return;
  ++cnt;
  for (auto to : g[v]) {
    if (to == par)
      continue;
    dfs(to, v, lvl + 1);
  }
}

int main() {
  ios_base ::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  // freopen("in", "r", stdin);

  cin >> n >> K;
  forn(i, 1, n - 1) {
    int x, y;
    cin >> x >> y;
    g[x].eb(y);
    g[y].eb(x);
  }

  int ans = n;
  forn(v, 1, n) {
    if (K & 1) {
      for (auto to : g[v]) {
        cnt = 0;
        dfs(to, v, 0);
        int res = cnt;
        cnt = 0;
        dfs(v, to, 0);
        res += cnt;
        ans = min(ans, n - res);
      }
    } else {
      cnt = 0;
      dfs(v, 0, 0);
      ans = min(ans, n - cnt);
    }
  }

  cout << ans << "\n";

  return 0;
}