Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n); i >= 0; i--)
#define FOR(i, m, n) for (int i = (m); i < (n); i++)

using namespace std;
using ll = long long;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

//////////////////////////////////////////////////

struct Edge {
  int to;
  ll cost;
};

int main() {
  int n;
  cin >> n;
  vector<vector<Edge>> g(n);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    ll c;
    cin >> c;

    g[a].push_back(Edge{b, c});
    g[b].push_back(Edge{a, c});
  }

  int q, k;
  cin >> q >> k;
  k--;
  vector<pair<int, int>> xy(n);
  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    xy[i] = make_pair(x, y);
  }

  vector<ll> dp(n);
  vector<bool> visited(n, false);

  auto dfs = [&](auto &&dfs, int now, ll len) -> void {
    if (visited[now])
      return;
    visited[now] = true;
    dp[now] = len;

    for (int i = 0; i < g[now].size(); i++) {
      int next = g[now][i].to;
      ll c = g[now][i].cost;
      dfs(dfs, next, dp[now] + c);
    }
  };

  // int q, k; cin >> q >> k;
  // k--;
  dfs(dfs, k, 0);
  // vector<pair<int, int>> xy(n);
  // for (int i = 0; i < q; i++)
  // {
  //     int x, y;
  //     cin >> x >> y;
  //     x--; y--;
  //     cout << dp[x] + dp[y] << endl;
  // }

  for (int i = 0; i < q; i++) {
    int x = xy[i].first, y = xy[i].second;
    cout << dp[x] + dp[y] << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n); i >= 0; i--)
#define FOR(i, m, n) for (int i = (m); i < (n); i++)

using namespace std;
using ll = long long;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

//////////////////////////////////////////////////

struct Edge {
  int to;
  ll cost;
};

int main() {
  int n;
  cin >> n;
  vector<vector<Edge>> g(n);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    ll c;
    cin >> c;

    g[a].push_back(Edge{b, c});
    g[b].push_back(Edge{a, c});
  }

  int q, k;
  cin >> q >> k;
  k--;
  vector<pair<int, int>> xy(q);
  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    xy[i] = make_pair(x, y);
  }

  vector<ll> dp(n);
  vector<bool> visited(n, false);

  auto dfs = [&](auto &&dfs, int now, ll len) -> void {
    if (visited[now])
      return;
    visited[now] = true;
    dp[now] = len;

    for (int i = 0; i < g[now].size(); i++) {
      int next = g[now][i].to;
      ll c = g[now][i].cost;
      dfs(dfs, next, dp[now] + c);
    }
  };

  // int q, k; cin >> q >> k;
  // k--;
  dfs(dfs, k, 0);
  // vector<pair<int, int>> xy(n);
  // for (int i = 0; i < q; i++)
  // {
  //     int x, y;
  //     cin >> x >> y;
  //     x--; y--;
  //     cout << dp[x] + dp[y] << endl;
  // }

  for (int i = 0; i < q; i++) {
    int x = xy[i].first, y = xy[i].second;
    cout << dp[x] + dp[y] << endl;
  }

  return 0;
}