Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
typedef int64_t llong;
char moji[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
                 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
                 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char moji2[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
                  'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
                  'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
char moji3[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
#define Sort(a) sort(a.begin(), a.end());
#define Reverse(a) reverse(a.begin(), a.end());
#define ld PRId64
#define n_max 1000
int n;

void BFS(vector<pair<llong, llong>> tree[], llong saitan[], int tmp,
         bool visited[]) {
  for (auto itr = tree[tmp].begin(); itr != tree[tmp].end(); itr++) {
    if (not visited[(*itr).first]) {
      saitan[(*itr).first] = saitan[tmp] + (*itr).second;
      visited[(*itr).first] = true;
      BFS(tree, saitan, (*itr).first, visited);
    }
  }
}

int main() {
  cin >> n;
  vector<pair<llong, llong>> tree[n_max + 5];
  llong a, b, c;
  REP(i, n - 1) {
    scanf("%ld%ld%ld", &a, &b, &c);
    tree[a].push_back(make_pair(b, c));
    tree[b].push_back(make_pair(a, c));
  }
  int Q, K;
  cin >> Q >> K;

  llong saitan[n_max + 5] = {};
  bool visited[n_max + 5];
  saitan[K] = 0;
  visited[K] = true;
  BFS(tree, saitan, K, visited);

  int x, y;
  REP(i, Q) {
    scanf("%d%d", &x, &y);
    printf("%ld\n", saitan[x] + saitan[y]);
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
typedef int64_t llong;
char moji[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
                 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
                 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char moji2[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
                  'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
                  'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
char moji3[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
#define Sort(a) sort(a.begin(), a.end());
#define Reverse(a) reverse(a.begin(), a.end());
#define ld PRId64
#define n_max 100000
int n;

void BFS(vector<pair<llong, llong>> tree[], llong saitan[], int tmp,
         bool visited[]) {
  for (auto itr = tree[tmp].begin(); itr != tree[tmp].end(); itr++) {
    if (not visited[(*itr).first]) {
      saitan[(*itr).first] = saitan[tmp] + (*itr).second;
      visited[(*itr).first] = true;
      BFS(tree, saitan, (*itr).first, visited);
    }
  }
}

int main() {
  cin >> n;
  vector<pair<llong, llong>> tree[n_max + 5];
  llong a, b, c;
  REP(i, n - 1) {
    scanf("%ld%ld%ld", &a, &b, &c);
    tree[a].push_back(make_pair(b, c));
    tree[b].push_back(make_pair(a, c));
  }
  int Q, K;
  cin >> Q >> K;

  llong saitan[n_max + 5] = {};
  bool visited[n_max + 5];
  saitan[K] = 0;
  visited[K] = true;
  BFS(tree, saitan, K, visited);

  int x, y;
  REP(i, Q) {
    scanf("%d%d", &x, &y);
    printf("%ld\n", saitan[x] + saitan[y]);
  }
}