Problem p03634 (C++)

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

#define FOR(i, a, b) for (ll(i) = a; (i) < (b); ++(i))
#define RFOR(i, a, b) for (ll(i) = a; (i) >= (b); --(i))
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, n, 0)
#define ALL(v) (v).begin(), (v).end()
#define BIT(n) (1LL << (n))
#define IN(x, l, r) ((l) <= (x) && (x) <= (r))
#define SHOW(x) cout << #x << " = " << x << endl
#define SORT(v) sort(ALL(v))
#define SZ(v) (ll)(v).size()
#define UNIQ(v)                                                                \
  SORT(v);                                                                     \
  (v).erase(unique(ALL(v)), (v).end())
#define PERM(c) for (bool p = 1; p; p = next_permutation(ALL(c)))
#define PQ(T) priority_queue<T, vector<T>, greater<T>>

template <typename T> string join(const vector<T> &v, string d = " ") {
  stringstream s;
  REP(i, SZ(v)) s << d << v[i];
  return s.str().substr(d.length());
}

template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
  if (SZ(v))
    s << join(v, ", ");
  return s;
}

const ll inf = 1e15;
const ll mod = 1e9 + 7;
const double eps = 1e-10;

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

struct edge {
  ll from, to, cost;
};

int main() {
  ll N;
  cin >> N;

  map<ll, vector<edge>> mp;
  REP(i, N - 1) {
    ll a, b, c;
    cin >> a >> b >> c;
    mp[b].push_back((edge){b, a, c});
    mp[a].push_back((edge){a, b, c});
  }

  ll Q, K;
  cin >> Q >> K;
  vector<ll> x(N), y(N);
  REP(i, Q) cin >> x[i] >> y[i];

  vector<ll> dist(N + 1, 0);
  queue<edge> que;
  for (edge e : mp[K])
    que.push(e);

  while (!que.empty()) {
    edge e1 = que.front();
    que.pop();
    if (dist[e1.to] != 0)
      continue;

    dist[e1.to] = e1.cost + dist[e1.from];
    for (edge e2 : mp[e1.to])
      que.push(e2);
  }

  REP(i, Q) cout << dist[x[i]] + dist[y[i]] << endl;

  return 0;
}


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

#define FOR(i, a, b) for (ll(i) = a; (i) < (b); ++(i))
#define RFOR(i, a, b) for (ll(i) = a; (i) >= (b); --(i))
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, n, 0)
#define ALL(v) (v).begin(), (v).end()
#define BIT(n) (1LL << (n))
#define IN(x, l, r) ((l) <= (x) && (x) <= (r))
#define SHOW(x) cout << #x << " = " << x << endl
#define SORT(v) sort(ALL(v))
#define SZ(v) (ll)(v).size()
#define UNIQ(v)                                                                \
  SORT(v);                                                                     \
  (v).erase(unique(ALL(v)), (v).end())
#define PERM(c) for (bool p = 1; p; p = next_permutation(ALL(c)))
#define PQ(T) priority_queue<T, vector<T>, greater<T>>

template <typename T> string join(const vector<T> &v, string d = " ") {
  stringstream s;
  REP(i, SZ(v)) s << d << v[i];
  return s.str().substr(d.length());
}

template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
  if (SZ(v))
    s << join(v, ", ");
  return s;
}

const ll inf = 1e15;
const ll mod = 1e9 + 7;
const double eps = 1e-10;

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

struct edge {
  ll from, to, cost;
};

int main() {
  ll N;
  cin >> N;

  map<ll, vector<edge>> mp;
  REP(i, N - 1) {
    ll a, b, c;
    cin >> a >> b >> c;
    mp[b].push_back((edge){b, a, c});
    mp[a].push_back((edge){a, b, c});
  }

  ll Q, K;
  cin >> Q >> K;
  vector<ll> x(Q), y(Q);
  REP(i, Q) cin >> x[i] >> y[i];

  vector<ll> dist(N + 1, 0);
  queue<edge> que;
  for (edge e : mp[K])
    que.push(e);

  while (!que.empty()) {
    edge e1 = que.front();
    que.pop();
    if (dist[e1.to] != 0)
      continue;

    dist[e1.to] = e1.cost + dist[e1.from];
    for (edge e2 : mp[e1.to])
      que.push(e2);
  }

  REP(i, Q) cout << dist[x[i]] + dist[y[i]] << endl;

  return 0;
}