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

#include <algorithm>
#include <iostream>
#include <map>
#define N 210
using namespace std;

int n, m;
int d[N][N];

bool check() { /* 誤った規則があればfalse */
  for (int k = 0; k < m; k++)
    for (int i = 0; i < m; i++)
      for (int j = 0; j < m; j++)
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);

  for (int i = 0; i < m; i++)
    if (d[i][i] < 0)
      return false;
  return true;
}

int main() {
  string a, b, s;
  int x;
  char c;

  while (cin >> n && n) {
    m = n * 2;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++)
        d[i][j] = 100000000;
      d[i][i] = 0;
    }
    map<string, int> units; // units[ a ] でaの配列番号が分かる
    m = 0;
    for (int i = 0; i < n; i++) {
      cin >> x >> a >> c; // 1 [A] =
      cin >> x >> c;      // 10^
      cin >> x >> b;      // [x] [B];

      if (units.find(a) == units.end())
        units[a] = m++;
      if (units.find(b) == units.end())
        units[b] = m++;

      int an = units[a], bn = units[b];

      d[an][bn] = -x;
      d[bn][an] = x;
    }

    if (check())
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <utility>
#define P pair<string, int>
#define N 110
using namespace std;

int n, m;
int d[N][N];
map<string, int> units; // units[ a ] でaの配列番号が分かる

bool check() { /* 誤った規則があればfalse */

  for (int k = 0; k < m; k++)
    for (int i = 0; i < m; i++)
      for (int j = 0; j < m; j++)
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);

  for (int i = 0; i < m; i++)
    if (d[i][i] != 0)
      return false;
  return true;
}

int main() {
  string a, b, s;
  int x;

  while (cin >> n && n) {
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++)
        d[i][j] = 1000;

    units.clear();
    m = 0;
    getline(cin, s); // 改行コードまで
    for (int i = 0; i < n; i++) {
      /*
      cin >> x >> a >> c; // 1 [A] =
      cin >> x >> c;  // 10^
      cin >> x >> b;  // [x] [B];
      */
      getline(cin, s); //      cout << s << endl;

      int j = s.find(" ") + 1;
      a = "";
      while (s[j] != ' ')
        a += s[j++];
      j = s.find("^") + 1;
      x = atoi(s.c_str() + j);
      j = s.find(" ", j) + 1;
      b = "";
      while (j < (int)s.length())
        b += s[j++];
      //      cout << a << " " << x <<" " << b << endl;

      if (units.find(a) == units.end())
        units.insert(P(a, m++));
      if (units.find(b) == units.end())
        units.insert(P(b, m++));

      int an = units[a], bn = units[b];
      d[an][bn] = x;
      d[bn][an] = -x;
    }

    if (check())
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}
Problem p01324 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define rp(i, c) rep(i, (c).size())
#define fr(i, c)                                                               \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = 1 << 28;
const double INF = 1e12, EPS = 1e-9;

int n, m, e[200][200];

int main() {
  while (cin >> n, n) {
    map<string, int> id;
    rep(i, 2 * n) rep(j, 2 * n) e[i][j] = inf;

    string u1, u2, t;
    m = 0;
    rep(i, n) {
      cin >> t >> u1 >> t >> t >> u2;
      if (!id.count(u1))
        id[u1] = m++;
      if (!id.count(u2))
        id[u2] = m++;
      e[id[u1]][id[u2]] = atoi(t.substr(3).c_str());
      e[id[u2]][id[u1]] = -e[id[u1]][id[u2]];
    }
    rep(k, m) rep(i, m) rep(j, m) e[i][j] = min(e[i][j], e[i][k] + e[k][j]);
    bool f = 0;
    rep(i, m) if (e[i][i] != inf && e[i][i] != 0) f = 1;
    puts(f ? "No" : "Yes");
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define rp(i, c) rep(i, (c).size())
#define fr(i, c)                                                               \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = 1 << 28;
const double INF = 1e12, EPS = 1e-9;

int n, m, e[100][100];

int main() {
  while (cin >> n, n) {
    map<string, int> id;
    rep(i, n) rep(j, n) e[i][j] = inf;

    string u1, u2, t;
    m = 0;
    rep(i, n) {
      cin >> t >> u1 >> t >> t >> u2;
      if (!id.count(u1))
        id[u1] = m++;
      if (!id.count(u2))
        id[u2] = m++;
      e[id[u1]][id[u2]] = atoi(t.substr(3).c_str());
      e[id[u2]][id[u1]] = -e[id[u1]][id[u2]];
    }
    rep(k, n) rep(i, n) rep(j, n) e[i][j] = min(e[i][j], e[i][k] + e[k][j]);
    bool f = 0;
    rep(i, n) if (e[i][i] != inf && e[i][i] != 0) f = 1;
    puts(f ? "No" : "Yes");
  }
  return 0;
}
Problem p01324 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

struct edge {
  int from;
  int cost;
  int to;
};

map<string, int> mp;
int d[1000];
vector<edge> es;

int main() {
  int n;
  int a, b, size;
  string t1, t2, t3, t4;
  edge e1, e2;
  while (cin >> n && n) {
    int count = 1;
    mp.clear();
    es.clear();
    for (int i = 0; i < n; i++) {
      cin >> a >> t1 >> t2 >> t3 >> t4;
      string s(t3.begin() + 3, t3.end());
      stringstream ss;
      ss << s;
      ss >> b;
      if (mp[t1] == 0)
        mp[t1] = count++;
      if (mp[t4] == 0)
        mp[t4] = count++;
      e1.from = mp[t4];
      e1.cost = b;
      e1.to = mp[t1];
      es.push_back(e1);
      e2.from = mp[t1];
      e2.cost = -b;
      e2.to = mp[t4];
      es.push_back(e2);
    }
    size = es.size();
    fill(d, d + 1000, 0);
    bool update = true;
    for (int i = 0; i <= 2 * n; i++) {
      update = false;
      for (int j = 0; j < size; j++) {
        edge e = es[j];
        int s = e.from, t = e.to;
        if (d[t] > d[s] + e.cost) {
          d[t] = d[s] + e.cost;
          update = true;
        }
      }
      if (!update)
        break;
    }
    if (update)
      cout << "No" << endl;
    else
      cout << "Yes" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

struct edge {
  int from;
  int cost;
  int to;
};

map<string, int> mp;
int d[200];
vector<edge> es;

int main() {
  int n;
  int a, b, size;
  string t1, t2, t3, t4;
  edge e1, e2;
  while (cin >> n && n) {
    int count = 1;
    mp.clear();
    es.clear();
    for (int i = 0; i < n; i++) {
      cin >> a >> t1 >> t2 >> t3 >> t4;
      string s(t3.begin() + 3, t3.end());
      stringstream ss;
      ss << s;
      ss >> b;
      if (mp[t1] == 0)
        mp[t1] = count++;
      if (mp[t4] == 0)
        mp[t4] = count++;
      e1.from = mp[t4];
      e1.cost = b;
      e1.to = mp[t1];
      es.push_back(e1);
      e2.from = mp[t1];
      e2.cost = -b;
      e2.to = mp[t4];
      es.push_back(e2);
    }
    size = es.size();
    fill(d, d + count, 0);
    bool update = true;
    for (int i = 0; i <= 2 * n; i++) {
      update = false;
      for (int j = 0; j < size; j++) {
        edge e = es[j];
        int s = e.from, t = e.to;
        if (d[t] > d[s] + e.cost) {
          d[t] = d[s] + e.cost;
          update = true;
        }
      }
      if (!update)
        break;
    }
    if (update)
      cout << "No" << endl;
    else
      cout << "Yes" << endl;
  }
  return 0;
}
Problem p01324 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <map>
#include <stdio.h>
#include <string>
using namespace std;
char in[30];
int UF[300];
int w[300];
pair<int, int> FIND(int a) {
  if (UF[a] < 0)
    return make_pair(a, 0);
  pair<int, int> ret = FIND(UF[a]);
  ret.second += w[a];
  return ret;
}
bool ok = true;
void UNION(int a, int b, int c) {
  pair<int, int> A = FIND(a);
  pair<int, int> B = FIND(b);
  int d = A.first;
  int e = B.first;
  int f = A.second + c - B.second;
  if (d == e) {
    if (B.second - c != A.second)
      ok = false;
    return;
  }
  UF[d] += UF[d];
  UF[e] = d;
  w[e] = f;
}
int main() {
  int a;
  while (scanf("%d", &a), a) {
    map<string, int> m;
    int sz = 0;
    ok = true;
    for (int i = 0; i < 300; i++) {
      UF[i] = -1;
      w[i] = 0;
    }
    for (int i = 0; i < a; i++) {
      scanf("%s%s", in, in);
      string A = in;
      int C = 0;
      scanf("%s%s", in, in);
      if (in[1] == 0)
        C = 0;
      else {
        sscanf(in, "%d^%d", &C, &C);
      }
      scanf("%s", in);
      string B = in;
      int p, q;
      if (!m.count(A)) {
        p = sz;
        m[A] = sz++;
      } else
        p = m[A];
      if (!m.count(B)) {
        q = sz;
        m[B] = sz++;
      } else
        q = m[B];
      UNION(p, q, C);
      //	if(!ok)printf("%d\\n",i);
    }
    if (ok)
      printf("Yes\\n");
    else
      printf("No\\n");
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <map>
#include <stdio.h>
#include <string>
using namespace std;
char in[30];
int UF[300];
int w[300];
pair<int, int> FIND(int a) {
  if (UF[a] < 0)
    return make_pair(a, 0);
  pair<int, int> ret = FIND(UF[a]);
  ret.second += w[a];
  return ret;
}
bool ok = true;
void UNION(int a, int b, int c) {
  pair<int, int> A = FIND(a);
  pair<int, int> B = FIND(b);
  int d = A.first;
  int e = B.first;
  int f = A.second + c - B.second;
  if (d == e) {
    if (B.second - c != A.second)
      ok = false;
    return;
  }
  UF[a] += UF[b];
  UF[b] = a;
  w[b] = f;
}
int main() {
  int a;
  while (scanf("%d", &a), a) {
    map<string, int> m;
    int sz = 0;
    ok = true;
    for (int i = 0; i < 300; i++) {
      UF[i] = -1;
      w[i] = 0;
    }
    for (int i = 0; i < a; i++) {
      scanf("%s%s", in, in);
      string A = in;
      int C = 0;
      scanf("%s%s", in, in);
      if (in[1] == 0)
        C = 0;
      else {
        sscanf(in, "%d^%d", &C, &C);
      }
      scanf("%s", in);
      string B = in;
      int p, q;
      if (!m.count(A)) {
        p = sz;
        m[A] = sz++;
      } else
        p = m[A];
      if (!m.count(B)) {
        q = sz;
        m[B] = sz++;
      } else
        q = m[B];
      UNION(p, q, C);
      //	if(!ok)printf("%d\\n",i);
    }
    if (ok)
      printf("Yes\\n");
    else
      printf("No\\n");
  }
}
Problem p01324 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
typedef pair<int, int> P;
map<string, int> M;
vector<P> e[301];
int C[301], used[301];

bool check(int ima, int cost) {
  int size = e[ima].size();
  if (used[ima])
    return (cost - C[ima]) == 0;
  used[ima] = 1, C[ima] = cost;
  for (int i = 0; i < size; i++)
    if (!check(e[ima][i].f, cost + e[ima][i].s))
      return 0;
  return 1;
}

int main() {
  int n;
  while (cin >> n, n) {
    ;
    for (int i = 0; i < 300; i++)
      e[i].clear();
    M.clear();

    int N = 0;
    for (int i = 0; i < n; i++) {
      string a, b, c, d, E;
      cin >> a >> b >> c >> d >> E;
      if (!M.count(b))
        M[b] = N++;
      if (!M.count(E))
        M[E] = N++;
      int num = 0, j = 3, flg = 1;
      if (d[3] == '-')
        flg = -1, j = 4;
      while (j < d.size())
        num = num * 10 + d[j++] - '0';
      e[M[b]].push_back(P(M[E], num * flg));
      e[M[E]].push_back(P(M[b], -num * flg));
    }

    int ans = 1;
    for (int i = 0; i < N; i++) {
      memset(used, 0, sizeof(used));
      memset(C, 0, sizeof(C));
      ans *= check(i, 0);
    }
    cout << (ans ? "Yes" : "No") << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
typedef pair<int, int> P;
map<string, int> M;
vector<P> e[101];
bool used[101];
int C[101];

bool check(int ima, int prev, int cost) {
  int size = e[ima].size();
  if (used[ima])
    return (cost - C[ima]) == 0;
  used[ima] = 1, C[ima] = cost;

  for (int i = 0; i < size; i++) {
    int nx = e[ima][i].f;
    if (nx != prev && !check(nx, ima, cost + e[ima][i].s))
      return 0;
  }
  return 1;
}

int main() {
  int n;
  while (cin >> n, n) {
    ;
    for (int i = 0; i < 100; i++)
      e[i].clear();
    M.clear();

    int N = 0;
    for (int i = 0; i < n; i++) {
      string a, b, c, d, E;
      cin >> a >> b >> c >> d >> E;
      if (!M.count(b))
        M[b] = N++;
      if (!M.count(E))
        M[E] = N++;
      int num = 0, j = 3, flg = 1;
      if (d[3] == '-')
        flg = -1, j = 4;
      while (j < d.size())
        num = num * 10 + d[j++] - '0';
      e[M[b]].push_back(P(M[E], num * flg));
      e[M[E]].push_back(P(M[b], -num * flg));
    }

    int ans = 1;
    for (int i = 0; i < N; i++) {
      memset(used, 0, sizeof(used));
      memset(C, 0, sizeof(C));
      ans *= check(i, -1, 0);
    }
    cout << (ans ? "Yes" : "No") << endl;
  }
  return 0;
}
Problem p01324 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define MP make_pair
#define X first
#define Y second

typedef pair<int, int> P;

int d[201][201];

const int INF = 1 << 29;
int main() {
  int n;
  while (cin >> n, n) {
    rep(i, 201) rep(j, 201) d[i][j] = INF;
    map<string, int> num;
    int idx = 0;
    bool ans = true;
    rep(i, n) {
      string a, b;
      string c;
      cin >> c >> a >> c >> c >> b;
      if (!num[a])
        num[a] = ++idx;
      if (!num[b])
        num[b] = ++idx;
      int x = atoi(c.substr(3).c_str());
      // a = b+x
      int l = num[a], r = num[b];
      d[l][l] = d[r][r] = 0;
      rep(k, 2) {
        d[l][r] = x;
        swap(l, r);
        x = -x;
      }
      for (int j = 1; j <= idx; j++) {
        for (int k = 1; k <= idx; k++) {
          if (d[k][j] == INF)
            continue;
          for (int l = 1; l <= idx; l++) {
            if (d[j][l] == INF)
              continue;
            d[k][l] = min(d[k][l], d[k][j] + d[j][l]);
          }
        }
      }
      for (int j = 1; j <= idx; j++) {
        if (d[j][j] != 0)
          ans = false;
      }
    }

    cout << (ans ? "Yes" : "No") << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define MP make_pair
#define X first
#define Y second

typedef pair<int, int> P;

int d[101][101];

const int INF = 1 << 29;
int main() {
  int n;
  while (cin >> n, n) {
    rep(i, 101) rep(j, 101) d[i][j] = INF;
    map<string, int> num;
    int idx = 0;
    bool ans = true;
    rep(i, n) {
      string a, b;
      string c;
      cin >> c >> a >> c >> c >> b;
      if (!num[a])
        num[a] = ++idx;
      if (!num[b])
        num[b] = ++idx;
      int x = atoi(c.substr(3).c_str());
      // a = b+x
      int l = num[a], r = num[b];
      rep(k, 2) {
        if (d[l][r] == INF) {
          d[l][r] = x;
        } else {
          if (d[l][r] != x)
            ans = false;
        }
        swap(l, r);
        x = -x;
      }
      for (int j = 1; j <= idx; j++) {
        for (int k = 1; k <= idx; k++) {
          if (d[k][j] == INF)
            continue;
          for (int l = 1; l <= idx; l++) {
            if (d[j][l] == INF)
              continue;
            d[k][l] = min(d[k][l], d[k][j] + d[j][l]);
          }
        }
      }
    }

    cout << (ans ? "Yes" : "No") << endl;
  }

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

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

map<string, int> done;
map<string, vector<pair<string, int>>> graph;
bool flag;

void dfs(string p, int cost) {
  if (done.find(p) != done.end()) {
    if (cost != done[p])
      flag = false;
    return;
  } else {
    done[p] = cost;
  }
  vector<pair<string, int>> r = graph[p];
  for (int i = 0; i < r.size(); i++) {
    dfs(r[i].first, cost + r[i].second);
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    graph.clear();
    vector<string> vertex;
    for (int i = 0; i < n; i++) {
      string a, b, c, d;
      cin >> d >> a >> d >> c >> b;
      c = c.substr(c.find("^") + 1);
      int cost = atoi(c.c_str());
      graph[b].push_back(make_pair(a, cost));
      graph[a].push_back(make_pair(b, -cost));
      vertex.push_back(a);
      vertex.push_back(b);
    }
    sort(vertex.begin(), vertex.end());
    vertex.erase(unique(vertex.begin(), vertex.end()), vertex.end());
    bool ans = true;
    for (int i = 0; i < n; i++) {
      done.clear();
      flag = true;
      dfs(vertex[i], 0);
      ans &= flag;
    }
    cout << (ans ? "Yes" : "No") << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

map<string, int> done;
map<string, vector<pair<string, int>>> graph;
bool flag;

void dfs(string p, int cost) {
  if (done.find(p) != done.end()) {
    if (cost != done[p])
      flag = false;
    return;
  } else {
    done[p] = cost;
  }
  vector<pair<string, int>> r = graph[p];
  for (int i = 0; i < r.size(); i++) {
    dfs(r[i].first, cost + r[i].second);
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    vector<string> vertex;
    for (int i = 0; i < n; i++) {
      string a, b, c, d;
      cin >> d >> a >> d >> c >> b;
      c = c.substr(c.find("^") + 1);
      int cost = atoi(c.c_str());
      graph[b].push_back(make_pair(a, cost));
      vertex.push_back(a);
      vertex.push_back(b);
    }
    sort(vertex.begin(), vertex.end());
    vertex.erase(unique(vertex.begin(), vertex.end()), vertex.end());
    bool ans = true;
    for (int i = 0; i < n; i++) {
      done.clear();
      flag = true;
      dfs(vertex[i], 0);
      ans &= flag;
    }
    cout << (ans ? "Yes" : "No") << endl;
  }
}
Problem p01324 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> v;
int n, a[1000], b[1000], c[1000];
string s1[1000], s2[1000], t[1000];
vector<int> u[1000];
int group[1000], dist[1000];
bool OK = true;
int s(string Y) { return lower_bound(v.begin(), v.end(), Y) - v.begin(); }

void merge(int a, int b, int c) {
  if (group[a] == group[b]) {
    if ((dist[a] - dist[b]) != c)
      OK = false;
    return;
  }
  int w1 = group[a], w2 = group[b];
  int sa = (dist[a] - dist[b]) - c;
  for (int i : u[w2])
    dist[i] += sa;
  for (int i : u[w2])
    u[w1].push_back(i);
  for (int i : u[w2])
    group[i] = w1;
  u[w2].clear();
  sort(u[w1].begin(), u[w1].end());
  return;
}
int main() {
  while (true) {
    cin >> n;
    v.clear();
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      string a1, a2;
      cin >> a1 >> s1[i] >> a2 >> t[i] >> s2[i];
      v.push_back(s1[i]);
      v.push_back(s2[i]);
    }
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    OK = true;
    int m = v.size();
    for (int i = 0; i < m; i++) {
      group[i] = i;
      u[i].clear();
      u[i].push_back(i);
    }
    for (int i = 0; i < n; i++) {
      a[i] = s(s1[i]);
      b[i] = s(s2[i]);
      c[i] = stoi(t[i].substr(3, t[i].size() - 3));
    }
    for (int i = 0; i < n; i++) {
      merge(a[i], b[i], c[i]);
    }
    if (OK == true)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> v;
int n, a[100], b[100], c[100];
string s1[100], s2[100], t[100];
vector<int> u[100];
int group[100], dist[100];
bool OK = true;
int s(string Y) { return lower_bound(v.begin(), v.end(), Y) - v.begin(); }

void merge(int a, int b, int c) {
  if (group[a] == group[b]) {
    if (dist[a] - dist[b] != c)
      OK = false;
  }
  int sa = (dist[a] - dist[b]) - c;
  for (int i : u[group[b]])
    dist[i] += sa;
  for (int i : u[group[b]])
    u[group[a]].push_back(i);
  int w = group[b];
  for (int i : u[w])
    group[i] = group[a];
  u[group[b]].clear();
}
int main() {
  while (true) {
    cin >> n;
    v.clear();
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      string a1, a2;
      cin >> a1 >> s1[i] >> a2 >> t[i] >> s2[i];
      v.push_back(s1[i]);
      v.push_back(s2[i]);
    }
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    OK = true;
    for (int i = 0; i < n; i++) {
      u[i].clear();
      u[i].push_back(i);
      group[i] = i;
      a[i] = s(s1[i]);
      b[i] = s(s2[i]);
      c[i] = stoi(t[i].substr(3, t[i].size() - 3));
    }
    for (int i = 0; i < n; i++) {
      merge(a[i], b[i], c[i]);
    }
    if (OK == true)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}
Problem p01325 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, tmp;

  while (1) {
    cin >> n;
    if (!n)
      break;

    vector<int> a, b;
    for (int i = 0; i < n; i++) {
      cin >> tmp;
      a.push_back(tmp);
    }
    sort(a.begin(), a.end());

    for (int i = 0; i < n; i++) {
      cin >> tmp;
      b.push_back(tmp);
    }
    sort(b.begin(), b.end());

    while (a.size() || b.size()) {
      bool f = false;
      while (a.size() && !a[0])
        a.erase(a.begin());
      while (b.size() && !b[0])
        b.erase(b.begin());
      if (a[(int)a.size() - 1] == (int)b.size()) {
        for (int i = 0; i < (int)b.size(); i++)
          b[i]--;
        a.erase(a.end() - 1);
        f = true;
      } else if (b[(int)b.size() - 1] == (int)a.size()) {
        for (int i = 0; i < (int)a.size(); i++)
          a[i]--;
        b.erase(b.end() - 1);
        f = true;
      }
      if (!f)
        break;
    }

    if (a.empty() && b.empty())
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}

Error: WA

Fixed submission:

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

int main() {
  int n, tmp;

  while (1) {
    cin >> n;
    if (!n)
      break;

    vector<int> a, b;
    for (int i = 0; i < n; i++) {
      cin >> tmp;
      if (tmp)
        a.push_back(tmp);
    }
    if (a.size())
      sort(a.begin(), a.end());

    for (int i = 0; i < n; i++) {
      cin >> tmp;
      if (tmp)
        b.push_back(tmp);
    }
    if (b.size())
      sort(b.begin(), b.end());

    while (a.size() || b.size()) {
      bool f = false;
      while (a.size() && !a[0])
        a.erase(a.begin());
      while (b.size() && !b[0])
        b.erase(b.begin());
      if (a[(int)a.size() - 1] == (int)b.size()) {
        for (int i = 0; i < (int)b.size(); i++)
          b[i]--;
        a.erase(a.end() - 1);
        f = true;
      } else if (b[(int)b.size() - 1] == (int)a.size()) {
        for (int i = 0; i < (int)a.size(); i++)
          a[i]--;
        b.erase(b.end() - 1);
        f = true;
      }
      if (!f)
        break;
    }

    if (a.empty() && b.empty())
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}