alexjercan/bugnet huggingface dataset:
Problem p00179 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 30;

char hage[] = {'r', 'g', 'b'};
char hoge(char a, char b) {
  REP(i, 3)
  if (a != hage[i] && b != hage[i])
    return hage[i];
}
typedef pair<int, string> pis;

int bfs(string s) {
  int res = INF;
  queue<pis> Q;
  map<string, bool> ma;
  Q.push(pis(0, s));
  while (!Q.empty()) {
    pis p = Q.front();
    Q.pop();
    string ss = p.second;
    if (ma[ss])
      continue;
    ma[ss] = true;
    //    cout << p.first << " " <<ss << endl;
    bool f = true;
    REP(i, ss.length() - 1)
    if (ss[i] != ss[i + 1])
      f = false;
    if (f)
      return p.first;
    REP(i, ss.length() - 1) {
      if (ss[i] != ss[i + 1]) {
        string sss = ss;
        sss[i] = sss[i + 1] = hoge(ss[i], ss[i + 1]);
        Q.push(pis(p.first + 1, sss));
      }
    }
  }
  return -1;
}

int main() {
  string s;
  while (cin >> s, s != "0") {
    int ans = bfs(s);
    if (ans >= 0)
      cout << ans << endl;
    else
      cout << "NA" << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 30;

char hage[] = {'r', 'g', 'b'};
char hoge(char a, char b) {
  REP(i, 3)
  if (a != hage[i] && b != hage[i])
    return hage[i];
}
typedef pair<int, string> pis;

int bfs(string s) {
  int res = INF;
  queue<pis> Q;
  Q.push(pis(0, s));
  while (!Q.empty()) {
    pis p = Q.front();
    Q.pop();
    if (p.first > s.length())
      continue;
    string ss = p.second;
    //    cout << p.first << " " <<ss << endl;
    bool f = true;
    REP(i, ss.length() - 1)
    if (ss[i] != ss[i + 1])
      f = false;
    if (f)
      return p.first;
    REP(i, ss.length() - 1) {
      if (ss[i] != ss[i + 1]) {
        string sss = ss;
        sss[i] = sss[i + 1] = hoge(ss[i], ss[i + 1]);
        Q.push(pis(p.first + 1, sss));
      }
    }
  }
  return -1;
}

int main() {
  string s;
  while (cin >> s, s != "0") {
    int ans = bfs(s);
    if (ans >= 0)
      cout << ans << endl;
    else
      cout << "NA" << endl;
  }
}
Problem p00179 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

typedef pair<string, int> P;

#define mp(a, b) make_pair(a, b)

char diff(char a, char b) {
  if (a != 'r' && b != 'r')
    return 'r';
  if (a != 'g' && b != 'g')
    return 'g';
  if (a != 'b' && b != 'b')
    return 'b';
  return 'a';
}

bool check(string str) {
  char c = str[0];
  for (int i = 0; i < str.length(); i++) {
    if (c != str[i])
      return false;
  }

  return true;
}

int main(void) {
  string str;

  while (cin >> str) {
    if (str == "0")
      return 0;

    int ret = -1, a;
    queue<P> q;
    map<string, bool> memo;
    q.push(mp(str, 0));
    while (!q.empty()) {
      P now = q.front();
      q.pop();
      if (memo[now.first]) {
        continue;
      }
      memo[now.first] = true;
      if (check(now.first)) {
        ret = now.second;
        break;
      }
      for (int i = 0; i < now.first.length() - 1; i++) {
        if (now.first[i] != now.first[i + 1]) {
          string tmp = now.first;
          tmp[i] = diff(now.first[i], now.first[i + 1]);
          tmp[i + 1] = diff(now.first[i], now.first[i + 1]);
          q.push(mp(tmp, now.second + 1));
        }
      }
    }

    if (ret >= 0)
      cout << ret << endl;
    else
      cout << "NA" << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>

using namespace std;

typedef pair<string, int> P;

#define mp(a, b) make_pair(a, b)

char diff(char a, char b) {
  if (a != 'r' && b != 'r')
    return 'r';
  if (a != 'g' && b != 'g')
    return 'g';
  if (a != 'b' && b != 'b')
    return 'b';
  return 'a';
}

bool check(string str) {
  char c = str[0];
  for (int i = 0; i < str.length(); i++) {
    if (c != str[i])
      return false;
  }

  return true;
}

int main(void) {
  string str;

  while (cin >> str) {
    if (str == "0")
      return 0;

    int ret = -1, a;
    queue<P> q;
    q.push(mp(str, 0));
    while (!q.empty()) {
      P now = q.front();
      q.pop();
      if (check(now.first)) {
        ret = now.second;
        break;
      }
      if (q.size() > 800000) {
        break;
      }
      for (int i = 0; i < now.first.length() - 1; i++) {
        if (now.first[i] != now.first[i + 1]) {
          string tmp = now.first;
          tmp[i] = diff(now.first[i], now.first[i + 1]);
          tmp[i + 1] = diff(now.first[i], now.first[i + 1]);
          q.push(mp(tmp, now.second + 1));
        }
      }
    }

    if (ret >= 0)
      cout << ret << endl;
    else
      cout << "NA" << endl;
  }

  return 0;
}
Problem p00179 (C++) — buggy submission, verdict: Memory Limit Exceeded

// 57
#include <iostream>
#include <queue>
#include <set>
#include <utility>
#include <vector>

using namespace std;

int main() {
  for (char w[11]; cin >> w, w[0] != '0';) {
    queue<pair<vector<int>, int>> que;
    vector<int> in;
    for (int i = 0; w[i]; i++) {
      int n = (w[i] == 'r') ? 1 : (w[i] == 'g') ? 2 : 4;
      in.push_back(n);
    }
    que.push(make_pair(in, 0));
    set<vector<int>> s;
    while (!que.empty()) {
      vector<int> v = que.front().first;
      if (s.find(v) != s.end()) {
        que.pop();
      } else {
        s.insert(v);
        int c = 0;
        for (int i = 0; i < v.size(); i++) {
          c |= v[i];
        }
        if ((c & (c - 1)) == 0)
          break;
        for (int i = 0; i < v.size() - 1; i++) {
          if (v[i] != v[i + 1]) {
            vector<int> n = v;
            int nc = 7 ^ v[i] ^ v[i + 1];
            n[i] = n[i + 1] = nc;
            que.push(make_pair(n, que.front().second + 1));
          }
        }
        que.pop();
      }
    }
    if (que.empty()) {
      cout << "NA" << endl;
    } else {
      cout << que.front().second << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

// 57
#include <iostream>
#include <queue>
#include <set>
#include <utility>
#include <vector>

using namespace std;

int main() {
  for (char w[11]; cin >> w, w[0] != '0';) {
    queue<pair<vector<int>, int>> que;
    vector<int> in;
    for (int i = 0; w[i]; i++) {
      int n = (w[i] == 'r') ? 1 : (w[i] == 'g') ? 2 : 4;
      in.push_back(n);
    }
    que.push(make_pair(in, 0));
    set<vector<int>> s;
    while (!que.empty()) {
      int c = 0;
      vector<int> v = que.front().first;
      if (s.find(v) != s.end()) {
        que.pop();
      } else {
        for (int i = 0; i < v.size(); i++) {
          c |= v[i];
        }
        if ((c & (c - 1)) == 0)
          break;
        for (int i = 0; i < v.size() - 1; i++) {
          vector<int> n = v;
          int nc = 7 ^ v[i] ^ v[i + 1];
          n[i] = n[i + 1] = nc;
          if (s.find(n) == s.end()) {
            que.push(make_pair(n, que.front().second + 1));
          }
        }
        que.pop();
      }
    }
    if (que.empty()) {
      cout << "NA" << endl;
    } else {
      cout << que.front().second << endl;
    }
  }
  return 0;
}
Problem p00179 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  while (true) {
    string s;
    cin >> s;
    if (s == "0")
      break;
    string sr(s.size(), 'r');
    string sg(s.size(), 'g');
    string sb(s.size(), 'b');
    map<string, int> mp;
    queue<string> q;
    q.push(s);
    mp[s] = 0;
    int f = 0;
    if (s == sr || s == sb || s == sg) {
      cout << 0 << endl;
    } else {
      // for(int i=0;i<1e6;i++){
      while (!q.empty()) {
        string x = q.front();
        q.pop();
        for (int j = 0; j < s.size() - 1; j++) {
          string y = x;
          if (y[j] == 'r' && y[j + 1] == 'g') {
            y[j] = 'b';
            y[j + 1] = 'b';
          } else if (y[j] == 'b' && y[j + 1] == 'g') {
            y[j] = 'r';
            y[j + 1] = 'r';
          } else if (y[j] == 'r' && y[j + 1] == 'b') {
            y[j] = 'g';
            y[j + 1] = 'g';
          } else if (y[j] == 'g' && y[j + 1] == 'b') {
            y[j] = 'r';
            y[j + 1] = 'r';
          } else if (y[j] == 'g' && y[j + 1] == 'r') {
            y[j] = 'b';
            y[j + 1] = 'b';
          } else if (y[j] == 'b' && y[j + 1] == 'r') {
            y[j] = 'g';
            y[j + 1] = 'g';
          }
          // cout<<mp[x]+1<<" "<<y<<endl;
          if (y == sr || y == sb || y == sg) {
            cout << mp[x] + 1 << endl;
            f = 1;
            break;
          }
          if (mp.count(y) == 0) {
            mp[y] = mp[x] + 1;
            q.push(y);
          }
        }
        if (f)
          break;
      }
      if (!f)
        cout << "NA" << endl;
    }
  }
}


Error: WA

Fixed submission:

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

int main() {
  while (true) {
    string s;
    cin >> s;
    if (s[0] == '0')
      break;
    string sr(s.size(), 'r');
    string sg(s.size(), 'g');
    string sb(s.size(), 'b');
    map<string, int> mp;
    queue<string> q;
    q.push(s);
    mp[s] = 0;
    int f = 0;
    for (int i = 0; i < 1e3; i++) {
      string x = q.front();
      q.pop();
      for (int j = 0; j < s.size() - 1; j++) {
        string y = x;
        if (y[j] == 'r' && y[j + 1] == 'g') {
          y[j] = 'b';
          y[j + 1] = 'b';
        }
        if (y[j] == 'b' && y[j + 1] == 'g') {
          y[j] = 'r';
          y[j + 1] = 'r';
        }
        if (y[j] == 'r' && y[j + 1] == 'b') {
          y[j] = 'g';
          y[j + 1] = 'g';
        }
        if (y[j] == 'g' && y[j + 1] == 'b') {
          y[j] = 'r';
          y[j + 1] = 'r';
        }
        if (y[j] == 'g' && y[j + 1] == 'r') {
          y[j] = 'b';
          y[j + 1] = 'b';
        }
        if (y[j] == 'b' && y[j + 1] == 'r') {
          y[j] = 'g';
          y[j + 1] = 'g';
        }
        // cout<<mp[x]+1<<" "<<y<<endl;
        if (y == sr || y == sb || y == sg) {
          cout << mp[x] + 1 << endl;
          f = 1;
          break;
        }
        if (mp[y] == 0) {
          mp[y] = mp[x] + 1;
          q.push(y);
        }
      }
      if (f)
        break;
    }
    if (!f)
      cout << "NA" << endl;
  }
}
Problem p00180 (C++) — buggy submission, verdict: Runtime Error

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

struct Dset {
  Dset *parent;
  int rank;

  Dset() { clear(); }

  void clear() {
    parent = NULL;
    rank = 0;
  }

  Dset *find() {
    if (!parent) {
      return this;
    } else {
      parent = parent->find();
      return parent;
    }
  }

  bool merge(Dset *a) {
    Dset *myRoot = find();
    Dset *yourRoot = a->find();
    if (myRoot->rank < yourRoot->rank) {
      myRoot->parent = yourRoot;
    } else if (myRoot->rank > yourRoot->rank) {
      yourRoot->parent = myRoot;
    } else if (myRoot != yourRoot) {
      yourRoot->parent = myRoot;
      myRoot->rank++;
    } else {
      return false;
    }
    return true;
  }
};

struct Bridge {
  int a;
  int b;
  int cost;

  bool operator<(const Bridge &obj) const { return (cost < obj.cost); }
};

int main() {
  int n, m, ans;
  Bridge bridge[6000];
  Dset x[100];
  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;
    for (int i = 0; i < m; i++) {
      cin >> bridge[i].a >> bridge[i].b >> bridge[i].cost;
    }
    sort(bridge, bridge + m);
    for (int i = 0; i < n; i++) {
      x[i].clear();
    }
    ans = 0;
    for (int i = 0; i < m; i++) {
      if (x[bridge[i].a].merge(&x[bridge[i].b])) {
        ans += bridge[i].cost;
      }
    }
    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

struct Dset {
  Dset *parent;
  int rank;

  Dset() { clear(); }

  void clear() {
    parent = NULL;
    rank = 0;
  }

  Dset *find() {
    if (!parent) {
      return this;
    } else {
      parent = parent->find();
      return parent;
    }
  }

  bool merge(Dset *a) {
    Dset *myRoot = find();
    Dset *yourRoot = a->find();
    if (myRoot->rank < yourRoot->rank) {
      myRoot->parent = yourRoot;
    } else if (myRoot->rank > yourRoot->rank) {
      yourRoot->parent = myRoot;
    } else if (myRoot != yourRoot) {
      yourRoot->parent = myRoot;
      myRoot->rank++;
    } else {
      return false;
    }
    return true;
  }
};

struct Bridge {
  int a;
  int b;
  int cost;

  bool operator<(const Bridge &obj) const { return (cost < obj.cost); }
};

int main() {
  int n, m, ans;
  Bridge bridge[6000];
  Dset x[100];
  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;
    for (int i = 0; i < m; i++) {
      cin >> bridge[i].a >> bridge[i].b >> bridge[i].cost;
    }
    sort(bridge, bridge + m);
    for (int i = 0; i < m; i++) {
      x[i].clear();
    }
    ans = 0;
    for (int i = 0; i < m; i++) {
      if (x[bridge[i].a].merge(&x[bridge[i].b])) {
        ans += bridge[i].cost;
      }
    }
    cout << ans << endl;
  }

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

// kruskal tree
#include <algorithm>
#include <iostream>
using namespace std;
#define M 9999
int parent[M], a[M], b[M];
pair<int, int> node[M];
int root(int a) {
  if (parent[a] == a)
    return a;
  return parent[a] = root(parent[a]);
}
int unite(int a, int b) {
  if (root(a) == root(b))
    return 0;
  parent[root(a)] = root(b);
  return 1;
}
int main() {
  int i, s, n, m;
  for (; cin >> n >> m, n; cout << s << endl) {
    for (i = 0; i < m; i++)
      cin >> a[i] >> b[i] >> node[i].first, node[i].second = i;
    sort(node, node + m);
    for (i = 0; i < n; i++)
      parent[i] = i;
    for (s = i = 0; i < m; i++)
      if (unite(a[node[i].second], b[node[i].second]))
        s += node[i].first;
  }
}

Error: 0

Fixed submission:

// kruskal tree
#include <algorithm>
#include <iostream>
using namespace std;
#define M 101
int parent[M], a[M], b[M];
pair<int, int> node[M];
int root(int a) {
  if (parent[a] == a)
    return a;
  return parent[a] = root(parent[a]);
}
int unite(int a, int b) {
  if (root(a) == root(b))
    return 0;
  parent[root(a)] = root(b);
  return 1;
}
int main() {
  int i, s, n, m;
  for (; cin >> n >> m, n; cout << s << endl) {
    for (i = 0; i < m; i++)
      cin >> a[i] >> b[i] >> node[i].first, node[i].second = i;
    sort(node, node + m);
    for (i = 0; i < n; i++)
      parent[i] = i;
    for (s = i = 0; i < m; i++)
      if (unite(a[node[i].second], b[node[i].second]))
        s += node[i].first;
  }
}
Problem p00180 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int br[100][100];
  bool f[100];
  int tr[100];
  int n, m;

  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++)
        br[i][j] = -1;
      tr[i] = INT_MAX;
      f[i] = 0;
    }
    for (int k = 0; k < m; k++) {
      int a, b, cost;
      cin >> a >> b >> cost;
      br[a][b] = cost;
      br[b][a] = cost;
    }
    int comin = 0, r, trmin;
    tr[0] = 0;

    for (int k = 0; k < n; k++) {
      trmin = INT_MAX;
      for (int i = 0; i < n; i++)
        if (f[i] == 0 && trmin > tr[i])
          r = i, trmin = tr[i];
      // cout << trmin<<endl;
      comin += trmin;
      f[r] = 1;
      for (int i = 0; i < n; i++) {
        if (f[i] == 0 && tr[i] > br[r][i] && br[r][i] != -1)
          tr[i] = br[r][i];
      }
    }
    cout << comin << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int br[100][100];
  bool f[100];
  int tr[100];
  int n, m;

  while (1) {
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++)
        br[i][j] = -1;
      tr[i] = INT_MAX;
      f[i] = 0;
    }
    for (int k = 0; k < m; k++) {
      int a, b, cost;
      cin >> a >> b >> cost;
      br[a][b] = cost;
      br[b][a] = cost;
    }
    int comin = 0, r, trmin;
    tr[0] = 0;

    for (int k = 0; k < n; k++) {
      trmin = INT_MAX;
      for (int i = 0; i < n; i++)
        if (f[i] == 0 && trmin > tr[i])
          r = i, trmin = tr[i];
      // cout << trmin<<endl;
      comin += trmin;
      f[r] = 1;
      for (int i = 0; i < n; i++) {
        if (f[i] == 0 && tr[i] > br[r][i] && br[r][i] != -1)
          tr[i] = br[r][i];
      }
    }
    cout << comin << endl;
  }

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

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

using namespace std;

int n, m;
vector<set<pair<int, int>>> cost(200, set<pair<int, int>>());

int prim() {
  int res = 0;
  set<int> v;
  v.insert(0);
  while (v.size() < n) {
    pair<int, int> mini(100000007, -1);
    for (set<int>::iterator it = v.begin(); it != v.end(); it++) {
      for (set<pair<int, int>>::iterator it2 = cost[*it].begin();
           it2 != cost[*it].end(); it2++)
        if (v.find((*it2).second) == v.end()) {
          if (mini.first > (*it2).first)
            mini = (*it2);
          break;
        }
    }
    v.insert(mini.second);
    res += mini.first;
  }
  return res;
}

int main() {
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < m; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      cost[a].insert(pair<int, int>(c, b));
      cost[b].insert(pair<int, int>(c, a));
    }
    cout << prim() << endl;
    for (int i = 0; i < m; i++)
      cost[i].clear();
  }
  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

int n, m;
vector<set<pair<int, int>>> cost(100, set<pair<int, int>>());

int prim() {
  int res = 0;
  set<int> v;
  v.insert(0);
  while (v.size() < n) {
    pair<int, int> mini(100000007, -1);
    for (set<int>::iterator it = v.begin(); it != v.end(); it++) {
      for (set<pair<int, int>>::iterator it2 = cost[*it].begin();
           it2 != cost[*it].end(); it2++)
        if (v.find((*it2).second) == v.end()) {
          if (mini.first > (*it2).first)
            mini = (*it2);
          break;
        }
    }
    v.insert(mini.second);
    res += mini.first;
  }
  return res;
}

int main() {
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < m; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      cost[a].insert(pair<int, int>(c, b));
      cost[b].insert(pair<int, int>(c, a));
    }
    cout << prim() << endl;
    for (int i = 0; i < m; i++)
      cost[i].clear();
  }
  return 0;
}
Problem p00180 (C++) — buggy submission, verdict: Runtime Error

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

int n, m, sum, r;
int P[110], a[10010], b[10010], cost[10010];
pair<int, int> bridge[10010];
void init(int N) {
  for (int i = 0; i < N; ++i)
    P[i] = i;
}
int root(int a) {
  if (P[a] == a)
    return a;
  return (P[a] = root(P[a]));
}
void unite(int a, int b) { P[root(a)] = root(b); }

int main() {
  cin >> n >> m;
  while (n != 0 && m != 0) {
    for (int i = 0; i < m; ++i) {
      cin >> a[i] >> b[i] >> cost[i];
      bridge[i].first = cost[i];
      bridge[i].second = i;
    }
    sort(bridge, bridge + m);
    init(n);
    for (int i = 0; i < m; ++i) {
      int br2 = bridge[i].second;
      if (P[root(b[br2])] != P[root(a[br2])]) {
        unite(a[br2], b[br2]);
        sum += bridge[i].first;
      }
    }
    cout << sum << endl;
    cin >> n >> m;
    sum = 0;
  }
}

Error: 0

Fixed submission:

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

int n, m, sum;
int P[110], a[10010], b[10010], cost[10010];
pair<int, int> bridge[10010];
void init(int N) {
  for (int i = 0; i < N; ++i)
    P[i] = i;
}
int root(int a) {
  if (P[a] == a)
    return a;
  return (P[a] = root(P[a]));
}

int main() {
  cin >> n >> m;
  while (n != 0 && m != 0) {
    for (int i = 0; i < m; ++i) {
      cin >> a[i] >> b[i] >> cost[i];
      bridge[i].first = cost[i];
      bridge[i].second = i;
    }
    sort(bridge, bridge + m);
    init(n);
    for (int i = 0; i < m; ++i) {
      if (P[b[bridge[i].second]] == b[bridge[i].second]) {
        if (P[a[bridge[i].second]] != b[bridge[i].second]) {
          P[b[bridge[i].second]] = a[bridge[i].second];
          root(b[bridge[i].second]);
          sum += bridge[i].first;
        }
      }
    }
    cout << sum << endl;
    cin >> n >> m;
    sum = 0;
  }
}
Problem p00180 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF 114514810
#define MOD 1000000007
#define ALL(a) (a).begin(), (a).end()
#define pb(x) push_back(x)
#define ms(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge {
  int from, to, cost;
};

int par[1000];

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

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  par[x] = y;
}

bool same(int x, int y) { return find(x) == find(y); }

void init_UFT(int n) {
  REP(i, n) { par[i] = i; }
}

bool comp(const edge &e1, const edge &e2) { return e1.cost < e2.cost; }

vector<edge> es;
int main() {
  int n, m;
  while (cin >> n >> m, n) {
    init_UFT(n);
    es.clear();
    int a, b, c;
    REP(i, m) {
      cin >> a >> b >> c;
      edge e = {a, b, c};
      es.push_back(e);
    }
    sort(ALL(es), comp);
    int sum = 0;
    REP(i, es.size()) {
      if (!same(es[i].from, es[i].to)) {
        unite(es[i].from, es[i].to);
        sum += es[i].cost;
      }
    }
    cout << sum << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF 114514810
#define MOD 1000000007
#define ALL(a) (a).begin(), (a).end()
#define pb(x) push_back(x)
#define ms(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge {
  int from, to, cost;
};

int par[10000];
int hukasa[10000];

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

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  par[x] = y;
}

bool same(int x, int y) { return find(x) == find(y); }

void init_UFT(int n) {
  REP(i, n) { par[i] = i; }
}

bool comp(const edge &e1, const edge &e2) { return e1.cost < e2.cost; }

vector<edge> es;
int main() {
  int n, m;
  while (cin >> n >> m, n) {
    init_UFT(n);
    es.clear();
    int a, b, c;
    REP(i, m) {
      cin >> a >> b >> c;
      edge e = {a, b, c};
      es.push_back(e);
    }
    sort(ALL(es), comp);
    int sum = 0;
    REP(i, es.size()) {
      if (!same(es[i].from, es[i].to)) {
        unite(es[i].from, es[i].to);
        sum += es[i].cost;
      }
    }
    cout << sum << endl;
  }

  return 0;
}