prithivMLmods/Coder-Stat huggingface dataset:
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 dump(x) cerr << #x << " = " << (x) << endl;
#define INF2 1000000000000000000
#define INF 2000000000
#define int long long int
struct edge {
  int to, cost;
};
typedef pair<int, int> P;

int V;
vector<edge> G[10010];
int d[10010];

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V + 1, INF2);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

signed main(void) {
  cin >> V;
  rep(i, V - 1) {
    int s, t, c;
    cin >> s >> t >> c;
    G[s].push_back({t, c});
    G[t].push_back({s, c});
  }
  int Q, K;
  cin >> Q >> K;
  dijkstra(K);
  int x[Q], y[Q];
  rep(i, Q) cin >> x[i] >> y[i];
  rep(i, Q) cout << d[x[i]] + d[y[i]] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF2 1000000000000000000
#define INF 2000000000
#define int long long int
struct edge {
  int to, cost;
};
typedef pair<int, int> P;

int V;
vector<edge> G[100010];
int d[100010];

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V + 1, INF2);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

signed main(void) {
  cin >> V;
  rep(i, V - 1) {
    int s, t, c;
    cin >> s >> t >> c;
    G[s].push_back({t, c});
    G[t].push_back({s, c});
  }
  int Q, K;
  cin >> Q >> K;
  dijkstra(K);
  int x[Q], y[Q];
  rep(i, Q) cin >> x[i] >> y[i];
  rep(i, Q) cout << d[x[i]] + d[y[i]] << endl;
  return 0;
}
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

int main() {
  int n, a, b, Q, k, x, y;
  long long int c;
  map<int, long long int> vm[100000];

  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    scanf("%d %d %lld", &a, &b, &c);
    vm[a][b] = c;
    vm[b][a] = c;
  }
  scanf("%d %d", &Q, &k);

  long long int d[100001];
  for (int i = 1; i <= n; i++) {
    if (i == k) {
      d[k] = 0;
    } else {
      d[i] = -1;
    }
  }
  vector<int> v;
  v.push_back(k);
  while (!v.empty()) {
    int v1 = v.back();
    v.pop_back();
    for (auto itr = vm[v1].begin(); itr != vm[v1].end(); itr++) {
      int v2 = itr->first;
      if (d[v2] == -1) {
        d[v2] = d[v1] + itr->second;
        v.push_back(v2);
      }
    }
  }

  for (int i = 0; i < Q; i++) {
    scanf("%d %d", &x, &y);
    printf("%lld\n", d[x] + d[y]);
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

int main() {
  int n, a, b, Q, k, x, y;
  long long int c;
  map<int, long long int> vm[100001];

  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    scanf("%d %d %lld", &a, &b, &c);
    vm[a][b] = c;
    vm[b][a] = c;
  }
  scanf("%d %d", &Q, &k);

  long long int d[100001];
  for (int i = 1; i <= n; i++) {
    if (i == k) {
      d[k] = 0;
    } else {
      d[i] = -1;
    }
  }
  vector<int> v;
  v.push_back(k);
  while (!v.empty()) {
    int v1 = v.back();
    v.pop_back();
    for (auto itr = vm[v1].begin(); itr != vm[v1].end(); itr++) {
      int v2 = itr->first;
      if (d[v2] == -1) {
        d[v2] = d[v1] + itr->second;
        v.push_back(v2);
      }
    }
  }

  for (int i = 0; i < Q; i++) {
    scanf("%d %d", &x, &y);
    printf("%lld\n", d[x] + d[y]);
  }

  return 0;
}
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxx = 1e5 + 100;
const int INF = 99999999;
const int MOD = 1e9 + 7;
typedef long long ll;
int n;
struct node {
  int r, nextt;
  ll dis;
} s[maxx * 2];
ll d[maxx];
int h[maxx];
int cnt;
void add(int a, int b, ll c) {
  s[cnt].r = b;
  s[cnt].dis = c;
  s[cnt].nextt = h[a];
  h[a] = cnt;
  cnt++;
}
void dfs(int k, int dd) {
  for (int i = h[k]; i != -1; i = s[i].nextt) {
    int v = s[i].r;
    // if(v==dd)  continue;
    d[v] = d[k] + s[i].dis;
    dfs(v, k);
  }
}
int main() {
  scanf("%d", &n);
  int a, b;
  ll c;
  cnt = 0;
  memset(h, -1, sizeof(h));
  memset(d, 0, sizeof(d));
  for (int i = 0; i < n - 1; i++) {
    scanf("%d%d%lld", &a, &b, &c);
    add(a, b, c);
    add(b, a, c);
  }
  int m, k;
  scanf("%d%d", &m, &k);
  dfs(k, -1);
  while (m--) {
    scanf("%d%d", &a, &b);
    cout << d[a] + d[b] << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxx = 1e5 + 100;
const int INF = 99999999;
const int MOD = 1e9 + 7;
typedef long long ll;
int n;
struct node {
  int r, nextt;
  ll dis;
} s[maxx * 2];
ll d[maxx];
int h[maxx];
int cnt;
void add(int a, int b, ll c) {
  s[cnt].r = b;
  s[cnt].dis = c;
  s[cnt].nextt = h[a];
  h[a] = cnt;
  cnt++;
}
void dfs(int k, int dd) {
  for (int i = h[k]; i != -1; i = s[i].nextt) {
    int v = s[i].r;
    if (v == dd)
      continue;
    d[v] = d[k] + s[i].dis;
    dfs(v, k);
  }
}
int main() {
  scanf("%d", &n);
  int a, b;
  ll c;
  cnt = 0;
  memset(h, -1, sizeof(h));
  memset(d, 0, sizeof(d));
  for (int i = 0; i < n - 1; i++) {
    scanf("%d%d%lld", &a, &b, &c);
    add(a, b, c);
    add(b, a, c);
  }
  int m, k;
  scanf("%d%d", &m, &k);
  dfs(k, -1);
  while (m--) {
    scanf("%d%d", &a, &b);
    cout << d[a] + d[b] << endl;
  }
  return 0;
}
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define all(vec) vec.begin(), vec.end()
typedef long long int ll;
typedef pair<ll, ll> P;
const ll MOD = 1000000007;
const ll INF = 1000000010;
const ll LINF = 4000000000000000010LL;
const int MAX = 310;
const double EPS = 1e-9;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
struct edge {
  int to;
  ll cost;
};
vector<edge> G[100010];
vector<ll> dijkstra(int s, vector<edge> g[]) {
  vector<ll> d(MAX, LINF);
  priority_queue<P, vector<P>, greater<P>> q;
  d[s] = 0;
  q.push(P(0, s));
  while (!q.empty()) {
    P p = q.top();
    q.pop();
    int v = p.second;
    for (int i = 0; i < (int)g[v].size(); i++) {
      edge e = g[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        q.push(P(d[e.to], e.to));
      }
    }
  }
  return d;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    G[a].push_back({b, c});
    G[b].push_back({a, c});
  }
  int q, k;
  cin >> q >> k;
  k--;
  vector<ll> d = dijkstra(k, G);
  while (q--) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    cout << d[x] + d[y] << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define all(vec) vec.begin(), vec.end()
typedef long long int ll;
typedef pair<ll, ll> P;
const ll MOD = 1000000007;
const ll INF = 1000000010;
const ll LINF = 4000000000000000010LL;
const int MAX = 100010;
const double EPS = 1e-9;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
struct edge {
  int to;
  ll cost;
};
vector<edge> G[100010];
vector<ll> dijkstra(int s, vector<edge> g[]) {
  vector<ll> d(MAX, LINF);
  priority_queue<P, vector<P>, greater<P>> q;
  d[s] = 0;
  q.push(P(0, s));
  while (!q.empty()) {
    P p = q.top();
    q.pop();
    int v = p.second;
    for (int i = 0; i < (int)g[v].size(); i++) {
      edge e = g[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        q.push(P(d[e.to], e.to));
      }
    }
  }
  return d;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    G[a].push_back({b, c});
    G[b].push_back({a, c});
  }
  int q, k;
  cin >> q >> k;
  k--;
  vector<ll> d = dijkstra(k, G);
  while (q--) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    cout << d[x] + d[y] << endl;
  }
  return 0;
}
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
using edge = struct {
  int to;
  ll cost;
};
vector<edge> tree[10010];
ll depth[10010];

void dfs(int v, int p, ll d) {
  depth[v] = d;
  for (auto &e : tree[v]) {
    if (e.to == p)
      continue;
    dfs(e.to, v, d + e.cost);
  }
}

int main() {

  int n;
  cin >> n;
  rep(i, n - 1) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    tree[a].push_back({b, c});
    tree[b].push_back({a, c});
  }
  int q, k;
  cin >> q >> k;
  k--;

  dfs(k, -1, 0);
  rep(i, q) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    cout << depth[x] + depth[y] << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
using edge = struct {
  int to;
  ll cost;
};
vector<vector<edge>> tree(100010);
vector<ll> depth(100010);

void dfs(int v, int p, ll d) {
  depth[v] = d;
  for (auto &e : tree[v]) {
    if (e.to == p)
      continue;
    dfs(e.to, v, d + e.cost);
  }
}

int main() {

  int n;
  cin >> n;
  rep(i, n - 1) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    tree[a].push_back({b, c});
    tree[b].push_back({a, c});
  }
  int q, k;
  cin >> q >> k;
  k--;

  dfs(k, -1, 0);
  rep(i, q) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    cout << depth[x] + depth[y] << endl;
  }
  return 0;
}
Problem p03634 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
const ll mod = ll(1e9) + 7;
const int INF = int(1e9);

int main() {
  int N;
  cin >> N;
  vector<vector<pair<int, ll>>> e(N);
  rep(i, N - 1) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    e[b].push_back({a, c});
    e[a].push_back({b, c});
  }
  int Q, K;
  cin >> Q >> K;
  K--;
  vector<ll> cost(N);
  cost[K] = 0;
  queue<pair<int, ll>> que;
  rep(i, e[K].size()) que.push(e[K][i]);
  while (!que.empty()) {
    int node = que.front().first;
    ll cst = que.front().second;
    que.pop();
    cost[node] = cst;
    rep(i, e[node].size()) {
      if (cost[e[node][i].first] == 0) {
        que.push({e[node][i].first, e[node][i].second + cst});
      }
    }
    // cout << node << endl;
  }
  vector<ll> ans;
  rep(i, Q) {
    int x, y;
    cin >> x >> y;
    ans.push_back(cost[x - 1] + cost[y - 1]);
  }
  rep(i, Q) cout << ans[i] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
const ll mod = ll(1e9) + 7;
const int INF = int(1e9);

int main() {
  int N;
  cin >> N;
  vector<vector<pair<int, ll>>> e(N);
  rep(i, N - 1) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    e[b].push_back({a, c});
    e[a].push_back({b, c});
  }
  int Q, K;
  cin >> Q >> K;
  K--;
  vector<ll> cost(N);
  cost[K] = 0;
  queue<pair<int, ll>> que;
  rep(i, e[K].size()) que.push(e[K][i]);
  while (!que.empty()) {
    int node = que.front().first;
    ll cst = que.front().second;
    que.pop();
    cost[node] = cst;
    rep(i, e[node].size()) {
      if (e[node][i].first != K && cost[e[node][i].first] == 0) {
        que.push({e[node][i].first, e[node][i].second + cst});
      }
    }
    // cout << node << endl;
  }
  vector<ll> ans;
  rep(i, Q) {
    int x, y;
    cin >> x >> y;
    ans.push_back(cost[x - 1] + cost[y - 1]);
  }
  rep(i, Q) cout << ans[i] << endl;
  return 0;
}
Problem p03634 (C++)

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

using namespace std;

typedef long long ll;

int main() {
  ll N;
  cin >> N;
  vector<vector<ll>> to(N);
  vector<vector<ll>> cost(N);
  for (ll i = 0; i < N; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--; // 0-indexed
    to[a].push_back(b);
    to[b].push_back(a);
    cost[a].push_back(c);
    cost[b].push_back(c);
  }

  ll Q, K;
  cin >> Q >> K;
  K--;

  // K~を中心としたBFS
  vector<ll> path(N, -1); //-1は未到達、

  queue<ll> q;
  q.push(K);
  path[K] = 0;

  while (!q.empty()) {
    ll p = q.front();
    q.pop();
    for (ll i = 0; i < to[p].size(); i++) {
      ll r = to[p][i];
      if (path[r] != -1) {
        continue;
      } else {
        path[r] = path[p] + cost[p][i];
        q.push(r);
      }
    }
  }

  for (ll i = 0; i < Q; i++) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    cout << path[x] + path[y] << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
  ll N;
  cin >> N;
  vector<vector<ll>> to(N);
  vector<vector<ll>> cost(N);
  for (ll i = 0; i < N - 1; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--; // 0-indexed
    to[a].push_back(b);
    to[b].push_back(a);
    cost[a].push_back(c);
    cost[b].push_back(c);
  }

  ll Q, K;
  cin >> Q >> K;
  K--;

  // K~を中心としたBFS
  vector<ll> path(N, -1); //-1は未到達、

  queue<ll> q;
  q.push(K);
  path[K] = 0;

  while (!q.empty()) {
    ll p = q.front();
    q.pop();
    for (ll i = 0; i < to[p].size(); i++) {
      ll r = to[p][i];
      if (path[r] != -1) {
        continue;
      } else {
        path[r] = path[p] + cost[p][i];
        q.push(r);
      }
    }
  }

  for (ll i = 0; i < Q; i++) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    cout << path[x] + path[y] << endl;
  }

  return 0;
}
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef long long int lld;

const lld mod = 1e9 + 7;
const lld INF = 1e9;
const lld MAXN = 1e2;

struct edge {
  lld node, cost;
  edge() {
    node = 0;
    cost = 0;
  }
};

vector<edge> e[MAXN];
lld dist[MAXN] = {0};

void dfs(lld p) {
  if (dist[p] >= 0) {
    lld s = e[p].size();
    for (int i = 0; i < s; i++) {
      if (dist[e[p][i].node] < 0) {
        dist[e[p][i].node] = dist[p] + e[p][i].cost;
        dfs(e[p][i].node);
      }
    }
  }
}

int main() {
  lld n, q, k;

  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int aa, bb, cc;
    cin >> aa >> bb >> cc;

    edge f;
    f.node = bb - 1;
    f.cost = cc;

    e[aa - 1].push_back(f);

    f.node = aa - 1;
    e[bb - 1].push_back(f);
  }

  for (int i = 0; i < n; i++) {
    dist[i] = -1;
  }

  cin >> q >> k;

  dist[k - 1] = 0;

  dfs(k - 1);

  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    cout << dist[x - 1] + dist[y - 1] << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef long long int lld;

const lld mod = 1e9 + 7;
const lld INF = 1e9;
const lld MAXN = 1e5;

struct edge {
  lld node, cost;
  edge() {
    node = 0;
    cost = 0;
  }
};

vector<edge> e[MAXN];
lld dist[MAXN] = {0};

void dfs(lld p) {
  if (dist[p] >= 0) {
    lld s = e[p].size();
    for (int i = 0; i < s; i++) {
      if (dist[e[p][i].node] < 0) {
        dist[e[p][i].node] = dist[p] + e[p][i].cost;
        dfs(e[p][i].node);
      }
    }
  }
}

int main() {
  lld n, q, k;

  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int aa, bb, cc;
    cin >> aa >> bb >> cc;

    edge f;
    f.node = bb - 1;
    f.cost = cc;

    e[aa - 1].push_back(f);

    f.node = aa - 1;
    e[bb - 1].push_back(f);
  }

  for (int i = 0; i < n; i++) {
    dist[i] = -1;
  }

  cin >> q >> k;

  dist[k - 1] = 0;

  dfs(k - 1);

  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    cout << dist[x - 1] + dist[y - 1] << endl;
  }

  return 0;
}
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
#define clr(x, y) memset(x, y, sizeof x)
#define ll long long
using namespace std;

const int maxn = 100010;

struct Edge {
  int to, next;
  ll w;
} edge[maxn];
int head[maxn];
ll f[maxn], cnt;
void init() {
  cnt = 0;
  clr(head, -1);
  clr(f, 0);
}

void add(int u, int v, int w) {
  edge[cnt].to = v;
  edge[cnt].w = w;
  edge[cnt].next = head[u];
  head[u] = cnt++;
}

void dfs(int u, ll dist, int fa) {
  f[u] = dist;
  for (int i = head[u]; ~i; i = edge[i].next) {
    int v = edge[i].to;
    if (v == fa)
      continue;
    dfs(v, dist + edge[i].w, u);
  }
}

int u, v, w, q, k;
int main() {
  int n;
  scanf("%d", &n);
  init();
  for (int i = 1; i < n; i++) {
    scanf("%d%d%d", &u, &v, &w);
    add(u, v, w);
    add(v, u, w);
  }
  scanf("%d%d", &q, &k);
  dfs(k, 0, -1);
  for (int i = 1; i <= q; i++) {
    scanf("%d%d", &u, &v);
    printf("%lld\n", f[u] + f[v]);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#define clr(x, y) memset(x, y, sizeof x)
#define ll long long
using namespace std;

const int maxn = 200010;

struct Edge {
  int to, next;
  ll w;
} edge[maxn];
int head[maxn];
ll f[maxn], cnt;
void init() {
  cnt = 0;
  clr(head, -1);
  clr(f, 0);
}

void add(int u, int v, int w) {
  edge[cnt].to = v;
  edge[cnt].w = w;
  edge[cnt].next = head[u];
  head[u] = cnt++;
}

void dfs(int u, ll dist, int fa) {
  f[u] = dist;
  for (int i = head[u]; ~i; i = edge[i].next) {
    int v = edge[i].to;
    if (v == fa)
      continue;
    dfs(v, dist + edge[i].w, u);
  }
}

int u, v, w, q, k;
int main() {
  int n;
  scanf("%d", &n);
  init();
  for (int i = 1; i < n; i++) {
    scanf("%d%d%d", &u, &v, &w);
    add(u, v, w);
    add(v, u, w);
  }
  scanf("%d%d", &q, &k);
  dfs(k, 0, -1);
  for (int i = 1; i <= q; i++) {
    scanf("%d%d", &u, &v);
    printf("%lld\n", f[u] + f[v]);
  }
  return 0;
}
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <vector>
#define rep(i, N) for (int i = 0; i < (int)N; i++)
using namespace std;
typedef long long ll;
const ll LLINF = 9223372036854775807;
const int MOD = 1000000007;

const int N_max = 10010;
using edge = struct {
  int to;
  ll cost;
};
vector<edge> tree[N_max];
ll dis[N_max];

void dfs(int v, int p, ll d) {
  dis[v] = d;
  for (auto &e : tree[v]) {
    if (e.to == p)
      continue;
    dfs(e.to, v, d + e.cost);
  }
}

int main() {
  int N;
  cin >> N;
  rep(i, N - 1) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    tree[a].push_back({b, c});
    tree[b].push_back({a, c});
  }

  int Q, K;
  cin >> Q >> K;
  K--;

  dfs(K, -1, 0);

  rep(i, Q) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    ll result = dis[x] + dis[y];
    cout << result << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <vector>
#define rep(i, N) for (int i = 0; i < (int)N; i++)
using namespace std;
typedef long long ll;
const ll LLINF = 9223372036854775807;
const int MOD = 1000000007;

const int N_max = 100000;
using edge = struct {
  int to;
  ll cost;
};
vector<edge> tree[N_max];
ll dis[N_max];

void dfs(int v, int p, ll d) {
  dis[v] = d;
  for (auto &e : tree[v]) {
    if (e.to == p)
      continue;
    dfs(e.to, v, d + e.cost);
  }
}

int main() {
  int N;
  cin >> N;
  rep(i, N - 1) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    tree[a].push_back({b, c});
    tree[b].push_back({a, c});
  }

  int Q, K;
  cin >> Q >> K;
  K--;

  dfs(K, -1, 0);

  rep(i, Q) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    ll result = dis[x] + dis[y];
    cout << result << endl;
  }

  return 0;
}
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define MAX 100005
#define ll long long
struct Edge {
  ll to;
  ll next;
  ll cost;
};
Edge edge[MAX];
ll head[MAX];
ll dis[MAX];
void DFS(ll u, ll v, ll w) {
  dis[u] = w;
  for (ll i = head[u]; i != -1; i = edge[i].next) {
    ll to = edge[i].to;
    if (to == v)
      continue;
    DFS(to, u, w + edge[i].cost);
  }
  return;
}
int main() {
  ll n, u, v, w, q, k;
  scanf("%lld", &n);
  ll cnt = 0;
  memset(head, -1, sizeof(head));
  for (ll i = 1; i <= n - 1; i++) {
    scanf("%lld%lld%lld", &u, &v, &w);
    edge[cnt].to = v;
    edge[cnt].cost = w;
    edge[cnt].next = head[u];
    head[u] = cnt++;
    edge[cnt].to = u;
    edge[cnt].cost = w;
    edge[cnt].next = head[v];
    head[v] = cnt++;
  }
  scanf("%lld%lld", &q, &k);
  DFS(k, -1, 0);
  while (q--) {
    scanf("%lld%lld", &u, &v);
    printf("%lld\n", dis[u] + dis[v]);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define MAX 100005
#define ll long long
struct Edge {
  ll to;
  ll next;
  ll cost;
};
Edge edge[MAX * 2];
ll head[MAX];
ll dis[MAX];
void DFS(ll u, ll v, ll w) {
  dis[u] = w;
  for (ll i = head[u]; i != -1; i = edge[i].next) {
    ll to = edge[i].to;
    if (to == v)
      continue;
    DFS(to, u, w + edge[i].cost);
  }
  return;
}
int main() {
  ll n, u, v, w, q, k;
  scanf("%lld", &n);
  ll cnt = 0;
  memset(head, -1, sizeof(head));
  for (ll i = 1; i <= n - 1; i++) {
    scanf("%lld%lld%lld", &u, &v, &w);
    edge[cnt].to = v;
    edge[cnt].cost = w;
    edge[cnt].next = head[u];
    head[u] = cnt++;
    edge[cnt].to = u;
    edge[cnt].cost = w;
    edge[cnt].next = head[v];
    head[v] = cnt++;
  }
  scanf("%lld%lld", &q, &k);
  DFS(k, -1, 0);
  while (q--) {
    scanf("%lld%lld", &u, &v);
    printf("%lld\n", dis[u] + dis[v]);
  }
  return 0;
}
Problem p03634 (C++)

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

#define ll long long
#define limit 100002
struct edge {
  int to;
  int cost;
};

vector<edge> arr[limit];
ll depth[limit];
void dfs(int v, int p, ll dist) {
  depth[v] = dist;

  for (auto x : arr[v]) {
    if (x.to == p) {
      continue;
    } else {
      dfs(x.to, v, dist + x.cost);
    }
  }
}
int main() {

  int n;
  cin >> n;

  for (int i = 0; i < n - 1; i++) {

    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;

    arr[a].push_back({b, c});
    arr[b].push_back({a, c});
  }
  int q, k;
  cin >> q >> k;
  k--;
  dfs(k, -1, 0);
  for (int i = 0; i < q; i++) {

    int x, y;
    x--, y--;

    cout << depth[x] + depth[y] << endl;
  }
}


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

#define ll long long
#define limit 100002
struct edge {
  int to;
  int cost;
};

vector<edge> arr[limit];
ll depth[limit];
void dfs(int v, int p, ll dist) {
  depth[v] = dist;

  for (auto x : arr[v]) {
    if (x.to == p) {
      continue;
    } else {
      dfs(x.to, v, dist + x.cost);
    }
  }
}
int main() {

  int n;
  cin >> n;

  for (int i = 0; i < n - 1; i++) {

    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;

    arr[a].push_back({b, c});
    arr[b].push_back({a, c});
  }
  int q, k;
  cin >> q >> k;
  k--;
  dfs(k, -1, 0);
  for (int i = 0; i < q; i++) {

    int x, y;
    cin >> x >> y;
    x--, y--;

    cout << depth[x] + depth[y] << endl;
  }
}
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn = 100000 + 5;
typedef long long ll;
ll edge_num, m, n, q, k;
ll head[maxn];
ll d[maxn];

struct Edge {
  ll from, to, next;
  ll dis;
} edge[maxn];

void add_edge(ll from, ll to, ll c) {
  edge[++edge_num].next = head[from];
  edge[edge_num].from = from;
  edge[edge_num].to = to;
  edge[edge_num].dis = c;
  head[from] = edge_num;
}

bool vis[maxn];
void dfs(ll p) {
  vis[p] = true;
  for (ll i = head[p]; i; i = edge[i].next) {
    ll to = edge[i].to;
    if (vis[to])
      continue;
    d[to] = d[p] + edge[i].dis;
    dfs(to);
  }
}

int main() {
  cin >> n;
  for (ll i = 1; i < n; i++) {
    ll x, y, c;
    cin >> x >> y >> c;
    add_edge(x, y, c);
    add_edge(y, x, c);
  }
  cin >> q >> k;
  dfs(k);
  for (ll i = 1; i <= q; i++) {
    ll qa, qb;
    cin >> qa >> qb;
    cout << d[qa] + d[qb] << "\n";
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn = 1000000 + 5;
typedef long long ll;
ll edge_num, m, n, q, k;
ll head[maxn];
ll d[maxn];

struct Edge {
  ll from, to, next;
  ll dis;
} edge[maxn];

void add_edge(ll from, ll to, ll c) {
  edge[++edge_num].next = head[from];
  edge[edge_num].from = from;
  edge[edge_num].to = to;
  edge[edge_num].dis = c;
  head[from] = edge_num;
}

bool vis[maxn];
void dfs(ll p) {
  vis[p] = true;
  for (ll i = head[p]; i; i = edge[i].next) {
    ll to = edge[i].to;
    if (vis[to])
      continue;
    d[to] = d[p] + edge[i].dis;
    dfs(to);
  }
}

int main() {
  cin >> n;
  for (ll i = 1; i < n; i++) {
    ll x, y, c;
    cin >> x >> y >> c;
    add_edge(x, y, c);
    add_edge(y, x, c);
  }
  cin >> q >> k;
  dfs(k);
  for (ll i = 1; i <= q; i++) {
    ll qa, qb;
    cin >> qa >> qb;
    cout << d[qa] + d[qb] << "\n";
  }
  return 0;
}
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>

using namespace std;
map<long long, long long> lpath[100000];
long long dist[100000] = {};
int visited[100000] = {};
long long n;

int dfs(long long s, long long cost) {
  map<long long, long long> mp_dist = lpath[s];
  visited[s] = 1;
  for (auto it = mp_dist.begin(); it != mp_dist.end(); it++) {
    if (visited[it->first] == 0) {
      //      cout << s << "\t" << it->first << "\t" << it->second << endl;
      dist[it->first] = cost + it->second;
      //      visited[it->first] = 1;
      dfs(it->first, dist[it->first]);
    }
  }
  return (0);
}

int main() {
  long long q, k;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    long long a, b, c;
    cin >> a >> b >> c;
    lpath[a][b] = c;
    lpath[b][a] = c;
  }
  cin >> q >> k;
  dfs(k, 0);

  for (int i = 0; i < q; i++) {
    long long x, y;
    cin >> x >> y;
    cout << dist[x] + dist[y] << endl;
  }
  return (0);
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>

using namespace std;
map<long long, long long> lpath[100001];
long long dist[100001] = {};
int visited[100001] = {};
long long n;

int dfs(long long s, long long cost) {
  map<long long, long long> mp_dist = lpath[s];
  visited[s] = 1;
  for (auto it = mp_dist.begin(); it != mp_dist.end(); it++) {
    if (visited[it->first] == 0) {
      //      cout << s << "\t" << it->first << "\t" << it->second << endl;
      dist[it->first] = cost + it->second;
      //      visited[it->first] = 1;
      dfs(it->first, dist[it->first]);
    }
  }
  return (0);
}

int main() {
  long long q, k;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    long long a, b, c;
    cin >> a >> b >> c;
    lpath[a][b] = c;
    lpath[b][a] = c;
  }
  cin >> q >> k;
  dfs(k, 0);

  for (int i = 0; i < q; i++) {
    long long x, y;
    cin >> x >> y;
    cout << dist[x] + dist[y] << endl;
  }
  return (0);
}
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const ll mod = 1000000007;
const int inf = 1e9;
const long long INF = 1LL << 60;

struct edge {
  ll to, cost;
};
vector<edge> G[100050];
ll d[10050];

void dfs(int s, int p, ll dis) {
  d[s] = dis;
  for (auto j : G[s]) {
    if (j.to == p)
      continue;
    dfs(j.to, s, dis + j.cost);
  }
}

int main() {
  int n;
  cin >> n;

  for (int i = 0; i < n - 1; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    G[a].push_back((edge){b, c});
    G[b].push_back((edge){a, c});
  }
  int q, k;
  cin >> q >> k;
  k--;
  dfs(k, -1, 0);
  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    cout << d[x] + d[y] << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const ll mod = 1000000007;
const int inf = 1e9;
const long long INF = 1LL << 60;

struct edge {
  ll to, cost;
};
vector<edge> G[100050];
ll d[100050];

void dfs(int s, int p, ll dis) {
  d[s] = dis;
  for (auto j : G[s]) {
    if (j.to == p)
      continue;
    dfs(j.to, s, dis + j.cost);
  }
}

int main() {
  int n;
  cin >> n;

  for (int i = 0; i < n - 1; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    G[a].push_back((edge){b, c});
    G[b].push_back((edge){a, c});
  }
  int q, k;
  cin >> q >> k;
  k--;
  dfs(k, -1, 0);
  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    cout << d[x] + d[y] << endl;
  }
}
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long n, m, u, v, w, q, k, d[50001], used[50001];
vector<pair<int, long long>> ve[50001];
void dfs(int s) {
  used[s] = 1;
  int sz = ve[s].size();
  for (int i = 0; i < sz; i++) {
    u = ve[s][i].first;
    w = ve[s][i].second;
    if (used[u] == false) {
      d[u] = d[s] + w;
      dfs(u);
    }
  }
}
int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    cin >> u >> v >> w;
    ve[u].push_back(make_pair(v, w));
    ve[v].push_back(make_pair(u, w));
  }
  cin >> q >> k;
  dfs(k);
  while (q--) {
    cin >> u >> w;
    cout << d[u] + d[w] << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long n, m, u, v, w, q, k, d[100001], used[100001];
vector<pair<int, long long>> ve[100001];
void dfs(int s) {
  used[s] = 1;
  int sz = ve[s].size();
  for (int i = 0; i < sz; i++) {
    u = ve[s][i].first;
    w = ve[s][i].second;
    if (used[u] == false) {
      d[u] = d[s] + w;
      dfs(u);
    }
  }
}
int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    cin >> u >> v >> w;
    ve[u].push_back(make_pair(v, w));
    ve[v].push_back(make_pair(u, w));
  }
  cin >> q >> k;
  dfs(k);
  while (q--) {
    cin >> u >> w;
    cout << d[u] + d[w] << endl;
  }
  return 0;
}
Problem p03634 (C++)

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

typedef long long LL;
typedef unsigned long long ULL;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1

const int maxn = 1e5 + 7;
const int inf = 2e9 + 7;
const int mod = 1e9 + 7;

struct {
  int u, v, w, next;
} edge[maxn];
int head[maxn];
int cnt = 0;
void add(int u, int v, int w) {
  edge[cnt].u = u;
  edge[cnt].v = v;
  edge[cnt].w = w;
  edge[cnt].next = head[u];
  head[u] = cnt++;
}
LL dis[maxn];
void dfs(int from, int now, LL tot) {
  if (dis[now] == -1)
    dis[now] = tot;
  for (int i = head[now]; i != -1; i = edge[i].next)
    if (edge[i].v != from)
      dfs(now, edge[i].v, tot + edge[i].w);
}

int main() {
#ifdef LOCAL
  freopen("sample.in", "r", stdin);
#endif
  // ios::sync_with_stdio(false);
  memset(head, -1, sizeof(head));
  memset(dis, -1, sizeof(dis));
  int n;
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    add(a, b, c);
    add(b, a, c);
  }
  int q, k;
  scanf("%d%d", &q, &k);
  dfs(-1, k, 0);
  while (q--) {
    int x, y;
    scanf("%d%d", &x, &y);
    printf("%lld\n", dis[x] + dis[y]);
  }
  return 0;
}


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

typedef long long LL;
typedef unsigned long long ULL;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1

const int maxn = 3e5 + 7;
const int inf = 2e9 + 7;
const int mod = 1e9 + 7;

struct {
  int u, v, w, next;
} edge[maxn];
int head[maxn];
int cnt = 0;
void add(int u, int v, int w) {
  edge[cnt].u = u;
  edge[cnt].v = v;
  edge[cnt].w = w;
  edge[cnt].next = head[u];
  head[u] = cnt++;
}
LL dis[maxn];
void dfs(int from, int now, LL tot) {
  if (dis[now] == -1)
    dis[now] = tot;
  for (int i = head[now]; i != -1; i = edge[i].next)
    if (edge[i].v != from)
      dfs(now, edge[i].v, tot + edge[i].w);
}

int main() {
#ifdef LOCAL
  freopen("sample.in", "r", stdin);
#endif
  // ios::sync_with_stdio(false);
  memset(head, -1, sizeof(head));
  memset(dis, -1, sizeof(dis));
  int n;
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    add(a, b, c);
    add(b, a, c);
  }
  int q, k;
  scanf("%d%d", &q, &k);
  dfs(-1, k, 0);
  while (q--) {
    int x, y;
    scanf("%d%d", &x, &y);
    printf("%lld\n", dis[x] + dis[y]);
  }
  return 0;
}
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;

#define LL long long int

struct edge {
  LL next;
  LL cost;
};

vector<vector<edge>> vec(10001);
vector<LL> total(10001, 0);
vector<bool> close(10001, false);

void dfs(int r) {
  close[r] = true;
  for (int i = 0; i < vec[r].size(); i++) {
    if (!close[vec[r][i].next]) {
      total[vec[r][i].next] = total[r] + vec[r][i].cost;
      dfs(vec[r][i].next);
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;

  cin >> n;

  for (int i = 0; i < n - 1; i++) {
    LL a, b, c;
    cin >> a >> b >> c;
    vec[a].push_back(edge{b, c});
    vec[b].push_back(edge{a, c});
  }

  int q, k;

  cin >> q >> k;

  vector<int> qx(q), qy(q);

  for (int i = 0; i < q; i++) {
    cin >> qx[i] >> qy[i];
  }

  dfs(k);

  for (int i = 0; i < q; i++) {
    cout << total[qx[i]] + total[qy[i]] << "\n";
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

#define LL long long int

struct edge {
  LL next;
  LL cost;
};

vector<vector<edge>> vec(100001);
vector<LL> total(100001, 0);
vector<bool> close(100001, false);

void dfs(int r) {
  close[r] = true;
  for (int i = 0; i < vec[r].size(); i++) {
    if (!close[vec[r][i].next]) {
      total[vec[r][i].next] = total[r] + vec[r][i].cost;
      dfs(vec[r][i].next);
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;

  cin >> n;

  for (int i = 0; i < n - 1; i++) {
    LL a, b, c;
    cin >> a >> b >> c;
    vec[a].push_back(edge{b, c});
    vec[b].push_back(edge{a, c});
  }

  int q, k;

  cin >> q >> k;

  vector<int> qx(q), qy(q);

  for (int i = 0; i < q; i++) {
    cin >> qx[i] >> qy[i];
  }

  dfs(k);

  for (int i = 0; i < q; i++) {
    cout << total[qx[i]] + total[qy[i]] << "\n";
  }

  return 0;
}
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define INF (1 << 30)
#define INFLL (1ll << 60)
typedef pair<ll, int> P;
#define MOD (1000000007ll)
#define l_ength size

int main(void) {
  int n, a, b, q, k, x, y, i;
  ll c, e, d[123456];
  P tmp;
  fill(d, d + 1233456, INFLL);
  vector<P> g[123456];
  priority_queue<P, vector<P>, greater<P>> pq;
  cin >> n;
  for (i = 1; i < n; ++i) {
    cin >> a >> b >> c;
    g[a].push_back(P(c, b));
    g[b].push_back(P(c, a));
  }
  cin >> q >> k;
  d[k] = 0ll;
  pq.push(P(d[k], k));
  while (!pq.empty()) {
    tmp = pq.top();
    pq.pop();
    a = tmp.second;
    c = tmp.first;
    for (i = (g[a].size() - 1); i >= 0; --i) {
      b = g[a][i].second;
      e = g[a][i].first;
      if (d[b] > c + e) {
        d[b] = c + e;
        pq.push(P(d[b], b));
      }
    }
  }
  for (i = 0; i < q; ++i) {
    cin >> x >> y;
    cout << (d[x] + d[y]) << endl;
  }
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define INF (1 << 30)
#define INFLL (1ll << 60)
typedef pair<ll, int> P;
#define MOD (1000000007ll)
#define l_ength size

int main(void) {
  int n, a, b, q, k, x, y, i;
  ll c, e, d[123456];
  P tmp;
  fill(d, d + 123456, INFLL);
  vector<P> g[123456];
  priority_queue<P, vector<P>, greater<P>> pq;
  cin >> n;
  for (i = 1; i < n; ++i) {
    cin >> a >> b >> c;
    g[a].push_back(P(c, b));
    g[b].push_back(P(c, a));
  }
  cin >> q >> k;
  d[k] = 0ll;
  pq.push(P(d[k], k));
  while (!pq.empty()) {
    tmp = pq.top();
    pq.pop();
    a = tmp.second;
    c = tmp.first;
    for (i = (g[a].size() - 1); i >= 0; --i) {
      b = g[a][i].second;
      e = g[a][i].first;
      if (d[b] > c + e) {
        d[b] = c + e;
        pq.push(P(d[b], b));
      }
    }
  }
  for (i = 0; i < q; ++i) {
    cin >> x >> y;
    cout << (d[x] + d[y]) << endl;
  }
  return 0;
}
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"

typedef long long ll;
#define int ll
#define fi first
#define se second
#define SORT(a) sort(a.begin(), a.end())
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define MP(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define INF LLONG_MAX / 2

using namespace std;

int MOD = 1000000007;

int used[100001] = {0};
vector<pair<int, int>> m1[10001];
int ans[100001];
int q, k;

void get_r(int x, int d) {
  ans[x] = d;
  if (m1[x].size() == 0)
    return;
  for (auto v : m1[x]) {
    if (used[v.fi] == 0) {
      used[v.fi] = 1;
      get_r(v.fi, d + v.se);
    }
  }
}

signed main() {

  // vector<pair<int,int>> m2[10001];

  int n;
  cin >> n;
  rep(i, n - 1) {
    int a, b, c;
    cin >> a >> b >> c;
    m1[a].pb(MP(b, c));
    m1[b].pb(MP(a, c));
  }
  rep(i, 100001) ans[i] = INF;

  cin >> q >> k;

  used[k] = 1;
  get_r(k, 0);

  rep(i, q) {
    int X, Y;
    cin >> X >> Y;
    cout << ans[X] + ans[Y] << endl;
  }

  return 0;
}
// g++ -std=c++14 code1.cpp
// rm -r -f test;oj dl

Fixed submission:
#include "bits/stdc++.h"

typedef long long ll;
#define int ll
#define fi first
#define se second
#define SORT(a) sort(a.begin(), a.end())
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define MP(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define INF LLONG_MAX / 2

using namespace std;

int MOD = 1000000007;

int used[100001] = {0};
vector<pair<int, int>> m1[100001];
int ans[100001];
int q, k;

void get_r(int x, int d) {
  ans[x] = d;
  if (m1[x].size() == 0)
    return;
  for (auto v : m1[x]) {
    if (used[v.fi] == 0) {
      used[v.fi] = 1;
      get_r(v.fi, d + v.se);
    }
  }
}

signed main() {

  // vector<pair<int,int>> m2[10001];

  int n;
  cin >> n;
  rep(i, n - 1) {
    int a, b, c;
    cin >> a >> b >> c;
    m1[a].pb(MP(b, c));
    m1[b].pb(MP(a, c));
  }
  rep(i, 100001) ans[i] = INF;

  cin >> q >> k;

  used[k] = 1;
  get_r(k, 0);

  rep(i, q) {
    int X, Y;
    cin >> X >> Y;
    cout << ans[X] + ans[Y] << endl;
  }

  return 0;
}
// g++ -std=c++14 code1.cpp
// rm -r -f test;oj dl
Problem p03634 (C++)

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

#define int long long

#define INF 4611686016279904256LL
#define MAX_V 100

struct edge {
  int to, cost;
};
typedef pair<int, int> P;

int V;
vector<edge> G[MAX_V];
int d[MAX_V];

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

signed main() {
  cin >> V;
  for (int i = 0; i < V - 1; i++) {
    int a, b;
    ll c;
    cin >> a >> b >> c;
    edge e1;
    e1.to = b - 1;
    e1.cost = c;
    edge e2;
    e2.to = a - 1;
    e2.cost = c;
    G[a - 1].push_back(e1);
    G[b - 1].push_back(e2);
  }
  int q, k;
  cin >> q >> k;
  dijkstra(k - 1);
  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    cout << d[x - 1] + d[y - 1] << endl;
  }
  return 0;
}

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

#define int long long

#define INF 4611686016279904256LL
#define MAX_V 100000

struct edge {
  int to, cost;
};
typedef pair<int, int> P;

int V;
vector<edge> G[MAX_V];
int d[MAX_V];

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

signed main() {
  cin >> V;
  for (int i = 0; i < V - 1; i++) {
    int a, b;
    ll c;
    cin >> a >> b >> c;
    edge e1;
    e1.to = b - 1;
    e1.cost = c;
    edge e2;
    e2.to = a - 1;
    e2.cost = c;
    G[a - 1].push_back(e1);
    G[b - 1].push_back(e2);
  }
  int q, k;
  cin >> q >> k;
  dijkstra(k - 1);
  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    cout << d[x - 1] + d[y - 1] << endl;
  }
  return 0;
}
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<string> vs;
typedef pair<ll, ll> P;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep1(i, n) for (ll i = 1; i <= (n); i++)
#define all(x) (x).begin(), (x).end()
vi dy = {0, 0, 1, -1};
vi dx = {1, -1, 0, 0};
constexpr auto INF = INT_MAX / 2;
constexpr auto MOD = 1000000007;

int main() {
  ll n;
  cin >> n;
  vector<vector<P>> path(n, vector<P>(0));

  rep(i, n - 1) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    path[a].push_back(make_pair(b, c));
    path[b].push_back(make_pair(a, c));
  }

  ll q, k;
  cin >> q >> k;
  k--;
  vector<ll> x(n), y(n);
  rep(i, q) {
    cin >> x[i] >> y[i];
    x[i]--;
    y[i]--;
  }

  vector<ll> dist(n, 5000000000000000);
  dist[k] = 0;

  priority_queue<P> que;
  que.push(make_pair(0, k));

  while (!que.empty()) {

    ll sv = que.top().second;
    que.pop();

    rep(i, path[sv].size()) {
      ll gv = path[sv][i].first;
      ll d = path[sv][i].second;

      if (dist[gv] < dist[sv] + d)
        continue;

      dist[gv] = dist[sv] + d;
      que.push(make_pair(dist[gv], gv));
    }
  }

  rep(i, q) cout << dist[x[i]] + dist[y[i]] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<string> vs;
typedef pair<ll, ll> P;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep1(i, n) for (ll i = 1; i <= (n); i++)
#define all(x) (x).begin(), (x).end()
vi dy = {0, 0, 1, -1};
vi dx = {1, -1, 0, 0};
constexpr auto INF = INT_MAX / 2;
constexpr auto MOD = 1000000007;

int main() {
  ll n;
  cin >> n;
  vector<vector<P>> path(n, vector<P>(0));

  rep(i, n - 1) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    path[a].push_back(make_pair(b, c));
    path[b].push_back(make_pair(a, c));
  }

  ll q, k;
  cin >> q >> k;
  k--;
  vector<ll> x(q), y(q);
  rep(i, q) {
    cin >> x[i] >> y[i];
    x[i]--;
    y[i]--;
  }

  vector<ll> dist(n, 5000000000000000);
  dist[k] = 0;

  priority_queue<P> que;
  que.push(make_pair(0, k));

  while (!que.empty()) {

    ll sv = que.top().second;
    que.pop();

    rep(i, path[sv].size()) {
      ll gv = path[sv][i].first;
      ll d = path[sv][i].second;

      if (dist[gv] < dist[sv] + d)
        continue;

      dist[gv] = dist[sv] + d;
      que.push(make_pair(dist[gv], gv));
    }
  }

  rep(i, q) cout << dist[x[i]] + dist[y[i]] << endl;
}
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

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

  vector<vector<pair<int, long long>>> adjList(N + 1);
  int a, b;
  long long weight;
  for (int i = 0; i < N - 1; ++i) {
    cin >> a >> b >> weight;
    adjList[a].push_back(make_pair(b, weight));
    adjList[b].push_back(make_pair(a, weight));
  }

  int Q, K;
  cin >> Q >> K;

  vector<long long> dist(N, -1);
  priority_queue<pair<int, long long>, vector<pair<int, long long>>,
                 greater<pair<int, long long>>>
      pq;
  dist[K] = 0;
  pq.push(make_pair(K, 0));
  while (!pq.empty()) {
    pair<int, long long> cur = pq.top();
    pq.pop();
    int x = cur.first;
    long long d = cur.second;

    if (d > dist[x])
      continue;
    for (vector<pair<int, long long>>::iterator it = adjList[x].begin();
         it != adjList[x].end(); ++it) {
      int nx = it->first;
      long long nd = d + it->second;
      if (dist[nx] != -1 && dist[nx] <= nd)
        continue;
      dist[nx] = nd;
      pq.push(make_pair(nx, nd));
    }
  }

  int from, to;
  vector<long long> ans;
  for (int i = 0; i < Q; ++i) {
    cin >> from >> to;
    ans.push_back(dist[from] + dist[to]);
  }

  for (int i = 0; i < Q; ++i) {
    cout << ans[i] << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

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

  vector<vector<pair<int, long long>>> adjList(N + 1);
  int a, b;
  long long weight;
  for (int i = 0; i < N - 1; ++i) {
    cin >> a >> b >> weight;
    adjList[a].push_back(make_pair(b, weight));
    adjList[b].push_back(make_pair(a, weight));
  }

  int Q, K;
  cin >> Q >> K;

  vector<long long> dist(N + 1, -1);
  priority_queue<pair<int, long long>, vector<pair<int, long long>>,
                 greater<pair<int, long long>>>
      pq;
  dist[K] = 0;
  pq.push(make_pair(K, 0));
  while (!pq.empty()) {
    pair<int, long long> cur = pq.top();
    pq.pop();
    int x = cur.first;
    long long d = cur.second;

    if (d > dist[x])
      continue;
    for (vector<pair<int, long long>>::iterator it = adjList[x].begin();
         it != adjList[x].end(); ++it) {
      int nx = it->first;
      long long nd = d + it->second;
      if (dist[nx] != -1 && dist[nx] <= nd)
        continue;
      dist[nx] = nd;
      pq.push(make_pair(nx, nd));
    }
  }

  int from, to;
  vector<long long> ans;
  for (int i = 0; i < Q; ++i) {
    cin >> from >> to;
    ans.push_back(dist[from] + dist[to]);
  }

  for (int i = 0; i < Q; ++i) {
    cout << ans[i] << endl;
  }
  return 0;
}