alexjercan/bugnet huggingface dataset:
Problem p02343 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, -1, sizeof(mat))
#define N 10000
struct edge {
  int to, cost;
};
int par[N];
int q, n;

void init(int n) { FOR(i, 0, n) par[i] = i; }

int root(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = root(par[x]);
  }
}

void unite(int x, int y) {
  x = root(x);
  y = root(y);
  if (x == y)
    return;
  par[x] = y;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> q;
  init(n);
  int com, x, y;
  FOR(i, 0, q) {
    cin >> com >> x >> y;
    if (com) {
      if (root(x) == root(y)) {
        cout << "1" << endl;
      } else {
        cout << "0" << endl;
      }
    } else {
      unite(x, y);
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, -1, sizeof(mat))
#define N 101
struct edge {
  int to, cost;
};
int par[N];
int q, n;

void init(int n) { FOR(i, 0, n) par[i] = i; }

int root(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = root(par[x]);
  }
}

void unite(int x, int y) {
  x = root(x);
  y = root(y);
  if (x == y)
    return;
  par[x] = y;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> q;
  init(n);
  int com, x, y;
  FOR(i, 0, q) {
    cin >> com >> x >> y;
    if (com) {
      if (root(x) == root(y)) {
        cout << "1" << endl;
      } else {
        cout << "0" << endl;
      }
    } else {
      unite(x, y);
    }
  }
  return 0;
}
Problem p02343 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

int main(void) {
  int n, q;

  std::cin >> n;
  std::cin >> q;

  std::vector<std::set<int>> sets;

  for (int i = 0; i < n; i++) {
    std::set<int> set;
    set.insert(i);
    sets.push_back(set);
  }

  for (int i = 0; i < q; i++) {
    int com, x, y;
    std::cin >> com;
    std::cin >> x;
    std::cin >> y;

    int xIndex = -1, yIndex = -1;
    for (int j = 0; j < sets.size(); j++) {
      if (xIndex < 0) {
        std::set<int>::iterator it = sets[j].find(x);
        if (it != sets[j].end()) {
          xIndex = j;
        }
      }
      if (yIndex < 0) {
        std::set<int>::iterator it = sets[j].find(y);
        if (it != sets[j].end()) {
          yIndex = j;
        }
      }
      if (xIndex >= 0 && yIndex >= 0) {
        break;
      }
    }

    // unite
    if (com == 0) {
      if (xIndex != yIndex) {
        if (xIndex > yIndex) {
          std::swap(xIndex, yIndex);
        }
        sets[xIndex].insert(sets[yIndex].begin(), sets[yIndex].end());
        sets.erase(sets.begin() + yIndex);
      }
    }
    // same
    else {
      if (xIndex == yIndex) {
        std::cout << "1" << std::endl;
      } else {
        std::cout << "0" << std::endl;
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

int main(void) {
  int n, q;

  std::cin >> n;
  std::cin >> q;

  std::vector<std::set<int>> sets;

  for (int i = 0; i < n; i++) {
    std::set<int> set;
    set.insert(i);
    sets.push_back(set);
  }

  for (int i = 0; i < q; i++) {
    int com, x, y;
    std::cin >> com;
    std::cin >> x;
    std::cin >> y;

    int xIndex = -1, yIndex = -1;
    for (int j = 0; j < sets.size(); j++) {
      if (xIndex < 0) {
        std::set<int>::iterator it = sets[j].find(x);
        if (it != sets[j].end()) {
          xIndex = j;
        }
      }
      if (yIndex < 0) {
        std::set<int>::iterator it = sets[j].find(y);
        if (it != sets[j].end()) {
          yIndex = j;
        }
      }
      if (xIndex >= 0 && yIndex >= 0) {
        break;
      }
    }

    // unite
    if (com == 0) {
      if (xIndex != yIndex) {
        std::set<int> newSet;
        newSet.insert(sets[xIndex].begin(), sets[xIndex].end());
        newSet.insert(sets[yIndex].begin(), sets[yIndex].end());

        if (xIndex < yIndex) {
          sets.erase(sets.begin() + yIndex);
          sets.erase(sets.begin() + xIndex);
        } else {
          sets.erase(sets.begin() + xIndex);
          sets.erase(sets.begin() + yIndex);
        }
        sets.push_back(newSet);
      }
    }
    // same
    else {
      if (xIndex == yIndex) {
        std::cout << "1" << std::endl;
      } else {
        std::cout << "0" << std::endl;
      }
    }
  }

  return 0;
}
Problem p02343 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
using namespace std;

struct union_find_tree {

  vector<ll> par;

  union_find_tree(ll n) {
    par.resize(n);
    for (ll i = 0; i < n; i++) {
      par[i] = i;
    }
  }

  ll find(ll a) {
    if (par[a] == a)
      return a;
    return par[a] = find(par[a]);
  }

  void unite(ll a, ll b) { par[find(a)] = find(b); }

  bool same(ll a, ll b) { return find(a) == find(b); }
};

int main() {
  ll n, q;
  cin >> n >> q;
  union_find_tree uni(n);
  for (ll i = 0; i < q; i++) {
    ll c, x, y;
    cin >> c >> x >> y;
    if (c == 0) {
      uni.unite(x, y);
    } else {
      if (uni.same(x, y)) {
        cout << 1 << endl;
      } else {
        cout << 0 << endl;
      }
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
using namespace std;

struct union_find_tree {

  vector<ll> par;

  union_find_tree(ll n) {
    par.resize(n);
    for (ll i = 0; i < n; i++) {
      par[i] = i;
    }
  }

  ll find(ll a) {
    if (par[a] == a)
      return a;
    return par[a] = find(par[a]);
  }

  void unite(ll a, ll b) { par[find(a)] = b; }

  bool same(ll a, ll b) { return find(a) == find(b); }
};

int main() {
  ll n, q;
  cin >> n >> q;
  union_find_tree uni(n);
  for (ll i = 0; i < q; i++) {
    ll c, x, y;
    cin >> c >> x >> y;
    if (c == 0) {
      uni.unite(x, y);
    } else {
      if (uni.same(x, y)) {
        cout << 1 << endl;
      } else {
        cout << 0 << endl;
      }
    }
  }
}
Problem p02343 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;
#define MAX_N 114514
vector<int> X[MAX_N];
int Group[MAX_N], N, Q, A, B, C;
int main() {
  cin >> N >> Q;
  for (int i = 0; i < MAX_N; i++) {
    X[i].push_back(i);
    Group[i] = i;
  }
  for (int i = 0; i < Q; i++) {
    cin >> A >> B >> C;
    int D = Group[B], E = Group[C];
    if (A == 0 && D != E) {
      if (X[D].size() >= X[E].size()) {
        for (int j = 0; j < X[E].size(); j++) {
          Group[X[E][j]] = D;
          X[D].push_back(X[E][j]);
        }
        X[E].clear();
      } else if (X[D].size() < X[E].size()) {
        for (int j = 0; j < X[D].size(); j++) {
          Group[X[D][j]] = E;
          X[E].push_back(X[D][j]);
        }
        X[D].clear();
      }
    }
    if (A == 1) {
      if (D == E) {
        cout << "1" << endl;
      } else {
        cout << "0" << endl;
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
#define MAX_N 114514
vector<int> X[MAX_N];
int Group[MAX_N], N, Q, A, B, C;
int main() {
  cin >> N >> Q;
  for (int i = 0; i < MAX_N; i++) {
    X[i].push_back(i);
    Group[i] = i;
  }
  for (int i = 0; i < Q; i++) {
    cin >> A >> B >> C;
    int D = Group[B], E = Group[C];
    if (A == 0) {
      if (X[D].size() >= X[E].size()) {
        for (int j = 0; j < X[E].size(); j++) {
          Group[X[E][j]] = D;
          X[D].push_back(X[E][j]);
        }
        X[E].clear();
      } else if (X[D].size() < X[E].size()) {
        for (int j = 0; j < X[D].size(); j++) {
          Group[X[D][j]] = E;
          X[E].push_back(X[D][j]);
        }
        X[D].clear();
      }
    }
    if (A == 1) {
      if (D == E) {
        cout << "1" << endl;
      } else {
        cout << "0" << endl;
      }
    }
  }
  return 0;
}
Problem p02343 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

typedef long long signed int ll;

constexpr ll NIL = -20000000000;

int u[10010];

int main() {
  for (int i = 0; i < 10010; i++)
    u[i] = -1;
  int n, q, x, y, com;
  cin >> n >> q;
  for (int i = 0; i < q; i++) {
    cin >> com >> x >> y;
    if (com) {
      int c[2] = {x, y};
      while (u[c[0]] != -1)
        c[0] = u[c[0]];
      while (u[c[1]] != -1)
        c[1] = u[c[1]];
      if (c[0] == c[1])
        cout << 1 << endl;
      else
        cout << 0 << endl;
    } else {
      int c[2] = {x, y};
      while (u[c[0]] != -1)
        c[0] = u[c[0]];
      while (u[c[1]] != -1)
        c[1] = u[c[1]];
      if (c[0] != c[1])
        u[c[1]] = c[0];
    }
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

typedef long long signed int ll;

constexpr ll NIL = -20000000000;

int u[10010];

int main() {
  for (int i = 0; i < 10010; i++)
    u[i] = -1;
  int n, q, x, y, com;
  cin >> n >> q;
  for (int i = 0; i < q; i++) {
    cin >> com >> x >> y;
    if (com) {
      int c[2] = {x, y};
      while (u[c[0]] != -1)
        c[0] = u[c[0]];
      while (u[c[1]] != -1)
        c[1] = u[c[1]];
      if (c[0] == c[1])
        cout << 1 << endl;
      else
        cout << 0 << endl;
    } else {
      int c[2] = {x, y};
      while (u[c[0]] != -1)
        c[0] = u[c[0]];
      while (u[c[1]] != -1)
        c[1] = u[c[1]];
      u[c[1]] = c[0];
    }
  }
}
Problem p02343 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

int main() {
  int n, query_num;

  std::cin >> n >> query_num;

  std::vector<std::pair<int, int>> v;
  for (int i = 0; i < n; i++) {
    v.push_back(std::make_pair(i, i));
  }

  for (int i = 0; i < query_num; i++) {
    int query, a, b;
    std::cin >> query >> a >> b;
    if (query == 0) { // unite
      int num = v[b].second;
      for (size_t j = 0; j < v.size(); j++) {
        if (v[j].second == num) {
          v[j].second = v[a].second;
        }
      }
    } else { // same
      if (v[a].second == v[b].second) {
        std::cout << 1 << std::endl;
      } else {
        std::cout << 0 << std::endl;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

// num???v?????????????????¨???????????????.
int num_pos(std::vector<std::vector<int>> v, int num) {
  for (size_t i = 0; i < v.size(); i++) {
    if (std::find(v[i].begin(), v[i].end(), num) != v[i].end()) {
      return i;
    }
  }
  std::cerr << "vector??????" << num << "??????????????????" << std::endl;
  exit(1);
}

int main() {
  int n, query_num;

  std::cin >> n >> query_num;

  std::vector<std::vector<int>> v(n);
  for (int i = 0; i < n; i++) {
    v[i].push_back(i);
  }

  for (int i = 0; i < query_num; i++) {
    int query, a, b;
    std::cin >> query >> a >> b;
    if (query == 0) { // unite
      int c, d;
      c = num_pos(v, a);
      d = num_pos(v, b);
      if (c != d) {
        if (c > d)
          std::swap(c, d);
        std::copy(v[d].begin(), v[d].end(), std::back_inserter(v[c]));
        v.erase(v.begin() + d);
      }
    } else { // same
      int pos = num_pos(v, a);
      if (std::find(v[pos].begin(), v[pos].end(), b) != v[pos].end()) {
        std::cout << 1 << std::endl;
      } else {
        std::cout << 0 << std::endl;
      }
    }
  }
  return 0;
}
Problem p02343 (C++) — buggy submission, verdict: Runtime Error

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

int parent[10005];

int getp(int x) {
  if (parent[x] == x)
    return (x);
  return (parent[x] = getp(parent[x]));
}

int main() {
  int N, Q, A, B, C;
  cin >> N >> Q;
  for (int i = 0; i < N; i++)
    parent[i] = i;
  while (Q--) {
    cin >> A >> B >> C;
    if (A == 0)
      parent[getp(B)] = getp(C);
    else if (getp(B) == getp(C))
      cout << 1 << endl;
    else
      cout << 0 << endl;
  }
  return (0);
}

Error: 0

Fixed submission:

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

int parent[10005] = {};

int getp(int x) {
  if (!parent[x])
    return (x);
  return (parent[x] = getp(parent[x]));
}

int main() {
  int N, Q, A, B, C;
  cin >> N >> Q;
  while (Q--) {
    cin >> A >> B >> C;
    B++, C++;
    if (A == 0)
      parent[getp(B)] = getp(C);
    else if (getp(B) == getp(C))
      cout << 1 << endl;
    else
      cout << 0 << endl;
  }
  return (0);
}
Problem p02344 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;

vector<int> p, rank, cost;

void makeSet(int x) {
  p.push_back(x);
  rank.push_back(0);
  cost.push_back(0);
}

int findSet(int x) {
  int prep = p[x];
  if (x != p[x]) {
    p[x] = findSet(p[x]);
  }
  cost[x] += cost[prep];
  return p[x];
}

void relate(int x, int y, int z) {
  int rootx = findSet(x), rooty = findSet(y);
  if (rootx == rooty)
    return;
  z = z + cost[x] - cost[y];
  if (rank[rooty] < rank[rootx]) {
    p[rooty] = rootx;
    cost[rooty] = z;
  } else {
    p[rootx] = rooty;
    cost[rootx] = -z;
    if (rank[rootx] == rank[rooty])
      rank[rooty] += 1;
  }
}

int main() {
  int n, q, com, x, y, z;
  cin >> n >> q;
  for (int i = 0; i < n; i++)
    makeSet(i);
  for (int i = 0; i < q; i++) {
    cin >> com >> x >> y;
    if (com == 0) {
      cin >> z;
      relate(x, y, z);
    } else {
      if (findSet(x) == findSet(y))
        cout << cost[y] - cost[x] << endl;
      else
        cout << '?' << endl;
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

vector<int> p, cost;

void makeSet(int x) {
  p.push_back(x);
  cost.push_back(0);
}

int findSet(int x) {
  int prep = p[x];
  if (x != p[x]) {
    p[x] = findSet(p[x]);
  }
  cost[x] += cost[prep];
  return p[x];
}

void relate(int x, int y, int z) {
  if (p[y] != y) {
    if (cost[y] >= z) {
      p[x] = p[y];
      cost[x] = cost[y] - z;
      p[y] = x;
      cost[y] = z;
    } else {
      p[x] = p[p[y]];
      p[p[y]] = x;
      cost[p[y]] = z - cost[y];
    }
  } else {
    p[y] = x;
    cost[y] = z;
  }
}

int main() {
  int n, q, com, x, y, z;
  cin >> n >> q;
  for (int i = 0; i < n; i++)
    makeSet(i);
  for (int i = 0; i < q; i++) {
    cin >> com >> x >> y;
    if (com == 0) {
      cin >> z;
      relate(x, y, z);
    } else {
      if (findSet(x) == findSet(y))
        cout << cost[y] - cost[x] << endl;
      else
        cout << '?' << endl;
    }
  }
  return 0;
}
Problem p02344 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#include <stdlib.h>

int N = 0;
int Q = 0;
int S1[100000];
int S2[100000];
int S3[100000];
int Com = 0;
int V1 = 0;
int V2 = 0;
int V3 = 0;
void relate();
void query();
void merge();

int main() {
  int i = 0;
  scanf("%d %d", &N, &Q);
  for (i = 0; i < N; i++) {
    S1[i] = i;
    S2[i] = 0;
    S3[i] = 1;
  }

  while (Q > 0) {
    scanf("%d", &Com);
    if (Com) {
      scanf("%d %d", &V1, &V2);
      query();
    } else {
      scanf("%d %d %d", &V1, &V2, &V3);
      relate();
    }
    Q--;
  }
}

void relate() {
  // printf("R : %d %d %d\\n", V1, V2, V3);
  if (S1[V1] == S1[V2]) {
    return;
  } else {
    merge();
  }
}

void merge() {
  int i = 0;
  int s3 = S2[V2] - V3 - S2[V1];
  int s1 = S1[V1];
  int s2 = S1[V2];
  if (S3[s1] == 1) {
    S3[s1]--;
    S3[s2]++;
    S1[V1] = s2;
    S2[V1] = S2[V1] + s3;
    // printf("Fast1\\n");
    return;
  } else {
    if (S3[s2] == 1) {
      S3[s1]++;
      S3[s2]--;
      S1[V2] = s1;
      S2[V2] = S2[V2] - s3;
      // printf("Fast2\\n");
      return;
    }
  }

  for (i = 0; i < N; i++) {
    if (S1[i] == s1) {
      // System.out.println("V1 = "+ V1 + "V2 = " + V2+ "V3 = " + V3);
      // printf("V1 = %d, V2 = %d, V3 = %d\\n",V1, V2, V3);
      S3[s1]--;
      S3[s2]++;
      S1[i] = s2;
      S2[i] = S2[i] + s3;
      // printf("S2[%d] = %d\\n", i, S2[i]);
      // System.out.println("S2[i] = "+ S2[i]);
    } else {
      continue;
    }
  }
}

void query() {
  // printf("Q : %d %d \\n", V1, V2);
  if (S1[V1] == S1[V2]) {
    printf("%d\\n", S2[V2] - S2[V1]);
  } else {
    printf("?\\n");
  }
}


Error: TLE

Fixed submission:

#include <stdio.h>
#include <stdlib.h>

int N = 0;
int Q = 0;
int S1[100000];
int S2[100000];
int Com = 0;
int V1 = 0;
int V2 = 0;
int V3 = 0;
void relate();
void query();
void merge();

int main() {
  int i = 0;
  scanf("%d %d", &N, &Q);
  for (i = 0; i < N; i++) {
    S1[i] = i;
    S2[i] = 0;
  }

  while (Q > 0) {
    scanf("%d", &Com);
    if (Com) {
      scanf("%d %d", &V1, &V2);
      query();
    } else {
      scanf("%d %d %d", &V1, &V2, &V3);
      relate();
    }
    Q--;
  }
}

void relate() {
  // printf("R : %d %d %d\\n", V1, V2, V3);
  if (S1[V1] == S1[V2]) {
    return;
  } else {
    merge();
  }
}

void merge() {
  int i = 0;
  V3 = S2[V2] - V3 - S2[V1];
  V1 = S1[V1];
  V2 = S1[V2];
  for (i = 0; i < N; i++) {
    if (S1[i] == V1) {
      // System.out.println("V1 = "+ V1 + "V2 = " + V2+ "V3 = " + V3);
      // printf("V1 = %d, V2 = %d, V3 = %d\\n",V1, V2, V3);
      S1[i] = V2;
      S2[i] = S2[i] + V3;
      // printf("S2[%d] = %d\\n", i, S2[i]);
      // System.out.println("S2[i] = "+ S2[i]);
    } else {
      continue;
    }
  }
}

void query() {
  // printf("Q : %d %d \\n", V1, V2);
  if (S1[V1] == S1[V2]) {
    printf("%d\\n", S2[V2] - S2[V1]);
  } else {
    printf("?\\n");
  }
}
Problem p02344 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stack>
#include <stdio.h>
using namespace std;
const int N = 1e5 + 10;
int p[N], val[N];
stack<int> s[N];
int find(int n) {
  if (p[n] == n)
    return n;
  return p[n] = find(p[n]);
}
int main() {
  int n, q, l, r, temp, a, b;
  scanf("%d%d", &n, &q);
  for (int i = 0; i < n; i++) {
    p[i] = i;
    val[i] = 0;
    s[i].push(i);
  }
  while (q--) {
    scanf("%d%d%d", &temp, &l, &r);
    if (temp == 1) {
      if (find(l) != find(r))
        printf("?\\n");
      else
        printf("%d\\n", val[r] - val[l]);
    } else {
      scanf("%d", &temp);
      a = find(l);
      b = find(r);
      if (a == b)
        continue;
      if (s[a].size() > s[b].size()) {
        p[b] = a;
        temp += val[l] - val[r];
        while (!s[b].empty()) {
          val[s[b].top()] += temp;
          s[a].push(s[b].top());
          s[b].pop();
        }
      } else {
        p[a] = b;
        temp = val[r] - temp - val[l];
        while (!s[a].empty()) {
          val[s[a].top()] += temp;
          s[b].push(s[a].top());
          s[a].pop();
        }
      }
    }
  }
}


Error: TLE

Fixed submission:

#include <stack>
#include <stdio.h>
using namespace std;
const int N = 1e5 + 10;
int p[N], val[N];
stack<int> s[N];
int find(int n) {
  if (p[n] == n)
    return n;
  return p[n] = find(p[n]);
}
int main() {
  int n, q, l, r, temp, a, b;
  scanf("%d%d", &n, &q);
  for (int i = 0; i < n; i++) {
    p[i] = i;
    val[i] = 0;
    s[i].push(i);
  }
  while (q--) {
    scanf("%d%d%d", &temp, &l, &r);
    if (temp == 1) {
      if (find(l) != find(r))
        printf("?\\n");
      else
        printf("%d\\n", val[r] - val[l]);
    } else {
      scanf("%d", &temp);
      a = find(l);
      b = find(r);
      if (s[a].size() > s[b].size()) {
        p[b] = a;
        temp += val[l] - val[r];
        while (!s[b].empty()) {
          val[s[b].top()] += temp;
          s[a].push(s[b].top());
          s[b].pop();
        }
      } else {
        p[a] = b;
        temp = val[r] - temp - val[l];
        while (!s[a].empty()) {
          val[s[a].top()] += temp;
          s[b].push(s[a].top());
          s[a].pop();
        }
      }
    }
  }
}
Problem p02344 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <climits>
#include <cstdio>
#include <utility>
using namespace std;

class node {
  int c;
  int h;
  node *p;

public:
  node() {
    c = 0;
    h = 0;
    p = NULL;
  }
  void incC() { c++; }
  void setHP(int height, node *parent) {
    h = height;
    p = parent;
  }
  int getC() { return c; }
  pair<int, node *> getHP() {
    if (this->p == NULL)
      return make_pair(0, this);
    else {
      pair<int, node *> tmp = p->getHP();
      tmp.first += h;
      return tmp;
    }
  }
};

node ob[100000];

int diff(int x, int y) {
  pair<int, node *> tmp1 = ob[x].getHP();
  pair<int, node *> tmp2 = ob[y].getHP();
  if (tmp1.second != tmp2.second)
    return INT_MAX;
  else
    return tmp2.first - tmp1.first;
}

void relate(int x, int y, int z) {
  pair<int, node *> tmp1 = ob[x].getHP();
  pair<int, node *> tmp2 = ob[y].getHP();
  if (tmp1.second == tmp2.second)
    return;
  if (tmp1.second->getC() < tmp2.second->getC()) {
    tmp1.second->setHP(tmp2.first - tmp1.first - z, tmp2.second);
  } else {
    tmp2.second->setHP(tmp1.first - tmp2.first + z, tmp1.second);
    if (tmp1.second->getC() == tmp2.second->getC()) {
      tmp1.second->incC();
    }
  }
}

int main() {
  int i, n, q, com, x, y, z, tmp;
  scanf("%d %d", &n, &q);
  for (i = 0; i < q; i++) {
    scanf("%d", &com);
    if (com) {
      scanf("%d %d", &x, &y);
      int tmp = diff(x, y);
      if (tmp == INT_MAX)
        printf("?\\n");
      else
        printf("%d\\n", diff(x, y));
    } else {
      scanf("%d %d %d", &x, &y, &z);
      relate(x, y, z);
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <climits>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;

map<int, int> w[100000];
int kakunin[100000];

int diff(int x, int y, int c) {
  if (x == y)
    return 0;
  int ret = INT_MAX;
  int tmp;
  map<int, int>::iterator itr;
  for (itr = w[x].begin(); itr != w[x].end(); itr++) {
    if (kakunin[itr->first] == c)
      continue;
    kakunin[itr->first] = c;
    tmp = diff(itr->first, y, c);
    if (tmp < INT_MAX)
      ret = itr->second + tmp;
  }
  return ret;
}

int main() {
  int i, n, q, x, y, z, com;
  int c = 0;
  memset(kakunin, 0, sizeof(int) * 100000);
  scanf("%d %d", &n, &q);
  for (i = 0; i < q; i++) {
    scanf("%d", &com);
    if (com) {
      scanf("%d %d", &x, &y);
      c++;
      int ans = diff(x, y, c);
      if (ans == INT_MAX)
        printf("?\\n");
      else
        printf("%d\\n", ans);
    } else {
      scanf("%d %d %d", &x, &y, &z);
      w[x][y] = z;
      w[y][x] = -z;
    }
  }
  return 0;
}