Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#ifndef _WIN32
#define getchar getchar_unlocked
#endif

#define MP make_pair
#define PB push_back
#define ST first
#define ND second

typedef long long int LLI;
typedef unsigned long long int LLU;
typedef long double LD;
typedef pair<int, int> pii;
typedef pair<long long int, long long int> pll;
typedef vector<int> vi;

template <class TH> void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << "=" << h << "\n";
}
template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while (*sdbg != ',')
    cerr << *sdbg++;
  cerr << "=" << h << ",";
  _dbg(sdbg + 1, a...);
}

#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define boost                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)

const int MX = 100007;

vector<pii> G[MX];
int rp[MX];
LLI depth[MX];
int ojciec[MX][20];

void dfs1(int u, int p) {
  rp[u] = 1;
  for (pii P : G[u]) {
    int v = P.ST, w = P.ND;
    if (v != p) {
      ojciec[v][0] = u;
      depth[v] = depth[u] + w;
      dfs1(v, u);
      rp[u] += rp[v];
    }
  }
}

inline int LCA(int a, int b) {
  int d1 = depth[a], d2 = depth[b], pom = abs(d1 - d2);
  if (d1 > d2)
    swap(a, b);
  for (int i = 0; (1 << i) <= pom; ++i)
    if ((pom & (1 << i)) > 0)
      b = ojciec[b][i];
  if (a == b)
    return a;
  for (int i = 19; i >= 0; --i)
    if (ojciec[a][i] != ojciec[b][i])
      a = ojciec[a][i], b = ojciec[b][i];
  return ojciec[a][0];
}

inline int odl(int a, int b) {
  int l = LCA(a, b);
  return (depth[a] + depth[b] - 2 * depth[l]);
}

int32_t main() {
  boost;
  int n;
  cin >> n;
  for (int i = 1; i < n; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    G[a].PB({b, c});
    G[b].PB({a, c});
  }
  int q, k;
  cin >> q >> k;
  dfs1(k, 0);
  while (q--) {
    int a, b;
    scanf("%d%d", &a, &b);
    printf("%lld\n", depth[a] + depth[b]);
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#ifndef _WIN32
#define getchar getchar_unlocked
#endif

#define MP make_pair
#define PB push_back
#define ST first
#define ND second

typedef long long int LLI;
typedef unsigned long long int LLU;
typedef long double LD;
typedef pair<int, int> pii;
typedef pair<long long int, long long int> pll;
typedef vector<int> vi;

template <class TH> void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << "=" << h << "\n";
}
template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while (*sdbg != ',')
    cerr << *sdbg++;
  cerr << "=" << h << ",";
  _dbg(sdbg + 1, a...);
}

#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define boost                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)

const int MX = 100007;

vector<pii> G[MX];
int rp[MX];
LLI depth[MX];
int ojciec[MX][20];

void dfs1(int u, int p) {
  rp[u] = 1;
  for (pii P : G[u]) {
    int v = P.ST, w = P.ND;
    if (v != p) {
      ojciec[v][0] = u;
      depth[v] = depth[u] + w;
      dfs1(v, u);
      rp[u] += rp[v];
    }
  }
}

inline int LCA(int a, int b) {
  int d1 = depth[a], d2 = depth[b], pom = abs(d1 - d2);
  if (d1 > d2)
    swap(a, b);
  for (int i = 0; (1 << i) <= pom; ++i)
    if ((pom & (1 << i)) > 0)
      b = ojciec[b][i];
  if (a == b)
    return a;
  for (int i = 19; i >= 0; --i)
    if (ojciec[a][i] != ojciec[b][i])
      a = ojciec[a][i], b = ojciec[b][i];
  return ojciec[a][0];
}

inline int odl(int a, int b) {
  int l = LCA(a, b);
  return (depth[a] + depth[b] - 2 * depth[l]);
}

int32_t main() {
  boost;
  int n;
  cin >> n;
  for (int i = 1; i < n; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    G[a].PB({b, c});
    G[b].PB({a, c});
  }
  int q, k;
  cin >> q >> k;
  dfs1(k, 0);
  while (q--) {
    int a, b;
    cin >> a >> b;
    cout << depth[a] + depth[b] << '\n';
  }
}