prithivMLmods/Coder-Stat huggingface dataset:
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
const int INF = 1001001001;
const int MOD = 1000000007;
typedef long long ll;

using P = pair<ll, ll>;
const int V = 10000;
vector<P> G[V]; // pair<辺の距離, 行き先の頂点> (隣接リスト)
ll dist[V];     // dist[i]はsから頂点iへの最短距離が入る
bool used[V];
void dijkstra(ll s) { // s: 始点
  fill_n(dist, V, 1000000000000000);
  fill_n(used, V, false);
  priority_queue<P, vector<P>, greater<P>>
      q; // 値が小さい順に取り出されるpriority_queue
  q.push(P(0, s));
  while (!q.empty()) {
    ll d, t; // d: sからの距離 t: 行き先
    tie(d, t) = q.top();
    q.pop();
    if (used[t])
      continue; // 既に探索済か
    used[t] = true;
    dist[t] = d;
    for (P e : G[t]) {
      if (dist[e.second] <= d + e.first)
        continue;
      q.push(P(d + e.first, e.second));
    }
  }
}

int main() {
  int N;
  cin >> N;
  N--;
  rep(i, N) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    G[a].push_back(make_pair(c, b));
    G[b].push_back(make_pair(c, a));
  }
  int Q, K;
  cin >> Q >> K;
  K--;
  vector<int> x(Q), y(Q);
  rep(i, Q) {
    cin >> x[i] >> y[i];
    x[i]--;
    y[i]--;
  }

  dijkstra(K);
  for (int i = 0; i < Q; i++) {
    cout << dist[x[i]] + dist[y[i]] << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
const int INF = 1001001001;
const int MOD = 1000000007;
typedef long long ll;

using P = pair<ll, ll>;
const int V = 100010;
vector<P> G[V]; // pair<辺の距離, 行き先の頂点> (隣接リスト)
ll dist[V];     // dist[i]はsから頂点iへの最短距離が入る
bool used[V];
void dijkstra(ll s) { // s: 始点
  fill_n(dist, V, 1000000000000000);
  fill_n(used, V, false);
  priority_queue<P, vector<P>, greater<P>>
      q; // 値が小さい順に取り出されるpriority_queue
  q.push(P(0, s));
  while (!q.empty()) {
    ll d, t; // d: sからの距離 t: 行き先
    tie(d, t) = q.top();
    q.pop();
    if (used[t])
      continue; // 既に探索済か
    used[t] = true;
    dist[t] = d;
    for (P e : G[t]) {
      if (dist[e.second] <= d + e.first)
        continue;
      q.push(P(d + e.first, e.second));
    }
  }
}

int main() {
  int N;
  cin >> N;
  N--;
  rep(i, N) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    G[a].push_back(make_pair(c, b));
    G[b].push_back(make_pair(c, a));
  }
  int Q, K;
  cin >> Q >> K;
  K--;
  vector<int> x(Q), y(Q);
  rep(i, Q) {
    cin >> x[i] >> y[i];
    x[i]--;
    y[i]--;
  }

  dijkstra(K);
  for (int i = 0; i < Q; i++) {
    cout << dist[x[i]] + dist[y[i]] << endl;
  }

  return 0;
}
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

const ll MOD = ll(1e9) + 7;
const ll INF = LLONG_MAX;
const ll MAX_N = 1 << 12;
const double EPS = 10e-8;
const double PI = 3.14159265359;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)

struct edge {
  ll to, cost;
};

ll n, dmin[10005], q, k;
vector<edge> G[100005];

ll dijkstra(ll s) {
  fill(dmin, dmin + n, INF);
  priority_queue<P, vector<P>, greater<P>> que;
  dmin[s] = 0;
  que.push({0, s});

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

int main() {
  cin >> n;
  REP(i, n - 1) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    G[a].push_back({b, c});
    G[b].push_back({a, c});
  }
  cin >> q >> k;
  k--;
  dijkstra(k);
  REP(i, q) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    cout << dmin[x] + dmin[y] << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

const ll MOD = ll(1e9) + 7;
const ll INF = LLONG_MAX;
const ll MAX_N = 1 << 12;
const double EPS = 10e-8;
const double PI = 3.14159265359;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)

struct edge {
  ll to, cost;
};

ll n, dmin[100005], q, k;
vector<edge> G[100005];

ll dijkstra(ll s) {
  fill(dmin, dmin + n, INF);
  priority_queue<P, vector<P>, greater<P>> que;
  dmin[s] = 0;
  que.push({0, s});

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

int main() {
  cin >> n;
  REP(i, n - 1) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    G[a].push_back({b, c});
    G[b].push_back({a, c});
  }
  cin >> q >> k;
  k--;
  dijkstra(k);
  REP(i, q) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    cout << dmin[x] + dmin[y] << endl;
  }
  return 0;
}
Problem p03634 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> d(100000, -1);

void dfs(ll a, vector<vector<pair<ll, ll>>> v) {
  for (int i = 0; i < v[a].size(); i++) {
    if (d[v[a][i].first] != -1)
      continue;
    d[v[a][i].first] = d[a] + v[a][i].second;
    dfs(v[a][i].first, v);
  }
}

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> d(100000, -1);

void dfs(ll a, vector<vector<pair<ll, ll>>> &v) {
  for (int i = 0; i < v[a].size(); i++) {
    if (d[v[a][i].first] != -1)
      continue;
    d[v[a][i].first] = d[a] + v[a][i].second;
    dfs(v[a][i].first, v);
  }
}

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define debug(x) cerr << "\n" << #x << ": " << x << "\n"
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define INF (1LL << 30)
#define int long long
#define pii pair<long long, long long>
#define MOD 1000000007
#define print(x) cout << (x) << endl
using namespace std;
typedef long long ll;

vector<vector<pii>> graph(100011);
vector<int> dst(100011, -1);
bool visited[10001] = {false};
void dfs(int k, int now, int distance) { // Kからの距離をアレする
  // dst[k] : 0に初期化する
  if (visited[now])
    return;
  visited[now] = true;
  for (auto z : graph[now]) {
    /*
    if (dst[z.first] >= 0){
    dst[z.first] = min(dst[z.first], (distance + z.second));
    } else dst[z.first] = distance + z.second;
    */
    if (dst[z.first] < 0)
      dst[z.first] = distance + z.second;
    dfs(k, z.first, dst[z.first]);
  }
}

int query(int x, int y) { return dst[x] + dst[y]; }

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

  rep(i, N - 1) {
    int a, b, c;
    cin >> a >> b >> c;
    --a;
    --b;
    graph[a].push_back(make_pair(b, c));
    graph[b].push_back(make_pair(a, c));
  }
  int Q, K;
  cin >> Q >> K;
  --K; // 0 index

  dfs(K, K, 0);

  /*
      cout<<"dst:"<<endl;
      rep(i,N){
          cout<< "i:"<<i<<"  : "<<dst[i]<<endl;
      }
  */
  int x, y;
  int ans;
  rep(i, Q) {
    cin >> x >> y;
    --x;
    --y;
    cout << query(x, y) << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define debug(x) cerr << "\n" << #x << ": " << x << "\n"
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define INF (1LL << 30)
#define int long long
#define pii pair<long long, long long>
#define MOD 1000000007
#define print(x) cout << (x) << endl
using namespace std;
typedef long long ll;

vector<vector<pii>> graph(100011);
vector<int> dst(100011, -1);
bool visited[100011] = {false};
void dfs(int k, int now, int distance) { // Kからの距離をアレする
  // dst[k] : 0に初期化する
  if (visited[now])
    return;
  visited[now] = true;
  for (auto z : graph[now]) {
    /*
    if (dst[z.first] >= 0){
    dst[z.first] = min(dst[z.first], (distance + z.second));
    } else dst[z.first] = distance + z.second;
    */
    if (dst[z.first] < 0)
      dst[z.first] = distance + z.second;
    dfs(k, z.first, dst[z.first]);
  }
}

int query(int x, int y) { return dst[x] + dst[y]; }

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

  rep(i, N - 1) {
    int a, b, c;
    cin >> a >> b >> c;
    --a;
    --b;
    graph[a].push_back(make_pair(b, c));
    graph[b].push_back(make_pair(a, c));
  }
  int Q, K;
  cin >> Q >> K;
  --K; // 0 index

  dfs(K, K, 0);

  /*
      cout<<"dst:"<<endl;
      rep(i,N){
          cout<< "i:"<<i<<"  : "<<dst[i]<<endl;
      }
  */
  int x, y;
  int ans;
  rep(i, Q) {
    cin >> x >> y;
    --x;
    --y;
    cout << query(x, y) << endl;
  }

  return 0;
}
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

const int Limit = 10010;
using edge = struct {
  int to;
  int64_t cost;
};
vector<edge> Tree[Limit]; // Vector<edge> の 配列
int64_t Depth[Limit];

// 深さ優先探索　再帰で全点の深さを得る
void dfs(int currentPoint, int parentPoint, int64_t d) {
  Depth[currentPoint] = d;
  for (auto &e : Tree[currentPoint]) {
    if (e.to == parentPoint) {
      continue;
    }
    dfs(e.to, currentPoint, d + e.cost);
  }
}

int main(int argc, char **argv) {
  (void)argc;
  (void)argv;

  // 頂点は 0-index とする
  int N;
  cin >> N;

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

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

  //	vector<int64_t> ans;

  dfs(K, -1, 0);
  for (int i = 0; i < Q; ++i) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    //		ans.push_back( Depth[x]+Depth[y] );
    cout << Depth[x] + Depth[y] << endl;
  }

  //	for(auto i : ans){
  //		cout << i << endl;
  //	}

  return 0;
}


Fixed submission:
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

const int Limit = 100010;
using edge = struct {
  int to;
  int64_t cost;
};
vector<edge> Tree[Limit]; // Vector<edge> の 配列
int64_t Depth[Limit];

// 深さ優先探索　再帰で全点の深さを得る
void dfs(int currentPoint, int parentPoint, int64_t d) {
  Depth[currentPoint] = d;
  for (auto &e : Tree[currentPoint]) {
    if (e.to == parentPoint) {
      continue;
    }
    dfs(e.to, currentPoint, d + e.cost);
  }
}

int main(int argc, char **argv) {
  (void)argc;
  (void)argv;

  // 頂点は 0-index とする
  int N;
  cin >> N;

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

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

  //	vector<int64_t> ans;

  dfs(K, -1, 0);
  for (int i = 0; i < Q; ++i) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    //		ans.push_back( Depth[x]+Depth[y] );
    cout << Depth[x] + Depth[y] << endl;
  }

  //	for(auto i : ans){
  //		cout << i << endl;
  //	}

  return 0;
}
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
const int MaxN = 50000;
typedef long long LL;

LL n, all, m;

int pre[MaxN + 5], last[MaxN + 5], other[MaxN + 5];
int seq[MaxN + 5];
LL dis[MaxN + 5], cost[MaxN + 5];
bool Inque[600005];

void Build(int x, int y, LL w) {
  pre[++all] = last[x];
  last[x] = all;
  other[all] = y;
  cost[all] = w;
}

void Bfs(int s) {
  int head = 1, tail = 1, now, ed, dr;
  seq[tail] = s;
  for (int i = 1; i <= n; i++) {
    if (i == s)
      dis[i] = 0;
    else
      dis[i] = (LL)1 << 60;
  }
  Inque[s] = 1;
  while (head <= tail) {
    now = seq[head];
    ed = last[now];
    while (ed != -1) {
      dr = other[ed];
      if (dis[now] + cost[ed] < dis[dr]) {
        dis[dr] = dis[now] + cost[ed];
        if (!Inque[dr]) {
          Inque[dr] = 1;
          seq[++tail] = dr;
        }
      }
      ed = pre[ed];
    }
    Inque[now] = 0;
    head++;
  }
}

int main() {
  all = -1;
  memset(last, -1, sizeof(last));
  scanf("%d", &n);
  for (int i = 1; i <= n - 1; i++) {
    int a, b;
    LL w;
    scanf("%d %d %lld", &a, &b, &w);
    Build(a, b, w);
    Build(b, a, w);
  }
  memset(seq, 0, sizeof(seq));
  memset(Inque, 0, sizeof(Inque));
  int a, b;
  scanf("%d %d", &a, &b);
  Bfs(b);
  for (int i = 1; i <= a; i++) {
    LL ans = 0;
    int x, y;
    scanf("%d %d", &x, &y);
    ans += dis[x];
    ans += dis[y];
    printf("%lld\n", ans);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
const int MaxN = 500000;
typedef long long LL;

LL n, all, m;

int pre[MaxN + 5], last[MaxN + 5], other[MaxN + 5];
int seq[MaxN + 5];
LL dis[MaxN + 5], cost[MaxN + 5];
bool Inque[600005];

void Build(int x, int y, LL w) {
  pre[++all] = last[x];
  last[x] = all;
  other[all] = y;
  cost[all] = w;
}

void Bfs(int s) {
  int head = 1, tail = 1, now, ed, dr;
  seq[tail] = s;
  for (int i = 1; i <= n; i++) {
    if (i == s)
      dis[i] = 0;
    else
      dis[i] = (LL)1 << 60;
  }
  Inque[s] = 1;
  while (head <= tail) {
    now = seq[head];
    ed = last[now];
    while (ed != -1) {
      dr = other[ed];
      if (dis[now] + cost[ed] < dis[dr]) {
        dis[dr] = dis[now] + cost[ed];
        if (!Inque[dr]) {
          Inque[dr] = 1;
          seq[++tail] = dr;
        }
      }
      ed = pre[ed];
    }
    Inque[now] = 0;
    head++;
  }
}

int main() {
  all = -1;
  memset(last, -1, sizeof(last));
  scanf("%d", &n);
  for (int i = 1; i <= n - 1; i++) {
    int a, b;
    LL w;
    scanf("%d %d %lld", &a, &b, &w);
    Build(a, b, w);
    Build(b, a, w);
  }
  memset(seq, 0, sizeof(seq));
  memset(Inque, 0, sizeof(Inque));
  int a, b;
  scanf("%d %d", &a, &b);
  Bfs(b);
  for (int i = 1; i <= a; i++) {
    LL ans = 0;
    int x, y;
    scanf("%d %d", &x, &y);
    ans += dis[x];
    ans += dis[y];
    printf("%lld\n", ans);
  }
  return 0;
}
Problem p03634 (C++)

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

struct Node {
  int to, cost;
};

void dfs(int v, int par, long long dist, vector<long long> &d,
         vector<vector<Node>> g) {
  d[v] = dist;
  for (int i = 0; i < g[v].size(); i++) {
    if (g[v][i].to == par)
      continue;
    dfs(g[v][i].to, v, dist + g[v][i].cost, d, g);
  }
}
int main() {
  int N;
  cin >> N;

  vector<vector<Node>> g(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<long long> cost(N);
  dfs(K, -1, 0, cost, g);

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

  return 0;
}


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

struct Node {
  int to, cost;
};

void dfs(int v, int par, long long dist, vector<long long> &d,
         const vector<vector<Node>> &g) {
  d[v] = dist;
  for (int i = 0; i < g[v].size(); i++) {
    if (g[v][i].to == par)
      continue;
    dfs(g[v][i].to, v, dist + g[v][i].cost, d, g);
  }
}
int main() {
  int N;
  cin >> N;

  vector<vector<Node>> g(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<long long> cost(N);
  dfs(K, -1, 0, cost, g);

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

  return 0;
}
Problem p03634 (C++)

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

int n;
int MAXIMUM = 100005;
long INF = 1e15;
// W:未訪問
// G:訪問する候補
// B:訪問済み
int W = 0, G = 1, B = 2;
vector<pair<int, int>> adj[100005];
long d[100005];
int dijkstra(int K) {
  priority_queue<pair<int, int>> PQ;
  int u, minv;
  int color[MAXIMUM];
  // d[n]:Tに属する頂点と V - T
  // に属する頂点をつなぐ辺の中で重みが最小の辺の重みを記録

  // 初期化
  for (int i = 0; i < n; i++) {
    d[i] = INF;
    color[i] = W;
  }

  d[K] = 0;
  PQ.push(make_pair(0, K));
  color[K] = G;

  while (!PQ.empty()) {
    pair<int, int> f = PQ.top();
    PQ.pop();
    u = f.second;
    // 頂点を訪問済に
    color[u] = B;
    // 最小値を取り出し、それが最短でなければ無視
    if (d[u] < f.first * (-1))
      continue;

    // 訪問済みでないかつ辺がつながっている頂点の中でコストが最小の辺を選択
    for (int j = 0; j < adj[u].size(); j++) {
      int v = adj[u][j].first;
      if (color[v] == B)
        continue;
      if (d[v] > d[u] + adj[u][j].second) {
        d[v] = d[u] + adj[u][j].second;
        PQ.push(make_pair(d[v] * (-1), v));
        color[v] = G;
      }
    }
  }
}

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

  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;
  }
}


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

int n;
int MAXIMUM = 100005;
long INF = 1e15;
// W:未訪問
// G:訪問する候補
// B:訪問済み
int W = 0, G = 1, B = 2;
vector<pair<int, int>> adj[100005];
long d[100005];
void dijkstra(int K) {
  priority_queue<pair<int, int>> PQ;
  int u, minv;
  int color[MAXIMUM];
  // d[n]:Tに属する頂点と V - T
  // に属する頂点をつなぐ辺の中で重みが最小の辺の重みを記録

  // 初期化
  for (int i = 0; i < n; i++) {
    d[i] = INF;
    color[i] = W;
  }

  d[K] = 0;
  PQ.push(make_pair(0, K));
  color[K] = G;

  while (!PQ.empty()) {
    pair<int, int> f = PQ.top();
    PQ.pop();
    u = f.second;
    // 頂点を訪問済に
    color[u] = B;
    // 最小値を取り出し、それが最短でなければ無視
    if (d[u] < f.first * (-1))
      continue;

    // 訪問済みでないかつ辺がつながっている頂点の中でコストが最小の辺を選択
    for (int j = 0; j < adj[u].size(); j++) {
      int v = adj[u][j].first;
      if (color[v] == B)
        continue;
      if (d[v] > d[u] + adj[u][j].second) {
        d[v] = d[u] + adj[u][j].second;
        PQ.push(make_pair(d[v] * (-1), v));
        color[v] = G;
      }
    }
  }
}

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

  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;
  }
}
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define ll long long int
#define rep(i, x, y) for (int i = x; i < y; i++)
#define rel(i, x, y) for (int i = x - 1; i >= y; i--)
#define all(x) x.begin(), x.end()
#define NMAX 100000
using namespace std;

ll ranges[NMAX] = {};
vector<pair<int, ll>> graph[NMAX]; // first is ngh. second is ranges.
bool used[NMAX] = {};

void dfs(int v, ll range) {
  ranges[v] = range;
  used[v] = true;
  rep(i, 0, graph[v].size()) {
    if (!used[graph[v][i].first]) {
      dfs(graph[v][i].first, range + graph[v][i].second);
    }
  }
}

int main() {
  int N;
  cin >> N;
  int tmp1, tmp2, tmp3;
  rep(i, 0, N - 1) {
    cin >> tmp1 >> tmp2 >> tmp3;
    graph[tmp1].push_back(make_pair(tmp2, tmp3));
    graph[tmp2].push_back(make_pair(tmp1, tmp3));
  }
  int Q, K;
  int x, y;
  cin >> Q >> K;
  dfs(K, 0);
  rep(i, 0, Q) {
    cin >> x >> y;
    cout << ranges[x] + ranges[y] << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define ll long long int
#define rep(i, x, y) for (int i = x; i < y; i++)
#define rel(i, x, y) for (int i = x - 1; i >= y; i--)
#define all(x) x.begin(), x.end()
#define NMAX 100005
using namespace std;

ll ranges[NMAX] = {};
vector<pair<int, ll>> graph[NMAX]; // first is ngh. second is ranges.
bool used[NMAX] = {};

void dfs(int v, ll range) {
  ranges[v] = range;
  used[v] = true;
  rep(i, 0, graph[v].size()) {
    if (!used[graph[v][i].first]) {
      dfs(graph[v][i].first, range + graph[v][i].second);
    }
  }
}

int main() {
  int N;
  cin >> N;
  int tmp1, tmp2, tmp3;
  rep(i, 0, N - 1) {
    cin >> tmp1 >> tmp2 >> tmp3;
    graph[tmp1].push_back(make_pair(tmp2, tmp3));
    graph[tmp2].push_back(make_pair(tmp1, tmp3));
  }
  int Q, K;
  int x, y;
  cin >> Q >> K;
  dfs(K, 0);
  rep(i, 0, Q) {
    cin >> x >> y;
    cout << ranges[x] + ranges[y] << endl;
  }
}
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define MAX_N 10005
#define pii pair<int, int>
#define ll long long
#define FREI freopen("in.txt", "r", stdin)
#define FREO freopen("out.txt", "w", stdout)
using namespace std;
int n, m, src;
vector<pair<int, int>> g[MAX_N];
ll dis[MAX_N];
bool vis[MAX_N];
void dfs(int u) {
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i].first;
    if (vis[v] == 0) {
      vis[v] = 1;
      dis[v] = min(dis[v], dis[u] + g[u][i].second);
      dfs(v);
    }
  }
}
int main() {
  // FREI;
  // std::ios::sync_with_stdio(false);
  cin >> n;
  int u, v, cost;
  for (int i = 0; i < n - 1; i++) {
    cin >> u >> v >> cost;
    g[u].push_back(pii(v, cost));
    g[v].push_back(pii(u, cost));
  }
  for (int i = 0; i <= n; i++)
    dis[i] = 9223372036854775807;
  memset(vis, 0, sizeof(vis));
  int q, k;
  cin >> q >> k;
  src = k;
  vis[k] = 1;
  dis[k] = 0;
  dfs(k);
  for (int i = 0; i < q; i++) {
    cin >> u >> v;
    cout << dis[u] + dis[v] << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define MAX_N 100005
#define pii pair<int, int>
#define ll long long
#define FREI freopen("in.txt", "r", stdin)
#define FREO freopen("out.txt", "w", stdout)
using namespace std;
int n, m, src;
vector<pair<int, int>> g[MAX_N];
ll dis[MAX_N];
bool vis[MAX_N];
void dfs(int u) {
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i].first;
    if (vis[v] == 0) {
      vis[v] = 1;
      dis[v] = min(dis[v], dis[u] + g[u][i].second);
      dfs(v);
    }
  }
}
int main() {
  // FREI;
  // std::ios::sync_with_stdio(false);
  cin >> n;
  int u, v, cost;
  for (int i = 0; i < n - 1; i++) {
    cin >> u >> v >> cost;
    g[u].push_back(pii(v, cost));
    g[v].push_back(pii(u, cost));
  }
  for (int i = 0; i <= n; i++)
    dis[i] = 9223372036854775807;
  memset(vis, 0, sizeof(vis));
  int q, k;
  cin >> q >> k;
  src = k;
  vis[k] = 1;
  dis[k] = 0;
  dfs(k);
  for (int i = 0; i < q; i++) {
    cin >> u >> v;
    cout << dis[u] + dis[v] << endl;
  }
  return 0;
}
Problem p03634 (C++)

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

using u64 = unsigned long long;

int n, q, k;
std::vector<std::array<int, 2>> tree[100000];
u64 cost[100000];

void dfs(int v) {
  for (auto &i : tree[v]) {
    if (i[0] != k && !cost[i[0]]) {
      cost[i[0]] = cost[v] + i[1];
      dfs(i[0]);
    }
  }
}

int main() {
  std::cin >> n;
  int a, b, c;
  for (int i = 0; i < n - 1; i++) {
    std::cin >> a >> b >> c;
    tree[a].push_back({b, c});
    tree[b].push_back({a, c});
  }
  std::cin >> q >> k;
  cost[k] = 0;
  dfs(k);
  int x, y;
  for (int i = 0; i < q; i++) {
    std::cin >> x >> y;
    std::cout << cost[x] + cost[y] << std::endl;
  }
  return 0;
}


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

using u64 = unsigned long long;

int n, q, k;
std::vector<std::array<int, 2>> tree[100010];
u64 cost[100010];

void dfs(int v) {
  for (auto &i : tree[v]) {
    if (i[0] != k && !cost[i[0]]) {
      cost[i[0]] = cost[v] + i[1];
      dfs(i[0]);
    }
  }
}

int main() {
  std::cin >> n;
  int a, b, c;
  for (int i = 0; i < n - 1; i++) {
    std::cin >> a >> b >> c;
    tree[a].push_back({b, c});
    tree[b].push_back({a, c});
  }
  std::cin >> q >> k;
  cost[k] = 0;
  dfs(k);
  int x, y;
  for (int i = 0; i < q; i++) {
    std::cin >> x >> y;
    std::cout << cost[x] + cost[y] << std::endl;
  }
  return 0;
}
Problem p03634 (C++)

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
#define int ll

int n, q, k;
vector<vector<pii>> G;
int dis[10010];

void dfs(int v, int p, int d) {
  dis[v] = d;
  for (auto nv : G[v]) {
    if (nv.first == p)
      continue;

    dfs(nv.first, v, d + nv.second);
  }
}

signed main() {
  cin >> n;
  G.resize(n);
  int a, b, c;
  for (int i = 0; i < n - 1; ++i) {
    cin >> a >> b >> c;
    a--;
    b--;
    G[a].push_back(pii(b, c));
    G[b].push_back(pii(a, c));
  }
  cin >> q >> k;
  k--;
  dfs(k, -1, 0);

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

  for (int i = 0; i < q; ++i) {
    cout << dis[x[i]] + dis[y[i]] << endl;
  }
}

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
#define int ll

int n, q, k;
vector<vector<pll>> G;
int dis[100010];

void dfs(int v, int p, int d) {
  dis[v] = d;
  for (auto nv : G[v]) {
    if (nv.first == p)
      continue;

    dfs(nv.first, v, d + nv.second);
  }
}

signed main() {
  cin >> n;
  G.resize(n);
  int a, b, c;
  for (int i = 0; i < n - 1; ++i) {
    cin >> a >> b >> c;
    a--;
    b--;
    G[a].push_back(pii(b, c));
    G[b].push_back(pii(a, c));
  }
  cin >> q >> k;
  k--;
  dfs(k, -1, 0);

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

  for (int i = 0; i < q; ++i) {
    cout << dis[x[i]] + dis[y[i]] << endl;
  }
}
Problem p03634 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPS(i, n) for (int i = 1, i##_len = (n); i < i##_len; ++i)
#define ALL(x) (x).begin(), (x).end()
#define PRINT(A) std::cout << (#A) << ":" << (A) << std::endl;

typedef long long int ll;
const int MOD = 1e9 + 7;
const int INF = 1e9;

using namespace std;
using Edge = pair<int, long long>;
using Graph = vector<vector<Edge>>;

void dfs(int n, ll dist, int p, vector<ll> &d, Graph g) {
  for (auto e : g.at(n)) {
    if (e.first == p)
      continue;
    d.at(e.first) = dist + e.second;
    dfs(e.first, d.at(e.first), n, d, g);
  }
}

int main() {
  int N, Q, K;
  cin >> N;

  vector<ll> d = vector<ll>(100010);
  Graph g = Graph(100010);
  int a, b, c;
  REP(i, N - 1) {
    cin >> a >> b >> c;
    a--;
    b--;
    g.at(a).push_back({b, c});
    g.at(b).push_back({a, c});
  }

  cin >> Q >> K;
  K--;
  dfs(K, 0, -1, d, g);

  int x, y;
  REP(i, Q) {
    cin >> x >> y;
    x--;
    y--;
    ll ans = d.at(x) + d.at(y);
    cout << ans << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPS(i, n) for (int i = 1, i##_len = (n); i < i##_len; ++i)
#define ALL(x) (x).begin(), (x).end()
#define PRINT(A) std::cout << (#A) << ":" << (A) << std::endl;

typedef long long int ll;
const int MOD = 1e9 + 7;
const int INF = 1e9;

using namespace std;
using Edge = pair<int, long long>;
using Graph = vector<vector<Edge>>;

void dfs(int n, ll dist, int p, vector<ll> &d, Graph &g) {
  for (auto e : g.at(n)) {
    if (e.first == p)
      continue;
    d.at(e.first) = dist + e.second;
    dfs(e.first, d.at(e.first), n, d, g);
  }
}

int main() {
  int N, Q, K;
  cin >> N;

  vector<ll> d = vector<ll>(100010);
  Graph g = Graph(100010);
  int a, b, c;
  REP(i, N - 1) {
    cin >> a >> b >> c;
    a--;
    b--;
    g.at(a).push_back({b, c});
    g.at(b).push_back({a, c});
  }

  cin >> Q >> K;
  K--;
  dfs(K, 0, -1, d, g);

  int x, y;
  REP(i, Q) {
    cin >> x >> y;
    x--;
    y--;
    ll ans = d.at(x) + d.at(y);
    cout << ans << endl;
  }
}
Problem p03634 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
using namespace std;

#define MOD 1000000007

using ll = long long;
const int limit = 100010;
using edge = struct {
  int to;
  ll cost;
};

ll depth[limit];
vector<edge> tree[limit];

void dfs(int v, int p, ll d) {
  depth[v] = d;
  for (auto &e : tree[v]) {
    if (e.to == p) {
      continue;
    } else {
      dfs(e.to, v, d + e.cost);
    }
  }
}
int main(void) {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    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);
  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    cout << depth[x] + depth[y] << endl;
  }
  return 0;
}


Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
using namespace std;

#define MOD 1000000007

using ll = long long;
const int limit = 100010;
using edge = struct {
  int to;
  ll cost;
};

ll depth[limit];
vector<edge> tree[limit];

void dfs(int v, int p, ll d) {
  depth[v] = d;
  for (auto &e : tree[v]) {
    if (e.to == p) {
      continue;
    } else {
      dfs(e.to, v, d + e.cost);
    }
  }
}
int main(void) {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    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);
  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    cout << depth[x] + depth[y] << endl;
  }
  return 0;
}
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e16;
const ll mod = 1000000007;
#define rep(i, n) for (int i = 0; i < (ll)(n); i++)

int main() {
  ll n;
  cin >> n;
  vector<vector<pair<ll, ll>>> v(n, vector<pair<ll, ll>>());
  rep(i, n) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    v.at(a).push_back(make_pair(b, c));
    v.at(b).push_back(make_pair(a, c));
  }
  ll q, k;
  cin >> q >> k;
  k--;
  vector<ll> d(n, INF);
  stack<ll> s;
  d.at(k) = 0;
  s.push(k);
  while (s.size()) {
    ll p = s.top();
    s.pop();
    rep(i, v.at(p).size()) {
      ll next = v.at(p).at(i).first, dis = v.at(p).at(i).second;
      if (d.at(next) > d.at(p) + dis) {
        d.at(next) = d.at(p) + dis;
        s.push(next);
      }
    }
  }
  rep(i, q) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    cout << d.at(x) + d.at(y) << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e16;
const ll mod = 1000000007;
#define rep(i, n) for (int i = 0; i < (ll)(n); i++)

int main() {
  ll n;
  cin >> n;
  vector<vector<pair<ll, ll>>> v(n, vector<pair<ll, ll>>());
  rep(i, n - 1) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    v.at(a).push_back(make_pair(b, c));
    v.at(b).push_back(make_pair(a, c));
  }
  ll q, k;
  cin >> q >> k;
  k--;
  vector<ll> d(n, INF);
  stack<ll> s;
  d.at(k) = 0;
  s.push(k);
  while (s.size()) {
    ll p = s.top();
    s.pop();
    rep(i, v.at(p).size()) {
      ll next = v.at(p).at(i).first, dis = v.at(p).at(i).second;
      if (d.at(next) > d.at(p) + dis) {
        d.at(next) = d.at(p) + dis;
        s.push(next);
      }
    }
  }
  rep(i, q) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    cout << d.at(x) + d.at(y) << endl;
  }
}
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep0(i, n) for (ll(i) = 0; (i) < (n); ++(i))
#define rrep0(i, n) for (ll(i) = (n)-1; (i) >= 0; --(i))
#define rep1(i, n) for (ll(i) = 1; (i) <= (n); ++(i))
#define rrep1(i, n) for (ll(i) = (n); (i) >= 1; --(i))
#define nfor(i, a, b) for (ll(i) = (a); (i) < (b); ++(i))
#define rfor(i, a, b) for (ll(i) = (a); (i) > (b); --(i))
#define ll long long
#define mod (1000000007)
#define pf(x) cout << (x) << endl;
#define all(x) (x).begin(), (x).end()

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}

ll factorial(ll n) {
  ll t = 1;
  rep1(i, n) t *= i;
  return t;
}

ll combinations(ll n, ll k) {
  ll r = 1;
  for (ll i = 1; i <= k; ++i) {
    r *= n--;
    r /= i;
  }
  return r;
}

ll n, q, k;
vector<queue<pair<ll, ll>>> v(1e5);
vector<ll> cost(1e5, 0);

void d(ll p, ll now) {
  if (p != 0)
    cost[now] = cost[p] + v[p].front().second;
  while (!v[now].empty()) {
    if (v[now].front().first != p)
      d(now, v[now].front().first);
    v[now].pop();
  }
  return;
}

int main() {
  ll n, q, k;
  cin >> n;
  rep0(i, n - 1) {
    ll a, b, c;
    cin >> a >> b >> c;
    v[a].push(make_pair(b, c));
    v[b].push(make_pair(a, c));
  }
  cin >> q >> k;
  d(0, k);
  rep0(i, q) {
    ll x, y;
    cin >> x >> y;
    pf(cost[x] + cost[y]);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep0(i, n) for (ll(i) = 0; (i) < (n); ++(i))
#define rrep0(i, n) for (ll(i) = (n)-1; (i) >= 0; --(i))
#define rep1(i, n) for (ll(i) = 1; (i) <= (n); ++(i))
#define rrep1(i, n) for (ll(i) = (n); (i) >= 1; --(i))
#define nfor(i, a, b) for (ll(i) = (a); (i) < (b); ++(i))
#define rfor(i, a, b) for (ll(i) = (a); (i) > (b); --(i))
#define ll long long
#define mod (1000000007)
#define pf(x) cout << (x) << endl;
#define all(x) (x).begin(), (x).end()

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}

ll factorial(ll n) {
  ll t = 1;
  rep1(i, n) t *= i;
  return t;
}

ll combinations(ll n, ll k) {
  ll r = 1;
  for (ll i = 1; i <= k; ++i) {
    r *= n--;
    r /= i;
  }
  return r;
}

ll n, q, k;
vector<queue<pair<ll, ll>>> v(1e5 + 2);
vector<ll> cost(1e5 + 2, 0);

void d(ll p, ll now) {
  if (p != 0)
    cost[now] = cost[p] + v[p].front().second;
  while (!v[now].empty()) {
    if (v[now].front().first != p)
      d(now, v[now].front().first);
    v[now].pop();
  }
  return;
}

int main() {
  ll n, q, k;
  cin >> n;
  rep0(i, n - 1) {
    ll a, b, c;
    cin >> a >> b >> c;
    v[a].push(make_pair(b, c));
    v[b].push(make_pair(a, c));
  }
  cin >> q >> k;
  d(0, k);
  rep0(i, q) {
    ll x, y;
    cin >> x >> y;
    pf(cost[x] + cost[y]);
  }
  return 0;
}
Problem p03634 (C++)

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

using ll = long long;

using VI = vector<int>;
using VL = vector<ll>;
using VVL = vector<vector<ll>>;
using VC = vector<char>;
using PI = pair<int, int>;

#define FOR(i, s, n) for (int i = s; i < (n); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(x) x.begin(), x.end()

const long long INF = 1LL << 60;
const int MOD = 1000000007;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

using Edge = pair<ll, ll>;
using Graph = vector<vector<Edge>>;

vector<VVL> v;
ll N, Q, K;
VL dp;

Graph G;

ll x, y;

ll rec(ll cur, ll cost, ll past) {
  dp[cur] = min(dp[cur], cost);

  for (auto e : G[cur]) {
    if (e.first == past)
      continue;
    rec(e.first, cost + e.second, cur);
  }
}

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

  cin >> N;
  G.resize(N);
  dp.assign(N + 1, INF);

  REP(i, N - 1) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    G[a].push_back({b, c});
    G[b].push_back({a, c});
  }

  cin >> Q >> K;
  K--;

  rec(K, 0, -1);

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

  return 0;
}

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

using ll = long long;

using VI = vector<int>;
using VL = vector<ll>;
using VVL = vector<vector<ll>>;
using VC = vector<char>;
using PI = pair<int, int>;

#define FOR(i, s, n) for (int i = s; i < (n); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(x) x.begin(), x.end()

const long long INF = 1LL << 60;
const int MOD = 1000000007;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

using Edge = pair<ll, ll>;
using Graph = vector<vector<Edge>>;

vector<VVL> v;
ll N, Q, K;
VL dp;

Graph G;

ll x, y;

void rec(ll cur, ll cost, ll past) {
  dp[cur] = min(dp[cur], cost);

  for (auto e : G[cur]) {
    if (e.first == past)
      continue;
    rec(e.first, cost + e.second, cur);
  }
}

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

  cin >> N;
  G.resize(N);
  dp.assign(N + 1, INF);

  REP(i, N - 1) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    G[a].push_back({b, c});
    G[b].push_back({a, c});
  }

  cin >> Q >> K;
  K--;

  rec(K, 0, -1);

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

  return 0;
}
Problem p03634 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, f, n) for (ll i = (f); (i) < (n); i++)
#define repe(i, f, n) for (ll i = (f); (i) <= (n); i++)
using namespace std;
using ll = long long;

ll INF = 1LL << 60;
using G = vector<map<int, int>>;

void dfs(int node, ll count, G &arr, vector<bool> seen, vector<long> &to_K) {
  seen[node] = true;
  for (auto child : arr[node]) {
    if (seen[child.first])
      continue;
    to_K[child.first] = count + child.second;
    dfs(child.first, count + child.second, arr, seen, to_K);
  }
}

int main() {
  int N;
  cin >> N;
  G arr(N + 1);
  rep(i, 0, N - 1) {
    int a, b, c;
    cin >> a >> b >> c;
    arr[a][b] = c;
    arr[b][a] = c;
  }
  vector<long> to_K(N + 1);

  int Q, K;
  cin >> Q >> K;
  vector<pair<int, int>> q(Q);
  rep(i, 0, Q) {
    cin >> q[i].first;
    cin >> q[i].second;
  }

  vector<bool> seen(N + 1, false);
  dfs(K, 0, arr, seen, to_K);

  rep(i, 0, Q) { cout << to_K[q[i].first] + to_K[q[i].second] << endl; }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, f, n) for (ll i = (f); (i) < (n); i++)
#define repe(i, f, n) for (ll i = (f); (i) <= (n); i++)
using namespace std;
using ll = long long;

ll INF = 1LL << 60;
using G = vector<map<int, int>>;

void dfs(int node, ll count, G &arr, vector<bool> seen, vector<long> &to_K) {
  seen[node] = true;
  for (auto child : arr[node]) {
    if (seen[child.first])
      continue;
    to_K[child.first] = count + child.second;
    dfs(child.first, count + child.second, arr, seen, to_K);
  }
}

int main() {
  int N;
  cin >> N;
  G arr(N + 1);
  rep(i, 0, N - 1) {
    int a, b, c;
    cin >> a >> b >> c;
    arr[a][b] = c;
    arr[b][a] = c;
  }
  vector<long> to_K(N + 1);

  int Q, K;
  cin >> Q >> K;
  vector<pair<int, int>> q(Q);
  rep(i, 0, Q) {
    cin >> q[i].first;
    cin >> q[i].second;
  }

  vector<bool> seen(N + 1, false);
  // dfs(K, 0, arr, seen, to_K);

  queue<int> que;
  que.push(K);
  seen[K] = true;
  to_K[K] = 0;
  while (!que.empty()) {
    int node = que.front();
    que.pop();
    for (auto child : arr[node]) {
      if (seen[child.first])
        continue;
      to_K[child.first] = arr[node][child.first] + to_K[node];
      seen[child.first] = true;
      que.push(child.first);
    }
  }

  rep(i, 0, Q) { cout << to_K[q[i].first] + to_K[q[i].second] << endl; }
}
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
#define siz 100005
const int maxn = 10000;
typedef long long LL;
using namespace std;
struct Edge {
  int to, w, next;
} edges[maxn * 3];
int n, tot, q, k;
int head[maxn + 5];
LL d[maxn + 5];
void Addedge(int x, int y, int w) {
  edges[++tot].to = y, edges[tot].w = w;
  edges[tot].next = head[x], head[x] = tot;
}
void dfs(int x, int fa) {
  for (int i = head[x]; i != -1; i = edges[i].next) {
    int to = edges[i].to;
    if (to == fa)
      continue;
    d[to] = d[x] + edges[i].w * 1ll;
    dfs(to, x);
  }
}
void Init() {
  d[k] = 0;
  dfs(k, -1);
}
int main() {
  int x, y, w;
  while (~scanf("%d", &n)) {
    tot = 0;
    for (int i = 1; i <= n; i++)
      head[i] = -1;
    for (int i = 1; i < n; i++) {
      scanf("%d %d %d", &x, &y, &w);
      Addedge(x, y, w);
      Addedge(y, x, w);
    }
    scanf("%d %d", &q, &k);
    Init();
    for (int i = 1; i <= q; i++) {
      scanf("%d %d", &x, &y);
      LL ans = d[x] + d[y];
      cout << ans << endl;
    }
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
#define siz 100005
const int maxn = 100000;
typedef long long LL;
using namespace std;
struct Edge {
  int to, w, next;
} edges[maxn * 3];
int n, tot, q, k;
int head[maxn + 5];
LL d[maxn + 5];
void Addedge(int x, int y, int w) {
  edges[++tot].to = y, edges[tot].w = w;
  edges[tot].next = head[x], head[x] = tot;
}
void dfs(int x, int fa) {
  for (int i = head[x]; i != -1; i = edges[i].next) {
    int to = edges[i].to;
    if (to == fa)
      continue;
    d[to] = d[x] + edges[i].w * 1ll;
    dfs(to, x);
  }
}
void Init() {
  d[k] = 0;
  dfs(k, -1);
}
int main() {
  int x, y, w;
  while (~scanf("%d", &n)) {
    tot = 0;
    for (int i = 1; i <= n; i++)
      head[i] = -1;
    for (int i = 1; i < n; i++) {
      scanf("%d %d %d", &x, &y, &w);
      Addedge(x, y, w);
      Addedge(y, x, w);
    }
    scanf("%d %d", &q, &k);
    Init();
    for (int i = 1; i <= q; i++) {
      scanf("%d %d", &x, &y);
      LL ans = d[x] + d[y];
      cout << ans << endl;
    }
  }
  return 0;
}
Problem p03634 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
G = [[] for i in range(N)]
for i in range(N - 1):
    a, b, c = map(int, input().split())
    a, b = a - 1, b - 1
    G[a].append([b, c])
    G[b].append([a, c])

Q, K = map(int, input().split())
K -= 1
dist = [-1] * N
dist[K] = 0


def dfs(n):
    for v, c in G[n]:
        if dist[v] < 0:
            dist[v] = dist[n] + c
            dfs(v)


dfs(K)
for q in range(Q):
    x, y = map(int, input().split())
    x, y = x - 1, y - 1
    print(dist[x] + dist[y])


Fixed submission:
import sys

sys.setrecursionlimit(10**9)
N = int(input())
G = [[] for i in range(N)]
for i in range(N - 1):
    a, b, c = map(int, input().split())
    a, b = a - 1, b - 1
    G[a].append([b, c])
    G[b].append([a, c])

Q, K = map(int, input().split())
K -= 1
dist = [-1] * N
dist[K] = 0


def dfs(n):
    for v, c in G[n]:
        if dist[v] < 0:
            dist[v] = dist[n] + c
            dfs(v)


dfs(K)
for q in range(Q):
    x, y = map(int, input().split())
    x, y = x - 1, y - 1
    print(dist[x] + dist[y])
Problem p03634 (C++)

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

#define OUT(x) cerr << #x << " = " << x << endl;
#define all(x) x.begin(), x.end()
#define mp make_pair
#define pii pair<int, int>
#define piii pair<int, pair<int, int>>

struct edge {
  int to, cost;
};

vector<edge> g[101010];
long long depth[101010];

void dfs(int v, int prev, long long d) {
  depth[v] = d;
  for (auto u : g[v])
    if (u.to != prev) {
      dfs(u.to, v, d + u.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--;
    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 < n; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    cout << depth[x] + depth[y] << endl;
  }
  return 0;
}


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

#define OUT(x) cerr << #x << " = " << x << endl;
#define all(x) x.begin(), x.end()
#define mp make_pair
#define pii pair<int, int>
#define piii pair<int, pair<int, int>>

struct edge {
  int to, cost;
};

vector<edge> g[101010];
long long depth[101010];

void dfs(int v, int prev, long long d) {
  depth[v] = d;
  for (auto u : g[v])
    if (u.to != prev) {
      dfs(u.to, v, d + u.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--;
    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 << depth[x] + depth[y] << endl;
  }
  return 0;
}