Problem p03441 (C++)

Buggy submission (status: Runtime Error):
#pragma gcc optimize("Ofast")

#include "bits/stdc++.h"
using namespace std;
typedef int64_t lld;
typedef pair<int, int> pii;
typedef pair<lld, lld> pll;
typedef pair<int, pll> pip;
typedef pair<pll, int> ppi;
typedef pair<lld, pll> plp;
typedef pair<pll, lld> ppl;
typedef pair<pll, pll> ppp;
template <typename T> using maxHeap = priority_queue<T, vector<T>, less<T>>;
template <typename T> using minHeap = priority_queue<T, vector<T>, greater<T>>;
#define ff first
#define ss second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define endl '\n'
#define jizz                                                                   \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  ios::sync_with_stdio(0);
inline void input(int &_x) {
  _x = 0;
  int _tmp = 1;
  char _tc = getchar();
  while ((_tc < '0' || _tc > '9') && _tc != '-')
    _tc = getchar();
  if (_tc == '-')
    _tc = getchar(), _tmp = -1;
  while (_tc >= '0' && _tc <= '9')
    _x = _x * 10 + (_tc - 48), _tc = getchar();
  _x *= _tmp;
}
inline void output(int _x) {
  char _buff[20];
  int _f = 0;
  if (_x == 0)
    putchar('0');
  while (_x > 0) {
    _buff[_f++] = _x % 10 + '0';
    _x /= 10;
  }
  for (_f -= 1; _f >= 0; _f--)
    putchar(_buff[_f]);
  putchar('\n');
}
template <typename Iter> ostream &_out(ostream &s, Iter b, Iter e) {
  s << "[";
  for (auto it = b; it != e; it++)
    s << (it == b ? "" : " ") << *it;
  s << "]";
  return s;
}
template <class T1, class T2>
ostream &operator<<(ostream &out, pair<T1, T2> p) {
  return out << '(' << p.first << ", " << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &c) {
  return _out(s, c.begin(), c.end());
}
#ifdef erd1
#define pprint(x)                                                              \
  cerr << __PRETTY_FUNCTION__ << ":" << __LINE__ << " - " << (#x) << "="       \
       << (x) << endl
#else
#define pprint(x)
#endif

// code starts here
vector<vector<int>> G;
pii dfs(int i, int p) {
  int zcnt = 0, ans = 0, nu = false, cnt = 0, nn;
  for (auto x : G[i])
    if (x != p) {
      cnt++;
      pii d = dfs(x, i);
      if (!d.ff)
        zcnt++;
      ans += d.ff;
      if (d.ss)
        nu = true, nn = d.ff;
    }
  ans += max(0, zcnt - 1);
  if (nu)
    nu = ans == nn;
  if (!cnt)
    nu = true;
  if (i == p && nu)
    ans++;
  // cout << i << " " << ans << endl;
  return {ans, nu || zcnt != 0};
}
int S(pii p) { return p.ff + p.ss; }
signed main() {
  int n;
  cin >> n;
  G.resize(n);
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    G[a].pb(b);
    G[b].pb(a);
  }
  int ans = dfs(0, 0).ff;
  for (int i = n / 10; i < n; i += n / 10)
    assert(dfs(i, i).ff == ans);
  cout << ans << endl;
}


Fixed submission:
#pragma gcc optimize("Ofast")

#include "bits/stdc++.h"
using namespace std;
typedef int64_t lld;
typedef pair<int, int> pii;
typedef pair<lld, lld> pll;
typedef pair<int, pll> pip;
typedef pair<pll, int> ppi;
typedef pair<lld, pll> plp;
typedef pair<pll, lld> ppl;
typedef pair<pll, pll> ppp;
template <typename T> using maxHeap = priority_queue<T, vector<T>, less<T>>;
template <typename T> using minHeap = priority_queue<T, vector<T>, greater<T>>;
#define ff first
#define ss second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define endl '\n'
#define jizz                                                                   \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  ios::sync_with_stdio(0);
inline void input(int &_x) {
  _x = 0;
  int _tmp = 1;
  char _tc = getchar();
  while ((_tc < '0' || _tc > '9') && _tc != '-')
    _tc = getchar();
  if (_tc == '-')
    _tc = getchar(), _tmp = -1;
  while (_tc >= '0' && _tc <= '9')
    _x = _x * 10 + (_tc - 48), _tc = getchar();
  _x *= _tmp;
}
inline void output(int _x) {
  char _buff[20];
  int _f = 0;
  if (_x == 0)
    putchar('0');
  while (_x > 0) {
    _buff[_f++] = _x % 10 + '0';
    _x /= 10;
  }
  for (_f -= 1; _f >= 0; _f--)
    putchar(_buff[_f]);
  putchar('\n');
}
template <typename Iter> ostream &_out(ostream &s, Iter b, Iter e) {
  s << "[";
  for (auto it = b; it != e; it++)
    s << (it == b ? "" : " ") << *it;
  s << "]";
  return s;
}
template <class T1, class T2>
ostream &operator<<(ostream &out, pair<T1, T2> p) {
  return out << '(' << p.first << ", " << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &c) {
  return _out(s, c.begin(), c.end());
}
#ifdef erd1
#define pprint(x)                                                              \
  cerr << __PRETTY_FUNCTION__ << ":" << __LINE__ << " - " << (#x) << "="       \
       << (x) << endl
#else
#define pprint(x)
#endif

// code starts here
vector<vector<int>> G;
pii dfs(int i, int p) {
  int zcnt = 0, ans = 0, nu = false, cnt = 0, nn;
  for (auto x : G[i])
    if (x != p) {
      cnt++;
      pii d = dfs(x, i);
      if (!d.ff)
        zcnt++;
      ans += d.ff;
      if (d.ss)
        nu = true, nn = d.ff;
    }
  ans += max(0, zcnt - 1);
  if (nu)
    nu = ans == nn;
  if (!cnt)
    nu = true;
  if (i == p && nu)
    ans++;
  // cout << i << " " << ans << endl;
  return {ans, nu || zcnt != 0};
}
int S(pii p) { return p.ff + p.ss; }
signed main() {
  int n;
  cin >> n;
  G.resize(n);
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    G[a].pb(b);
    G[b].pb(a);
  }
  int ans = dfs(0, 0).ff;
  for (int i = n / 10; i < n; i += max(n / 10, 1))
    ans = max(dfs(i, i).ff, ans);
  cout << ans << endl;
}