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

Buggy submission (status: Runtime Error):
/***  بسم الله الرحمن ا لرحيم   ***/
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
// #include<unordered_map>
#define endl '\n'
#define Ceil(x, y) ((x + y - 1) / y)
#define sz(s) (int)s.size()
#define RT(x) return cout << x, 0;
#define mem(x, y) memset(x, y, sizeof(x))
#define all(v) ((v).begin()), ((v).end())
#define watch(x) cout << (#x) << " = " << x << endl
#define test                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
const double PI = acos(-1), EPS = 1e-7;
const int OO = 0x3f3f3f3f, N = 2e5 + 5, mod = 1e9 + 7;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ull gcd(ull x, ull y) { return (!y) ? x : gcd(y, x % y); }
ull lcm(ull x, ull y) { return ((x / gcd(x, y)) * y); }
void file() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
#endif !ONLINEJUDGE
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

int main() {
  file();
  int n;
  cin >> n;
  ull x, g;
  g = 1;
  while (n--) {
    cin >> x;
    g = lcm(x, g);
  }
  RT(g);
}

Fixed submission:
/***  بسم الله الرحمن ا لرحيم   ***/
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
// #include<unordered_map>
#define endl '\n'
#define Ceil(x, y) ((x + y - 1) / y)
#define sz(s) (int)s.size()
#define RT(x) return cout << x, 0;
#define mem(x, y) memset(x, y, sizeof(x))
#define all(v) ((v).begin()), ((v).end())
#define watch(x) cout << (#x) << " = " << x << endl
#define test                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
const double PI = acos(-1), EPS = 1e-7;
const int OO = 0x3f3f3f3f, N = 2e5 + 5, mod = 1e9 + 7;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ull gcd(ull x, ull y) { return (!y) ? x : gcd(y, x % y); }
ull lcm(ull x, ull y) { return ((x / gcd(x, y)) * y); }
void file() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
#endif !ONLINEJUDGE
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

int main() {
  // file();
  int n;
  cin >> n;
  ull x, g;
  g = 1;
  while (n--) {
    cin >> x;
    g = lcm(x, g);
  }
  RT(g);
}
Problem p03633 (C++)

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

typedef unsigned long long ll;
const int N_MAX = 100;
ll T[N_MAX];

ll gcd(ll a, ll b) {
  ll tmp;
  if (a < b) {
    tmp = a;
    a = b;
    b = tmp;
  }
  while (a % b != 0) {
    tmp = a;
    a = b;
    b = tmp % b;
  }
  return b;
}

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

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> T[i];
  }
  ll l = lcd(T[1], T[0]);
  for (int i = 2; i < N; i++) {
    l = lcd(T[i], l);
  }
  cout << l << endl;
}

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

typedef unsigned long long ll;
const int N_MAX = 100;
ll T[N_MAX];

ll gcd(ll a, ll b) {
  ll tmp;
  if (a < b) {
    tmp = a;
    a = b;
    b = tmp;
  }
  while (a % b != 0) {
    tmp = a;
    a = b;
    b = tmp % b;
  }
  return b;
}

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

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> T[i];
  }
  ll l;
  if (N == 1) {
    l = T[0];
  } else {
    l = lcd(T[1], T[0]);
    for (int i = 2; i < N; i++) {
      l = lcd(T[i], l);
    }
  }
  cout << l << endl;
}
Problem p03633 (C++)

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

using namespace std;

using ll = long long int;
using ld = long double;

using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vd = vector<double>;
using vvd = vector<vd>;
using vs = vector<string>;
using vb = vector<bool>;
using vvb = vector<vb>;

const int INF = (1 << 30) - 1;
const ll INF64 = ((ll)1 << 62) - 1;
const double PI = 3.1415926535897932384626433832795;

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

int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
int lcm(int x, int y) { return x / gcd(x, y) * y; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

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

  ll n;
  cin >> n;

  vl t(n);
  for (int i = 0; i < n; i++) {
    cin >> t[i];
  }

  if (t.size() == 1) {
    cout << t[0] << endl;
    return 0;
  }

  ll res = 0;
  do {
    res = max(res, lcm(t[0], t[1]));
  } while (next_permutation(t.begin(), t.end()));

  cout << res << endl;
  return 0;
}

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

using namespace std;

using ll = long long int;
using ld = long double;

using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vd = vector<double>;
using vvd = vector<vd>;
using vs = vector<string>;
using vb = vector<bool>;
using vvb = vector<vb>;

const int INF = (1 << 30) - 1;
const ll INF64 = ((ll)1 << 62) - 1;
const double PI = 3.1415926535897932384626433832795;

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

int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
int lcm(int x, int y) { return x / gcd(x, y) * y; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

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

  ll n;
  cin >> n;

  vl t(n);
  for (int i = 0; i < n; i++) {
    cin >> t[i];
  }

  if (t.size() == 1) {
    cout << t[0] << endl;
    return 0;
  }

  ll res = t[0];
  for (int i = 0; i < n - 1; i++) {
    res = lcm(res, t[i + 1]);
  }

  cout << res << endl;
  return 0;
}
Problem p03633 (C++)

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

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

long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }

int main() {
  int n;
  std::cin >> n;
  std::vector<long long> t(n);
  for (int i = 0; i < n; i++) {
    std::cin >> t[i];
  }

  long long ans = lcm(t[0], t[1]);
  for (int i = 2; i < n; i++) {
    ans = lcm(ans, t[i]);
  }

  std::cout << ans << std::endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>

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

long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }

int main() {
  int n;
  std::cin >> n;
  std::vector<long long> t(n);
  for (int i = 0; i < n; i++) {
    std::cin >> t[i];
  }

  long long ans;
  if (n != 1) {
    ans = lcm(t[0], t[1]);
    for (int i = 2; i < n; i++) {
      ans = lcm(ans, t[i]);
    }
  } else {
    ans = t[0];
  }

  std::cout << ans << std::endl;
}
Problem p03633 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;
#define ll long long
ll gcd(ll m1, ll m2) { return (m2 == 0) ? m1 : gcd(m2, m1 & m2); }
int main() {
  ll n, t[10000], smp, ans = 1;
  cin >> n;
  while (n--) {
    cin >> smp;
    ans = smp / gcd(ans, smp) * ans;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;
#define ll long long
ll gcd(ll m1, ll m2) { return (m2 == 0) ? m1 : gcd(m2, m1 % m2); }
int main() {
  ll n, t[10000], smp, ans = 1;
  cin >> n;
  while (n--) {
    cin >> smp;
    ans = smp / gcd(ans, smp) * ans;
  }
  cout << ans << endl;
  return 0;
}
Problem p03633 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MAX 1000000000
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
const ll INF = 10010010010001;
vector<vector<bool>> graph;
vector<bool> reached;
ll MOD = 998244353;

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

int main() {
  int N;
  cin >> N;
  vector<ll> T(N);
  for (int i = 0; i < N; i++)
    cin >> T[i];
  ll gcd_n = gcd(T[0], T[1]);
  ll lcm_n = T[1] / gcd_n * T[0];
  for (int i = 2; i < N; i++) {
    gcd_n = gcd(lcm_n, T[i]);
    lcm_n = T[i] / gcd_n * lcm_n;
  }
  cout << lcm_n << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MAX 1000000000
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
const ll INF = 10010010010001;
vector<vector<bool>> graph;
vector<bool> reached;
ll MOD = 998244353;

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

int main() {
  int N;
  cin >> N;
  vector<ll> T(N);
  for (int i = 0; i < N; i++)
    cin >> T[i];
  ll gcd_n = 1;
  ll lcm_n = 1;
  for (int i = 0; i < N; i++) {
    gcd_n = gcd(lcm_n, T[i]);
    lcm_n = T[i] / gcd_n * lcm_n;
  }
  cout << lcm_n << endl;
}
Problem p03633 (C++)

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

#define mod 1000000007

using namespace std;
using ll = long long;

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

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

int main(void) {

  int N;

  cin >> N;

  ll ans = 1LL;

  for (int i = 0; i < N; i++) {
    ll T;
    cin >> T;
    ans = lcm(ans, T);
  }
  cout << ans;

  return 0;
}

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

#define mod 1000000007

using namespace std;
using ll = long long;

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

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

int main(void) {

  int N;

  cin >> N;

  ll ans = 1LL;

  for (int i = 0; i < N; i++) {
    ll T;
    cin >> T;
    ans = lcm(ans, T);
  }
  cout << ans;

  return 0;
}
Problem p03633 (Python)

Buggy submission (status: Runtime Error):
def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)


def lcm(a, b):
    return int(a * b / gcd(max(a, b), min(a, b)))


N = int(input())
T = [int(input()) for i in range(N)]

A = T[0]
for i in range(N):
    A = lcm(A, T[i])
print(A)


Fixed submission:
def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)


def lcm(a, b):
    return a * b // gcd(max(a, b), min(a, b))


N = int(input())
T = [int(input()) for i in range(N)]

A = T[0]
for i in range(N):
    A = lcm(A, T[i])
print(A)
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1000000001
// using edge = struct{ll to;ll cost;};
vector<pair<ll, ll>> dist[10010];
ll depth[10010];
void dfs(ll v, ll p, ll d) {
  depth[v] = d;
  for (auto &e : dist[v]) {
    if (e.first == p)
      continue;
    dfs(e.first, v, d + e.second);
  }
}

int main() {
  ll n;
  cin >> n;
  ll a[n - 1], b[n - 1], c[n - 1];
  // dist=vector<vector<ll>>(n+1,vector<ll>(n+1,INF));
  for (ll i = 0; i < n - 1; i++) {
    cin >> a[i] >> b[i] >> c[i];
    dist[a[i]].push_back(make_pair(b[i], c[i]));
    dist[b[i]].push_back(make_pair(a[i], c[i]));
    //	cout<<"i"<<endl;
  }
  // cout<<"aa"<<endl;
  ll q, k;
  cin >> q >> k;
  ll x[q], y[q];
  for (ll i = 0; i < q; i++)
    cin >> x[i] >> y[i];

  dfs(k, -1, 0);

  for (ll i = 0; i < q; i++) {
    // cout<<dist[x[i]][k]<<dist[k][y[i]]<<endl;
    cout << depth[x[i]] + depth[y[i]] << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1000000001
// using edge = struct{ll to;ll cost;};
vector<pair<ll, ll>> dist[100010];
ll depth[100010];
void dfs(ll v, ll p, ll d) {
  depth[v] = d;
  for (auto &e : dist[v]) {
    if (e.first == p)
      continue;
    dfs(e.first, v, d + e.second);
  }
}

int main() {
  ll n;
  cin >> n;
  ll a[n - 1], b[n - 1], c[n - 1];
  // dist=vector<vector<ll>>(n+1,vector<ll>(n+1,INF));
  for (ll i = 0; i < n - 1; i++) {
    cin >> a[i] >> b[i] >> c[i];
    dist[a[i]].push_back(make_pair(b[i], c[i]));
    dist[b[i]].push_back(make_pair(a[i], c[i]));
    //	cout<<"i"<<endl;
  }
  // cout<<"aa"<<endl;
  ll q, k;
  cin >> q >> k;
  ll x[q], y[q];
  for (ll i = 0; i < q; i++)
    cin >> x[i] >> y[i];

  dfs(k, -1, 0);

  for (ll i = 0; i < q; i++) {
    // cout<<dist[x[i]][k]<<dist[k][y[i]]<<endl;
    cout << depth[x[i]] + depth[y[i]] << endl;
  }
}
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(a) (a).begin(), (a).end()
using namespace std;
using ll = long long;
using P = pair<int, ll>;
vector<vector<P>> tree(10010);
vector<ll> depth(10010);

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

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; ++i) {
    int a, b;
    ll 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, 0ll);
  rep(i, q) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    cout << depth[x] + depth[y] << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
using ll = long long;
using P = pair<int, ll>;
vector<vector<P>> tree(100010);
vector<ll> depth(100010);

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

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; ++i) {
    int a, b;
    ll 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, 0ll);
  rep(i, q) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    cout << depth[x] + depth[y] << endl;
  }
}
Problem p03634 (C++)

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

int n;
const int MAX = 10010;
const long long INF = 1e18;
struct edge {
  int to;
  long long cost;
};
vector<edge> G[MAX];
long long D[MAX];
typedef pair<int, long long> P;

void dijkstra(int s) {
  for (int i = 0; i < n; i++) {
    D[i] = INF;
  }
  D[s] = 0;
  priority_queue<P, vector<P>, greater<P>> q;
  q.push({s, 0});
  while (!q.empty()) {
    P p = q.top();
    q.pop();
    int now = p.first;
    if (D[now] < p.second)
      continue;
    for (int i = 0; i < G[now].size(); i++) {
      edge e = G[now][i];
      if (D[e.to] > D[now] + e.cost) {
        D[e.to] = D[now] + e.cost;
        q.push({e.to, D[e.to]});
      }
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    long long 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;
  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>
using namespace std;

int n;
const int MAX = 100010;
const long long INF = 1e18;
struct edge {
  int to;
  long long cost;
};
vector<edge> G[MAX];
long long D[MAX];
typedef pair<int, long long> P;

void dijkstra(int s) {
  for (int i = 0; i < n; i++) {
    D[i] = INF;
  }
  D[s] = 0;
  priority_queue<P, vector<P>, greater<P>> q;
  q.push({s, 0});
  while (!q.empty()) {
    P p = q.top();
    q.pop();
    int now = p.first;
    if (D[now] < p.second)
      continue;
    for (int i = 0; i < G[now].size(); i++) {
      edge e = G[now][i];
      if (D[e.to] > D[now] + e.cost) {
        D[e.to] = D[now] + e.cost;
        q.push({e.to, D[e.to]});
      }
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    long long 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;
  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;
inline int read() {
  int x = 0, fh = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      fh = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return x * fh;
}
const int N = 1e5 + 10;
struct node {
  int y, nxt;
  LL d;
} a[N];
priority_queue<pair<LL, int>, vector<pair<LL, int>>, greater<pair<LL, int>>>
    heap;
int n, q, k, x, y, d, len, i, lst[N], vis[N];
LL dist[N];
void ins(int x, int y, int d) {
  a[++len].y = y;
  a[len].d = d;
  a[len].nxt = lst[x];
  lst[x] = len;
}
void dij(int s) {
  memset(dist, 0x3f3f3f3f, sizeof(dist));
  int x, y, i;
  LL d;
  dist[s] = 0;
  heap.push(make_pair(0, s));
  while (!heap.empty()) {
    d = heap.top().first;
    x = heap.top().second;
    heap.pop();
    if (vis[x])
      continue;
    vis[x] = 1;
    for (i = lst[x]; i; i = a[i].nxt) {
      y = a[i].y;
      if (dist[y] > d + a[i].d) {
        dist[y] = d + a[i].d;
        if (!vis[y])
          heap.push(make_pair(dist[y], y));
      }
    }
  }
  return;
}
int main() {
  n = read();
  for (i = 1; i < n; i++) {
    x = read();
    y = read();
    d = read();
    ins(x, y, d);
    ins(y, x, d);
  }
  q = read();
  k = read();
  dij(k);
  while (q--) {
    x = read();
    y = read();
    cout << dist[x] + dist[y] << '\n';
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
inline int read() {
  int x = 0, fh = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      fh = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return x * fh;
}
const int N = 1e6 + 10;
struct node {
  int y, nxt;
  LL d;
} a[N];
priority_queue<pair<LL, int>, vector<pair<LL, int>>, greater<pair<LL, int>>>
    heap;
int n, q, k, x, y, d, len, i, lst[N], vis[N];
LL dist[N];
void ins(int x, int y, int d) {
  a[++len].y = y;
  a[len].d = d;
  a[len].nxt = lst[x];
  lst[x] = len;
}
void dij(int s) {
  memset(dist, 0x3f3f3f3f, sizeof(dist));
  int x, y, i;
  LL d;
  dist[s] = 0;
  heap.push(make_pair(0, s));
  while (!heap.empty()) {
    d = heap.top().first;
    x = heap.top().second;
    heap.pop();
    if (vis[x])
      continue;
    vis[x] = 1;
    for (i = lst[x]; i; i = a[i].nxt) {
      y = a[i].y;
      if (dist[y] > d + a[i].d) {
        dist[y] = d + a[i].d;
        if (!vis[y])
          heap.push(make_pair(dist[y], y));
      }
    }
  }
  return;
}
int main() {
  n = read();
  for (i = 1; i < n; i++) {
    x = read();
    y = read();
    d = read();
    ins(x, y, d);
    ins(y, x, d);
  }
  q = read();
  k = read();
  dij(k);
  while (q--) {
    x = read();
    y = read();
    cout << dist[x] + dist[y] << '\n';
  }
  return 0;
}
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef long long ll;

const ll INF = 1e16 + 10;
const int A = 1e6 + 10;
class Gra {
public:
  int v, next;
  ll w;
} G[A << 2];
int head[A], tot;
ll Ans[A];

void add(int u, int v, ll w) {
  G[tot].v = v;
  G[tot].w = w;
  G[tot].next = head[u];
  head[u] = tot++;
}

void dfs(int u, int pre, ll dep) {
  Ans[u] = min(Ans[u], dep);
  for (int i = head[u]; i != -1; i = G[i].next) {
    int v = G[i].v;
    if (v == pre)
      continue;
    ll w = G[i].w;
    dfs(v, u, dep + w);
  }
}

int main() {
  freopen("input", "r", stdin);
  int n;
  scanf("%d", &n);
  memset(head, -1, sizeof(head));
  tot = 0;
  for (int i = 1; i < n; i++) {
    int u, v;
    ll w;
    scanf("%d%d%lld", &u, &v, &w);
    add(u, v, w);
    add(v, u, w);
  }
  int q, k;
  scanf("%d%d", &q, &k);
  fill(Ans, Ans + n + 1, INF);
  dfs(k, 0, 0);
  for (int i = 1; i <= q; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    ll ans = Ans[u] + Ans[v];
    printf("%lld\n", ans);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef long long ll;

const ll INF = 1e16 + 10;
const int A = 1e6 + 10;
class Gra {
public:
  int v, next;
  ll w;
} G[A << 2];
int head[A], tot;
ll Ans[A];

void add(int u, int v, ll w) {
  G[tot].v = v;
  G[tot].w = w;
  G[tot].next = head[u];
  head[u] = tot++;
}

void dfs(int u, int pre, ll dep) {
  Ans[u] = min(Ans[u], dep);
  for (int i = head[u]; i != -1; i = G[i].next) {
    int v = G[i].v;
    if (v == pre)
      continue;
    ll w = G[i].w;
    dfs(v, u, dep + w);
  }
}

int main() {
  // freopen("input","r",stdin);
  int n;
  scanf("%d", &n);
  memset(head, -1, sizeof(head));
  tot = 0;
  for (int i = 1; i < n; i++) {
    int u, v;
    ll w;
    scanf("%d%d%lld", &u, &v, &w);
    add(u, v, w);
    add(v, u, w);
  }
  int q, k;
  scanf("%d%d", &q, &k);
  fill(Ans, Ans + n + 1, INF);
  dfs(k, 0, 0);
  for (int i = 1; i <= q; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    ll ans = Ans[u] + Ans[v];
    printf("%lld\n", ans);
  }
  return 0;
}
Problem p03634 (C++)

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

using ll = long long;
using namespace std;
constexpr ll inf = 1e15;
constexpr ll mod = 1e9 + 7;

#define MAX_N 10'000

ll dp[MAX_N + 1];

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

  int N;
  cin >> N;
  map<int, vector<pair<int, int>>> edges;
  for (int n = 1; n < N; n++) {
    int a, b, c;
    cin >> a >> b >> c;
    edges[a].push_back(make_pair(b, c));
    edges[b].push_back(make_pair(a, c));
  }
  fill(begin(dp), end(dp), inf);

  int Q, K;
  cin >> Q >> K;
  dp[K] = 0;
  queue<int> q;
  q.push(K);
  while (!q.empty()) {
    int v = q.front();
    q.pop();

    for (auto to : edges[v]) {
      if (dp[to.first] != inf)
        continue;
      dp[to.first] = dp[v] + to.second;
      q.push(to.first);
    }
  }

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

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

using ll = long long;
using namespace std;
constexpr ll inf = 1e15;
constexpr ll mod = 1e9 + 7;

#define MAX_N 100'000

ll dp[MAX_N + 1];

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

  int N;
  cin >> N;
  map<int, vector<pair<int, int>>> edges;
  for (int n = 1; n < N; n++) {
    int a, b, c;
    cin >> a >> b >> c;
    edges[a].push_back(make_pair(b, c));
    edges[b].push_back(make_pair(a, c));
  }
  fill(begin(dp), end(dp), inf);

  int Q, K;
  cin >> Q >> K;
  dp[K] = 0;
  queue<int> q;
  q.push(K);
  while (!q.empty()) {
    int v = q.front();
    q.pop();

    for (auto to : edges[v]) {
      if (dp[to.first] != inf)
        continue;
      dp[to.first] = dp[v] + to.second;
      q.push(to.first);
    }
  }

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

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

struct node {
  node(int n, ll d) {
    num = n;
    cost = d;
  }
  int num;
  ll cost;
};
vector<vector<struct node>> graph(10001);

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

  int n;
  cin >> n;
  int a, b;
  ll c;
  rep(i, n - 1) {
    cin >> a >> b >> c;
    a--;
    b--;
    graph[a].emplace_back(b, c);
    graph[b].emplace_back(a, c); // how?
  }

  int Q, K;
  cin >> Q >> K;
  vector<ll> k_dist(n, -1);
  queue<pair<int, ll>> q;
  q.emplace(K - 1, 0);

  while (!q.empty()) {
    int now = q.front().first;
    ll dist = q.front().second;
    q.pop();
    k_dist[now] = dist;

    rep(i, graph[now].size()) {
      int next = graph[now][i].num;
      if (k_dist[next] != -1)
        continue;
      q.emplace(next, dist + graph[now][i].cost);
    }
  }

  int x, y;
  rep(i, Q) {
    cin >> x >> y;
    x--;
    y--;
    cout << k_dist[x] + k_dist[y] << "\n";
  }
}

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

struct node {
  node(int n, ll d) {
    num = n;
    cost = d;
  }
  int num;
  ll cost;
};
vector<vector<struct node>> graph(100001);

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

  int n;
  cin >> n;
  int a, b;
  ll c;
  rep(i, n - 1) {
    cin >> a >> b >> c;
    a--;
    b--;
    graph[a].emplace_back(b, c);
    graph[b].emplace_back(a, c); // how?
  }

  int Q, K;
  cin >> Q >> K;
  vector<ll> k_dist(n, -1);
  queue<pair<int, ll>> q;
  q.emplace(K - 1, 0);

  while (!q.empty()) {
    int now = q.front().first;
    ll dist = q.front().second;
    q.pop();
    k_dist[now] = dist;

    rep(i, graph[now].size()) {
      int next = graph[now][i].num;
      if (k_dist[next] != -1)
        continue;
      q.emplace(next, dist + graph[now][i].cost);
    }
  }

  int x, y;
  rep(i, Q) {
    cin >> x >> y;
    x--;
    y--;
    cout << k_dist[x] + k_dist[y] << "\n";
  }
}
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <utility>
using namespace std;
#define MS(m, z) memset(m, z, sizeof(m))
typedef unsigned U;
typedef pair<int, int> P;
typedef long long ll;
void inp();
int main() {
#ifdef XS
  freopen("D.in", "r", stdin);
#endif
  // int n;scanf("%d",&n);for(int i=1;i<=n;i++)
  inp();
  return 0;
}
const int N = 1e5 + 100;
int head[N], ecnt = 0;
struct E {
  int f, t, n;
  ll c;
  void set(int f, int t, int c, int n) {
    this->f = f, this->t = t, this->c = c, this->n = n;
  }
} es[N];
void ae(int a, int b, int c) {
  es[ecnt].set(a, b, c, head[a]);
  head[a] = ecnt;
  ecnt++;
}
ll d[N];
void dfs(int v, int fa) {
  E e;
  for (int i = head[v]; i != -1; i = e.n) {
    e = es[i];
    if (e.t == fa)
      continue;
    d[e.t] = d[v] + e.c;
    dfs(e.t, v);
  }
}
void inp() {
  MS(head, -1);
  int n;
  scanf("%d", &n);
  int a, b, c;
  for (int i = 0; i < n - 1; i++) {
    scanf("%d%d%d", &a, &b, &c);
    ae(a, b, c);
    ae(b, a, c);
  }
  int q, rt;
  scanf("%d%d", &q, &rt);
  dfs(rt, -1);
  for (int i = 0; i < q; i++) {
    scanf("%d%d", &a, &b);
    printf("%lld\n", d[a] + d[b]);
  }
}
// D.cc by xsthunder at Sat Aug 12 20:16:49 2017


Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <utility>
using namespace std;
#define MS(m, z) memset(m, z, sizeof(m))
typedef unsigned U;
typedef pair<int, int> P;
typedef long long ll;
void inp();
int main() {
#ifdef XS
  freopen("D.in", "r", stdin);
#endif
  // int n;scanf("%d",&n);for(int i=1;i<=n;i++)
  inp();
  return 0;
}
const int N = 1e5 + 100;
int head[N], ecnt = 0;
struct E {
  int f, t, n;
  ll c;
  void set(int f, int t, int c, int n) {
    this->f = f, this->t = t, this->c = c, this->n = n;
  }
} es[N * 2];
void ae(int a, int b, int c) {
  es[ecnt].set(a, b, c, head[a]);
  head[a] = ecnt;
  ecnt++;
}
ll d[N];
void dfs(int v, int fa) {
  E e;
  for (int i = head[v]; i != -1; i = e.n) {
    e = es[i];
    if (e.t == fa)
      continue;
    d[e.t] = d[v] + e.c;
    dfs(e.t, v);
  }
}
void inp() {
  MS(head, -1);
  int n;
  scanf("%d", &n);
  int a, b, c;
  for (int i = 0; i < n - 1; i++) {
    scanf("%d%d%d", &a, &b, &c);
    ae(a, b, c);
    ae(b, a, c);
  }
  int q, rt;
  scanf("%d%d", &q, &rt);
  dfs(rt, -1);
  for (int i = 0; i < q; i++) {
    scanf("%d%d", &a, &b);
    printf("%lld\n", d[a] + d[b]);
  }
}
// D.cc by xsthunder at Sat Aug 12 20:16:49 2017
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define _                                                                      \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
#define f first
#define s second

using namespace std;

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

int n, m, k;
ll d[maxn];
vector<pair<int, int>> g[maxn];
void dfs(int v, int p = -1) {
  for (int i = 0; i < g[v].size(); i++) {
    int to = g[v][i].f, w = g[v][i].s;
    if (to == p)
      continue;
    d[to] = d[v] + w;
    dfs(to, v);
  }
}

int main() {
  _
#ifndef ONLINE_JUDGE
      freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    g[u].pb(mp(v, w));
    g[v].pb(mp(u, w));
  }
  cin >> m >> k;
  dfs(k);
  for (int i = 1, a, b; i <= m; i++) {
    cin >> a >> b;
    cout << d[a] + d[b] << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define _                                                                      \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
#define f first
#define s second

using namespace std;

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

int n, m, k;
ll d[maxn];
vector<pair<int, int>> g[maxn];
void dfs(int v, int p = -1) {
  for (int i = 0; i < g[v].size(); i++) {
    int to = g[v][i].f, w = g[v][i].s;
    if (to == p)
      continue;
    d[to] = d[v] + w;
    dfs(to, v);
  }
}

int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    g[u].pb(mp(v, w));
    g[v].pb(mp(u, w));
  }
  cin >> m >> k;
  dfs(k);
  for (int i = 1, a, b; i <= m; i++) {
    cin >> a >> b;
    cout << d[a] + d[b] << endl;
  }

  return 0;
}
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define check(v)                                                               \
  rep(i, v.size()) cout << v[i] << " ";                                        \
  cout << endl
#define INF 1e8
typedef long long ll;
using namespace std;
// オーバーフローに気をつけろよおおおおおお
// 確認忘れるなよおおおおおお

struct edge {
  int to;
  ll cost;
};

vector<edge> tree[100010];
ll cost_k[100010];

void dfs(int v, int par, ll dist) {
  cost_k[v] = dist;

  for (int i = 0; i < tree[v].size(); i++) {
    if (tree[v][i].to == par)
      continue;
    dfs(tree[v][i].to, v, dist + tree[v][i].cost);
  }
}

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

  rep(i, n - 1) {
    int 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 << cost_k[x] + cost_k[y] << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define check(v)                                                               \
  rep(i, v.size()) cout << v[i] << " ";                                        \
  cout << endl
#define INF 1e8
typedef long long ll;
using namespace std;
// オーバーフローに気をつけろよおおおおおお
// 確認忘れるなよおおおおおお

struct edge {
  int to;
  ll cost;
};

vector<edge> tree[100010];
ll cost_k[100010];

void dfs(int v, int par, ll dist) {
  cost_k[v] = dist;

  for (int i = 0; i < tree[v].size(); i++) {
    if (tree[v][i].to == par)
      continue;
    dfs(tree[v][i].to, v, dist + tree[v][i].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 << cost_k[x] + cost_k[y] << endl;
  }

  return 0;
}
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++)

int n;
int a, b;
long c;
int q;
int k;
int x, y;
long ans[100001];
bool used[100001];
vector<pair<int, long>> v[100001];

void dfs(int s, int p, long d) {
  ans[s] = d;
  for (int i = 0; i < (int)v[s].size(); i++) {
    if (v[s][i].first != p)
      dfs(v[s][i].first, p, d + v[s][i].second);
  }
}
int main() {
  cin >> n;
  rep(i, n - 1) {
    cin >> a >> b >> c;
    v[a].push_back({b, c});
    v[b].push_back({a, c});
  }
  cin >> q >> k;
  for (int i = 1; i <= n; i++)
    ans[i] = 10000000000000000;
  dfs(k, 0, 0) rep(i, q) {
    cin >> x >> y;
    cout << ans[x] + ans[y] << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n)                                                              \
  ;                                                                            \
  for (int i = 0; i < n; i++)

int n;
int a, b;
long c;
int q;
int k;
int x, y;
long ans[100001];
bool used[100001];
vector<pair<int, long>> v[100001];

void dfs(int s, int p, long d) {
  ans[s] = d;
  for (int i = 0; i < (int)v[s].size(); i++) {
    if (v[s][i].first != p)
      dfs(v[s][i].first, s, d + v[s][i].second);
  }
}
int main() {
  cin >> n;
  rep(i, n - 1) {
    cin >> a >> b >> c;
    v[a].push_back({b, c});
    v[b].push_back({a, c});
  }
  cin >> q >> k;
  for (int i = 1; i <= n; i++)
    ans[i] = 10000000000000000;
  dfs(k, 0, 0) rep(i, q) {
    cin >> x >> y;
    cout << ans[x] + ans[y] << endl;
  }
  return 0;
}
Problem p03634 (C++)

Buggy submission (status: Time Limit Exceeded):
//============================================================================
// Name        : abc70d.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

map<int, bool> mp;
void dfs(vector<vector<pair<int, long long>>> &A, vector<long long> &dist,
         int n) {
  mp[n] = 1;
  for (int i = 0; i < A[n].size(); ++i) {
    dist[A[n][i].first] = min(dist[n] + A[n][i].second, dist[A[n][i].first]);
    if (mp[A[n][i].first] == 0)
      dfs(A, dist, A[n][i].first);
  }

  mp[n] = 0;
}

int main() {
  int N;
  cin >> N;
  vector<vector<pair<int, long long>>> A(N + 1);
  for (int i = 0; i < N - 1; ++i) {
    int a, b;
    long long c;
    scanf("%d %d %lld", &a, &b, &c);
    A[a].push_back(make_pair(b, c));
    A[b].push_back(make_pair(a, c));
  }

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

  vector<long long> dist(N + 1, 1ll << 62);
  dist[K] = 0;
  for (int i = 0; i < A[K].size(); ++i) {
    dist[A[K][i].first] =
        min(dist[A[K][i].first] + A[K][i].second, A[K][i].second);
    dfs(A, dist, A[K][i].first);
  }

  vector<int> a(Q), b(Q);
  for (int i = 0; i < Q; ++i) {
    scanf("%d %d", &a[i], &b[i]);
    printf("%lld\n", dist[a[i]] + dist[b[i]]);
  }

  return 0;
}


Fixed submission:
//============================================================================
// Name        : abc70d.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

map<int, bool> mp;
void dfs(vector<vector<pair<int, long long>>> &A, vector<long long> &dist,
         int n) {
  mp[n] = 1;
  for (int i = 0; i < A[n].size(); ++i) {
    dist[A[n][i].first] = min(dist[n] + A[n][i].second, dist[A[n][i].first]);
    if (mp[A[n][i].first] == 0)
      dfs(A, dist, A[n][i].first);
  }
}

int main() {
  int N;
  cin >> N;
  vector<vector<pair<int, long long>>> A(N + 1);
  for (int i = 0; i < N - 1; ++i) {
    int a, b;
    long long c;
    scanf("%d %d %lld", &a, &b, &c);
    A[a].push_back(make_pair(b, c));
    A[b].push_back(make_pair(a, c));
  }

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

  vector<long long> dist(N + 1, 1ll << 62);
  dist[K] = 0;
  for (int i = 0; i < A[K].size(); ++i) {
    dist[A[K][i].first] =
        min(dist[A[K][i].first] + A[K][i].second, A[K][i].second);
    dfs(A, dist, A[K][i].first);
  }

  vector<int> a(Q), b(Q);
  for (int i = 0; i < Q; ++i) {
    scanf("%d %d", &a[i], &b[i]);
    printf("%lld\n", dist[a[i]] + dist[b[i]]);
  }

  return 0;
}
Problem p03634 (C++)

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

#define INF 1 << 29
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, k, n) for (int i = (k); i < (int)(n); i++)

using namespace std;

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

int n, a, b, q, k;
ll c;
ll depth[100000] = {0};

typedef struct {
  int to;
  ll cost;
} edge;
vector<edge> tree[100000];

void dfs(int v, int p, ll d) {
  depth[v] = d;
  for (int i = 0; i < tree[v].size(); i++) {
    if (tree[v][i].to == p)
      continue;
    dfs(tree[v][i].to, v, d + tree[v][i].cost);
  }
}

int main() {
  scanf("%d", &n);
  REP(i, n) {
    scanf("%d %d %lld", &a, &b, &c);
    a--;
    b--;
    tree[a].push_back({b, c});
    tree[b].push_back({a, c});
  }
  scanf("%d %d", &q, &k);
  k--;
  dfs(k, -1, 0);
  REP(i, q) {
    int x, y;
    scanf("%d %d", &x, &y);
    x--;
    y--;
    printf("%lld\n", depth[x] + depth[y]);
  }
}


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

#define INF 1 << 29
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, k, n) for (int i = (k); i < (int)(n); i++)

using namespace std;

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

int n, a, b, q, k;
ll c;
ll depth[100000] = {0};

typedef struct {
  int to;
  ll cost;
} edge;
vector<edge> tree[100000];

void dfs(int v, int p, ll d) {
  depth[v] = d;
  for (int i = 0; i < tree[v].size(); i++) {
    if (tree[v][i].to == p)
      continue;
    dfs(tree[v][i].to, v, d + tree[v][i].cost);
  }
}

int main() {
  scanf("%d", &n);
  REP(i, n - 1) {
    scanf("%d %d %lld", &a, &b, &c);
    a--;
    b--;
    tree[a].push_back({b, c});
    tree[b].push_back({a, c});
  }
  scanf("%d %d", &q, &k);
  k--;
  dfs(k, -1, 0);
  REP(i, q) {
    int x, y;
    scanf("%d %d", &x, &y);
    x--;
    y--;
    printf("%lld\n", depth[x] + depth[y]);
  }
}
Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;

const ll inf = 1145148931919810;
vector<pair<ll, ll>> v[10000];
ll d[10000];

void dfs(ll pos, ll bef, ll cost) {
  for (auto u : v[pos]) {
    if (u.first != bef) {
      d[u.first] = min(d[u.first], cost + u.second);
      dfs(u.first, pos, d[u.first]);
    }
  }
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    --a;
    --b;
    v[a].push_back(pair<ll, ll>(b, c));
    v[b].push_back(pair<ll, ll>(a, c));
    d[i] = inf;
  }
  d[n - 1] = inf;
  int q, k;
  cin >> q >> k;
  --k;
  dfs(k, -1, 0);
  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 <iostream>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;

const ll inf = 1145148931919810;
vector<pair<ll, ll>> v[100000];
ll d[100000];

void dfs(ll pos, ll bef, ll cost) {
  for (auto u : v[pos]) {
    if (u.first != bef) {
      d[u.first] = min(d[u.first], cost + u.second);
      dfs(u.first, pos, d[u.first]);
    }
  }
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    --a;
    --b;
    v[a].push_back(pair<ll, ll>(b, c));
    v[b].push_back(pair<ll, ll>(a, c));
    d[i] = inf;
  }
  d[n - 1] = inf;
  int q, k;
  cin >> q >> k;
  --k;
  dfs(k, -1, 0);
  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;
#define FOR(i, l, r) for (long long i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define int long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
const int MOD = 1e9 + 7;
const int INF = 1e18;
const int limit = 100010;
struct Edge {
  int to, cost;
  Edge(int to, int cost) : to(to), cost(cost) {}
};
vector<Edge> tree[limit];
int depth[limit];

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

signed main() {
  int N;
  cin >> N;
  REP(i, N - 1) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    tree[a].pb({b, c});
    tree[b].pb({a, c});
  }
  int Q, K;
  cin >> Q >> K;
  K--;
  dfs(-1, K, 0);
  REP(i, Q) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    cout << depth[x] + depth[y] << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, l, r) for (long long i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define int long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
const int MOD = 1e9 + 7;
const int INF = 1e18;
const int limit = 100010;
struct Edge {
  int to, cost;
  Edge(int to, int cost) : to(to), cost(cost) {}
};
vector<Edge> tree[limit];
int depth[limit];

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

signed main() {
  int N;
  cin >> N;
  REP(i, N - 1) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    tree[a].pb({b, c});
    tree[b].pb({a, c});
  }
  int Q, K;
  cin >> Q >> K;
  K--;
  dfs(-1, K, 0);
  REP(i, Q) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    cout << depth[x] + depth[y] << endl;
  }
}
Problem p03634 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;

const ll INF64 = 1LL << 60;
const int INF32 = 1 << 29;
const ll MOD = 1000000007;

void dfs(vector<vector<P>> g, vector<ll> &dist, ll k, ll sum) {
  if (dist[k] != -1) {
    return;
  }
  dist[k] = sum;
  for (auto i : g[k]) {
    dfs(g, dist, i.first, sum + i.second);
  }
}

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

  ll q, k;
  cin >> q >> k;
  --k;
  vector<ll> dist(n, -1);
  dfs(g, dist, k, 0);

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

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

const ll INF64 = 1LL << 60;
const int INF32 = 1 << 29;
const ll MOD = 1000000007;

void dfs(const vector<vector<P>> &g, vector<ll> &dist, ll k, ll sum) {
  if (dist[k] != -1) {
    return;
  }
  dist[k] = sum;
  for (auto i : g[k]) {
    dfs(g, dist, i.first, sum + i.second);
  }
}

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

  ll q, k;
  cin >> q >> k;
  --k;
  vector<ll> dist(n, -1);
  dfs(g, dist, k, 0);

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;

bool visited[10005];
ll value[10005];
vector<pair<int, ll>> vec[100005];

void dfs(int v) {
  visited[v] = true;
  for (int i = 0; i < vec[v].size(); i++) {
    if (visited[vec[v][i].first] == false) {
      value[vec[v][i].first] = vec[v][i].second + value[v];
      dfs(vec[v][i].first);
    }
  }
}

int main() {
  int n, a, b, q, k, x, y;
  ll c;

  cin >> n;
  fill(visited, visited + n, false);

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

  cin >> q >> k;
  value[k] = 0;
  dfs(k);

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

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;

bool visited[100005];
ll value[100005];
vector<pair<int, ll>> vec[100005];

void dfs(int v) {
  visited[v] = true;
  for (int i = 0; i < vec[v].size(); i++) {
    if (visited[vec[v][i].first] == false) {
      value[vec[v][i].first] = vec[v][i].second + value[v];
      dfs(vec[v][i].first);
    }
  }
}

int main() {
  int n, a, b, q, k, x, y;
  ll c;

  cin >> n;
  fill(visited, visited + n, false);

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

  cin >> q >> k;
  value[k] = 0;
  dfs(k);

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

  return 0;
}