Problem p03441 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define _USE_MATH_DEFINES
#include <complex>
#include <math.h>

using namespace std;

#define INF (int)1e8
#define _INF (int)-1e8
#define INFLL (long long)1e15
#define _INFLL (long long)-1e15
#define Loop(i, n) for (int i = 0; i < (int)n; i++)
#define Loop1(i, n) for (int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for (int i = (int)n; i >= 1; i--)
#define bitmanip(m, val) static_cast<bitset<(int)m>>(val)
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> P;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;

/*******************************************************/

struct tree_t {
  int n;           // |V|, index begins with 0
  int r;           // root
  vector<P> edges; // E
  vector<ll> cost; // cost or distance
};

class Tree {
private:
  struct node {
    int id;
    vi childs;
    int parent;
    ll cost;
    int deg;
    ll d;
    bool operator<(const node &another) const {
      return deg != another.deg ? deg < another.deg : id < another.id;
    }
  };
  int n;

public:
  vector<node> nodes;
  vi leaves;
  int root;
  Tree(tree_t T) {
    n = T.n;
    root = T.r;
    nodes.resize(n);
    Loop(i, n) nodes[i] = {i, {}, -1, 0, -1, 0};
    map<int, vector<pair<int, ll>>> edges;
    Loop(i, n - 1) {
      if (T.cost.size() == 0) {
        edges[T.edges[i].first].push_back({T.edges[i].second, 1});
        edges[T.edges[i].second].push_back({T.edges[i].first, 1});
      } else {
        edges[T.edges[i].first].push_back({T.edges[i].second, T.cost[i]});
        edges[T.edges[i].second].push_back({T.edges[i].first, T.cost[i]});
      }
    }
    leaves = {};
    stack<int> stk;
    stk.push(root);
    while (stk.size()) {
      int a = stk.top();
      stk.pop();
      if (nodes[a].deg != -1)
        continue;
      if (a == root)
        nodes[a].deg = 0;
      int leaf_flag = true;
      Loop(i, edges[a].size()) {
        int b = edges[a][i].first;
        if (nodes[b].deg != -1) {
          nodes[a].parent = b;
          nodes[a].cost = edges[a][i].second;
          nodes[a].deg = nodes[b].deg + 1;
          nodes[a].d = nodes[b].d + nodes[a].cost;
        } else {
          leaf_flag = false;
          nodes[a].childs.push_back(b);
          stk.push(b);
        }
      }
      if (leaf_flag)
        leaves.push_back(a);
    }
    if (n == 1)
      leaves.push_back(root);
    return;
  }
  int dfs(int k) {
    vi dat;
    Loop(i, nodes[k].childs.size()) { dat.push_back(dfs(nodes[k].childs[i])); }
    sort(dat.begin(), dat.end(), greater<int>());
    int sum = 0;
    Loop(i, dat.size() - 1) {
      if (dat[i] == 0)
        sum += 1;
      else
        sum += dat[i];
    }
    if (dat.back() > 0)
      sum += dat.back();
    return sum;
  }
};

int main() {
  tree_t T;
  cin >> T.n;
  vi d_cnt(T.n, 0);
  Loop(i, T.n - 1) {
    int a, b;
    cin >> a >> b;
    T.edges.push_back({a, b});
    d_cnt[a]++;
    d_cnt[b]++;
  }
  bool path_flag = true;
  Loop(i, d_cnt.size()) {
    if (d_cnt[i] >= 3) {
      path_flag = false;
      T.r = i;
    }
  }
  if (path_flag) {
    cout << 1 << endl;
    return 0;
  } else {
    Tree *tree = new Tree(T);
    cout << tree->dfs(T.r) << endl;
    return 0;
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define _USE_MATH_DEFINES
#include <complex>
#include <math.h>

using namespace std;

#define INF (int)1e8
#define _INF (int)-1e8
#define INFLL (long long)1e15
#define _INFLL (long long)-1e15
#define Loop(i, n) for (int i = 0; i < (int)n; i++)
#define Loop1(i, n) for (int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for (int i = (int)n; i >= 1; i--)
#define bitmanip(m, val) static_cast<bitset<(int)m>>(val)
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> P;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;

/*******************************************************/

struct tree_t {
  int n;           // |V|, index begins with 0
  int r;           // root
  vector<P> edges; // E
  vector<ll> cost; // cost or distance
};

class Tree {
private:
  struct node {
    int id;
    vi childs;
    int parent;
    ll cost;
    int deg;
    ll d;
    bool operator<(const node &another) const {
      return deg != another.deg ? deg < another.deg : id < another.id;
    }
  };
  int n;

public:
  vector<node> nodes;
  vi leaves;
  int root;
  Tree(tree_t T) {
    n = T.n;
    root = T.r;
    nodes.resize(n);
    Loop(i, n) nodes[i] = {i, {}, -1, 0, -1, 0};
    map<int, vector<pair<int, ll>>> edges;
    Loop(i, n - 1) {
      if (T.cost.size() == 0) {
        edges[T.edges[i].first].push_back({T.edges[i].second, 1});
        edges[T.edges[i].second].push_back({T.edges[i].first, 1});
      } else {
        edges[T.edges[i].first].push_back({T.edges[i].second, T.cost[i]});
        edges[T.edges[i].second].push_back({T.edges[i].first, T.cost[i]});
      }
    }
    leaves = {};
    stack<int> stk;
    stk.push(root);
    while (stk.size()) {
      int a = stk.top();
      stk.pop();
      if (nodes[a].deg != -1)
        continue;
      if (a == root)
        nodes[a].deg = 0;
      int leaf_flag = true;
      Loop(i, edges[a].size()) {
        int b = edges[a][i].first;
        if (nodes[b].deg != -1) {
          nodes[a].parent = b;
          nodes[a].cost = edges[a][i].second;
          nodes[a].deg = nodes[b].deg + 1;
          nodes[a].d = nodes[b].d + nodes[a].cost;
        } else {
          leaf_flag = false;
          nodes[a].childs.push_back(b);
          stk.push(b);
        }
      }
      if (leaf_flag)
        leaves.push_back(a);
    }
    if (n == 1)
      leaves.push_back(root);
    return;
  }
  int dfs(int k) {
    vi dat;
    Loop(i, nodes[k].childs.size()) { dat.push_back(dfs(nodes[k].childs[i])); }
    sort(dat.begin(), dat.end(), greater<int>());
    int sum = 0;
    Loop(i, dat.size() - 1) {
      if (dat[i] == 0)
        sum += 1;
      else
        sum += dat[i];
    }
    if (dat.size() && dat.back() > 0)
      sum += dat.back();
    return sum;
  }
};

int main() {
  tree_t T;
  cin >> T.n;
  vi d_cnt(T.n, 0);
  Loop(i, T.n - 1) {
    int a, b;
    cin >> a >> b;
    T.edges.push_back({a, b});
    d_cnt[a]++;
    d_cnt[b]++;
  }
  bool path_flag = true;
  Loop(i, d_cnt.size()) {
    if (d_cnt[i] >= 3) {
      path_flag = false;
      T.r = i;
    }
  }
  if (path_flag) {
    cout << 1 << endl;
    return 0;
  } else {
    Tree *tree = new Tree(T);
    cout << tree->dfs(T.r) << endl;
    return 0;
  }
}