Problem p03441 (C++)

Buggy submission (status: Time Limit Exceeded):
//	ZapZu's code hohoho
//	#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define dfs_black 1
#define dfs_white -1
#define pr pair
#define vt vector
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

typedef double db;
typedef long long li;
typedef long double ld;

typedef pr<int, int> ii;
typedef pr<ld, ld> dd;

typedef vt<int> vi;
typedef vt<li> vli;
typedef vt<ld> vld;
typedef vt<ii> vii;

typedef map<int, int> mii;
typedef map<int, bool> mib;
typedef map<int, char> mic;

typedef set<int> s_i;
typedef set<char> s_c;

const int MOD = 1e9 + 7;
const li INF = 1e18;
const ld PI = 4 * atan((ld)1);
vi G[200005];
int dp[200005];
int n;
void dfs(int u, int p) {
  int cnt = 0;
  for (auto v : G[u]) {
    if (v == p)
      continue;
    dfs(v, u);
    if (!dp[v])
      cnt++;
    dp[u] += dp[v];
  }
  dp[u] += max(cnt - 1, 0);
}
int solve(int root) {
  for (int i = 1; i <= n; i++)
    dp[i] = 0;
  dfs(root, 0);
  return dp[root] + 1;
}
signed main() {
  //	freopen("1_13.txt", "r", stdin);
  //	freopen(".out", "w", stdout);
  cin >> n;
  if (n < 3)
    return cout << 1, 0;
  for (int i = 1; i <= n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u++;
    v++;
    G[u].eb(v);
    G[v].eb(u);
  }
  vi v;
  for (int i = 1; i <= n; i++) {
    if (G[i].size() == 1)
      v.eb(i);
  }
  int cnt = 0, ans = INT_MAX;
  for (auto temp : v) {
    ans = min(ans, solve(temp));
    cnt++;
    if (cnt >= 500)
      break;
  }
  cout << ans;
}


Fixed submission:
//	ZapZu's code hohoho
//	#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define dfs_black 1
#define dfs_white -1
#define pr pair
#define vt vector
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

typedef double db;
typedef long long li;
typedef long double ld;

typedef pr<int, int> ii;
typedef pr<ld, ld> dd;

typedef vt<int> vi;
typedef vt<li> vli;
typedef vt<ld> vld;
typedef vt<ii> vii;

typedef map<int, int> mii;
typedef map<int, bool> mib;
typedef map<int, char> mic;

typedef set<int> s_i;
typedef set<char> s_c;

const int MOD = 1e9 + 7;
const li INF = 1e18;
const ld PI = 4 * atan((ld)1);
vi G[200005];
int dp[200005];
int n;
void dfs(int u, int p) {
  int cnt = 0;
  for (auto v : G[u]) {
    if (v == p)
      continue;
    dfs(v, u);
    if (!dp[v])
      cnt++;
    dp[u] += dp[v];
  }
  dp[u] += max(cnt - 1, 0);
}
int solve(int root) {
  for (int i = 1; i <= n; i++)
    dp[i] = 0;
  dfs(root, 0);
  return dp[root] + 1;
}
signed main() {
  //	freopen("1_13.txt", "r", stdin);
  //	freopen(".out", "w", stdout);
  cin >> n;
  if (n < 3)
    return cout << 1, 0;
  for (int i = 1; i <= n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u++;
    v++;
    G[u].eb(v);
    G[v].eb(u);
  }
  vi v;
  for (int i = 1; i <= n; i++) {
    if (G[i].size() == 1)
      v.eb(i);
  }
  int cnt = 0, ans = INT_MAX;
  for (auto temp : v) {
    ans = min(ans, solve(temp));
    cnt++;
    if (cnt >= 200)
      break;
  }
  cout << ans;
}