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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
using namespace std;
long long int inf = 10000000000;

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

int main() {
  long long int n, m;
  cin >> n >> m;

  vector<vector<long long int>> dist(n, vector<long long int>(n, inf));

  for (int i = 0; i < m; i++) {
    int s, t, d;
    cin >> s >> t >> d;
    dist[s][t] = d;
  }
  for (int i = 0; i < n; i++)
    dist[i][i] = 0;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (dist[j][i] == inf)
        continue;
      for (int k = 0; k < n; k++) {
        if (dist[i][k] == inf)
          continue;
        if (dist[j][k] > dist[j][i] + dist[i][k])
          dist[j][k] = dist[j][i] + dist[i][k];
      }
    }
  }

  for (int i = 0; i < n; i++) {
    if (dist[i][i] < 0) {
      cout << "NEGATIVE CYCLE" << endl;
      return 0;
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (dist[i][j] == inf)
        cout << "INF";
      else
        cout << dist[i][j];
      if (j == n - 1)
        cout << endl;
      else
        cout << " ";
    }
  }

  return 0;
}

// EOF

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
using namespace std;
long long int inf = 10000000000000;

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

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

  vector<vector<long long int>> dist(n, vector<long long int>(n, inf));

  for (int i = 0; i < m; i++) {
    int s, t, d;
    cin >> s >> t >> d;
    dist[s][t] = d;
  }
  for (int i = 0; i < n; i++)
    dist[i][i] = 0;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        dist[j][k] = min(dist[j][k], dist[j][i] + dist[i][k]);
      }
    }
  }

  for (int i = 0; i < n; i++) {
    if (dist[i][i] < 0) {
      cerr << dist[i][i] << endl;
      cout << "NEGATIVE CYCLE" << endl;
      return 0;
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (dist[i][j] == inf)
        cout << "INF";
      else
        cout << dist[i][j];
      if (j == n - 1)
        cout << endl;
      else
        cout << " ";
    }
  }

  return 0;
}

// EOF
Problem p02363 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>

#define Emax 9900
#define Vmax 100
#define Dmax (10000000)
#define INF (100000000000L)

using namespace std;

typedef unsigned int uint;
typedef long long int llint_t;

struct Edge {
  int from, to, cost;
};

Edge edges[Emax];
static llint_t d[Vmax][Vmax];
static int Vnum, Enum;

void warshall_floyd() {
  for (int i = 0; i < Vnum; i++) {
    for (int j = 0; j < Vnum; j++) {
      d[i][j] = INF;
    }
    d[i][i] = 0;
  }

  for (int i = 0; i < Enum; i++) {
    auto e = edges[i];
    d[e.from][e.to] = e.cost;
  }

  for (int k = 0; k < Vnum; k++) {
    for (int i = 0; i < Vnum; i++) {
      for (int j = 0; j < Vnum; j++) {
        if (d[i][k] == INF || d[k][j] == INF)
          continue;
        if (d[i][j] > d[i][k] + d[k][j]) {
          d[i][j] = d[i][k] + d[k][j];
        }
      }
    }
  }

  for (int i = 0; i < Vnum; i++) {
    if (d[i][i] < 0) {
      puts("NEGATIVE CYCLE");
      exit(0);
    }
  }
}

int main(int ac, char **av) {
  scanf("%d %d", &Vnum, &Enum);

  for (int i = 0; i < Enum; i++) {
    int s, t, d;
    scanf("%d %d %d", &s, &t, &d);
    Edge e;
    e.from = s;
    e.to = t;
    e.cost = d;
    edges[i] = e;
  }

  warshall_floyd();

  for (int i = 0; i < Vnum; i++) {
    for (int j = 0; j < Vnum; j++) {
      if (d[i][j] == INF) {
        printf("INF");
      } else {
        printf("%d", d[i][j]);
      }
      if (j != Vnum - 1) {
        printf(" ");
      } else {
        printf("\\n");
      }
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>

#define Emax 9900
#define Vmax 100
#define Dmax (10000000)
#define INF (1000000000L)

using namespace std;

typedef unsigned int uint;
typedef long long int llint_t;

struct Edge {
  int from, to, cost;
};

Edge edges[Emax];
static llint_t d[Vmax][Vmax];
static int Vnum, Enum;

void warshall_floyd() {
  for (int i = 0; i < Vnum; i++) {
    for (int j = 0; j < Vnum; j++) {
      d[i][j] = INF;
    }
    d[i][i] = 0;
  }

  for (int i = 0; i < Enum; i++) {
    auto e = edges[i];
    d[e.from][e.to] = e.cost;
  }

  for (int k = 0; k < Vnum; k++) {
    for (int i = 0; i < Vnum; i++) {
      for (int j = 0; j < Vnum; j++) {
        if (d[i][j] > d[i][k] + d[k][j]) {
          d[i][j] = d[i][k] + d[k][j];
        }
      }
    }
  }

  for (int i = 0; i < Vnum; i++) {
    if (d[i][i] < 0) {
      puts("NEGATIVE CYCLE");
      exit(1);
    }
  }
}

int main(int ac, char **av) {
  scanf("%d %d", &Vnum, &Enum);

  for (int i = 0; i < Enum; i++) {
    int s, t, d;
    scanf("%d %d %d", &s, &t, &d);
    Edge e;
    e.from = s;
    e.to = t;
    e.cost = d;
    edges[i] = e;
  }

  warshall_floyd();

  for (int i = 0; i < Vnum; i++) {
    for (int j = 0; j < Vnum; j++) {
      if (d[i][j] == INF) {
        printf("INF");
      } else {
        printf("%d", d[i][j]);
      }
      if (j != Vnum - 1) {
        printf(" ");
      } else {
        printf("\\n");
      }
    }
  }
  return 0;
}
Problem p02363 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repR(i, n) for (int i = (n)-1; i > -1; i--)
#define rep1(i, n) for (int i = 1; i < (int)(n + 1); i++)
#define rep1R(i, n) for (int i = (n); i > 0; i--)
#define ll long long
using namespace std;

const ll INF = (1LL << 32);
const int MAX = 100;
long long A[MAX + 1][MAX + 1];

void warshallFloyd(int V) {
  rep(k, V) rep(i, V) {
    if (A[i][k] == INF)
      continue;
    rep(j, V) {
      if (A[k][j] == INF)
        continue;
      A[i][j] = min(A[i][j], A[i][k] + A[k][j]);
    }
  }
}

int main() {
  int V, E;
  cin >> V >> E;

  rep(i, V) rep(j, V) { A[i][j] = (i == j) ? 0 : INF; }

  rep(i, E) {
    int s, t, d;
    cin >> s >> t >> d;
    A[s][t] = d;
  }

  warshallFloyd(V);

  rep(i, V) {
    if (A[i][i] < 0) {
      cout << "NEGATIVE CYCLE" << endl;
      return 0;
    }
  }

  rep(i, V) {
    rep(j, V) {
      if (A[i][j] != INF)
        cout << A[i][j];
      else
        cout << "INF";
      cout << ((j != V - 1) ? " " : "\\n");
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repR(i, n) for (int i = (n)-1; i > -1; i--)
#define rep1(i, n) for (int i = 1; i < (int)(n + 1); i++)
#define rep1R(i, n) for (int i = (n); i > 0; i--)
#define ll long long
using namespace std;

const ll INF = (1LL << 32);
const int MAX = 100;
long long A[MAX + 1][MAX + 1];

void warshallFloyd(int V) {
  rep(k, V) rep(i, V) rep(j, V) { A[i][j] = min(A[i][j], A[i][k] + A[k][j]); }
}

int main() {
  int V, E;
  cin >> V >> E;

  rep(i, V) rep(j, V) { A[i][j] = (i == j) ? 0 : INF; }

  rep(i, E) {
    int s, t, d;
    cin >> s >> t >> d;
    A[s][t] = d;
  }

  warshallFloyd(V);

  rep(i, V) rep(j, V) {
    if (A[i][j] < 0) {
      cout << "NEGATIVE CYCLE" << endl;
      return 1;
    }
  }

  rep(i, V) {
    rep(j, V) {
      if (A[i][j] != INF)
        cout << A[i][j];
      else
        cout << "INF";
      cout << ((j != V - 1) ? " " : "\\n");
    }
  }

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

#include <cstdint>
#include <iostream>
#include <vector>

using namespace std;

using s64 = int64_t;

constexpr s64 INF = 1'010'000'000'000'000'000LL;

template <typename T, typename U> bool chmin(T &xmin, const U &x) {
  if (x < xmin) {
    xmin = x;
    return true;
  } else {
    return false;
  }
}

#define FOR(i, start, end) for (s64 i = (start); i < (end); ++i)
#define REP(i, n) FOR(i, 0, n)

s64 N;
s64 M;
vector<vector<s64>> G;

void print_g(ostream &out) {
  REP(i, N) {
    REP(j, N) {
      s64 c = G[i][j];
      if (c == INF)
        out << "INF";
      else
        out << c;
      out << (j == N - 1 ? '\\n' : ' ');
    }
  }
}

bool floyd() {
  REP(k, N) {
    REP(i, N) {
      if (G[i][k] == INF)
        continue;
      REP(j, N) {
        if (G[k][j] == INF)
          continue;
        chmin(G[i][j], G[i][k] + G[k][j]);
        if (i == j && G[i][j] < 0)
          return false;
      }
    }
  }

  return true;
}

void solve() {
  if (!floyd()) {
    cout << "NEGATIVE CYCLE\\n";
    return;
  }

  print_g(cout);
}

int main() {
  cin >> N;
  G.assign(N, vector<s64>(N, INF));
  REP(i, N) { G[i][i] = 0; }

  cin >> M;
  REP(_, M) {
    s64 a, b, c;
    cin >> a >> b >> c;
    G[a][b] = c;
  }

  solve();

  return 0;
}

Error: 0
0
0
0
0


Fixed submission:

#include <cstdint>
#include <iostream>
#include <vector>

using namespace std;

using s64 = int64_t;

constexpr s64 INF = 1'010'000'000'000'000'000LL;

template <typename T, typename U> bool chmin(T &xmin, const U &x) {
  if (x < xmin) {
    xmin = x;
    return true;
  } else {
    return false;
  }
}

#define FOR(i, start, end) for (s64 i = (start); i < (end); ++i)
#define REP(i, n) FOR(i, 0, n)

s64 N;
s64 M;
vector<vector<s64>> G;

void print_g(ostream &out) {
  REP(i, N) {
    REP(j, N) {
      s64 c = G[i][j];
      if (c == INF)
        out << "INF";
      else
        out << c;
      out << (j == N - 1 ? '\\n' : ' ');
    }
  }
}

bool floyd() {
  REP(k, N) {
    REP(i, N) {
      if (G[i][k] == INF)
        continue;
      REP(j, N) {
        if (G[k][j] == INF)
          continue;
        chmin(G[i][j], G[i][k] + G[k][j]);
        if (i == j && G[i][j] < 0)
          return false;
      }
    }
  }

  return true;
}

void solve() {
  floyd();
  // print_g(cerr);
  REP(i, N) { cerr << G[i][i] << "\\n"; }

  REP(i, N) {
    if (G[i][i] < 0) {
      cout << "NEGATIVE CYCLE\\n";
      return;
    }
  }

  print_g(cout);
}

int main() {
  cin >> N;
  G.assign(N, vector<s64>(N, INF));
  REP(i, N) { G[i][i] = 0; }

  cin >> M;
  REP(_, M) {
    s64 a, b, c;
    cin >> a >> b >> c;
    G[a][b] = c;
  }

  solve();

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

#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;
struct side {
  int n;
  int m;
  int l;
};
side sides[100000];
void mergesort(int l, int r) {
  if (l == r)
    return;
  side temp;
  if (l == r - 1) {
    if (sides[l].l > sides[r].l) {
      temp = sides[l];
      sides[l] = sides[r];
      sides[r] = temp;
    }
    return;
  }
  int mid = (l + r) / 2, now = 0;
  side b[r - l + 1];
  mergesort(l, mid - 1);
  mergesort(mid, r);
  for (int i = 0; i <= r - l; i++) {
    if (sides[i + l - now].l < sides[mid + now].l) {
      b[i] = sides[i + l - now];
      if (i + l - now == mid - 1) {
        for (i++; i <= r - l; i++) {
          b[i] = sides[mid + now];
          now++;
        }
      }
    } else {
      b[i] = sides[mid + now];
      now++;
      if (mid + now > r) {
        for (i++; i <= r - l; i++)
          b[i] = sides[i + l - now];
      }
    }
  }
  for (int i = 0; i <= r - l; i++)
    sides[i + l] = b[i];
}
int main() {
  int n, m, a, ans = 0, c = 1;
  bool tree[10000];
  scanf("%d%d", &n, &m);
  if (m == 0) {
    printf("0\\n");
    return 0;
  }
  tree[0] = true;
  for (int i = 1; i < n; i++) {
    tree[i] = false;
  }
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &sides[i].n, &sides[i].m, &sides[i].l);
  }
  mergesort(0, m - 1);
  while (c < n) {
    a = 0;
    while (tree[sides[a].n] == tree[sides[a].m])
      a++;
    ans += sides[a].l;
    tree[sides[a].n] = tree[sides[a].m] = true;
    c++;
  }
  printf("%d\\n", ans);
}

Error: 0

Fixed submission:

#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;
struct side {
  int n;
  int m;
  int l;
};
side sides[100000];
void mergesort(int l, int r) {
  if (l == r)
    return;
  side temp;
  if (l == r - 1) {
    if (sides[l].l > sides[r].l) {
      temp = sides[l];
      sides[l] = sides[r];
      sides[r] = temp;
    }
    return;
  }
  int mid = (l + r) / 2, now = 0;
  side b[r - l + 1];
  mergesort(l, mid - 1);
  mergesort(mid, r);
  for (int i = 0; i <= r - l; i++) {
    if (sides[i + l - now].l < sides[mid + now].l) {
      b[i] = sides[i + l - now];
      if (i + l - now == mid - 1) {
        for (i++; i <= r - l; i++) {
          b[i] = sides[mid + now];
          now++;
        }
      }
    } else {
      b[i] = sides[mid + now];
      now++;
      if (mid + now > r) {
        for (i++; i <= r - l; i++)
          b[i] = sides[i + l - now];
      }
    }
  }
  for (int i = 0; i <= r - l; i++)
    sides[i + l] = b[i];
}
int main() {
  int n, m, a, ans = 0, c = 1;
  bool tree[10000];
  scanf("%d%d", &n, &m);
  tree[0] = true;
  for (int i = 1; i < n; i++) {
    tree[i] = false;
  }
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &sides[i].n, &sides[i].m, &sides[i].l);
  }
  mergesort(0, m - 1);
  while (c < n) {
    a = 0;
    while (tree[sides[a].n] == tree[sides[a].m])
      a++;
    ans += sides[a].l;
    tree[sides[a].n] = tree[sides[a].m] = true;
    c++;
  }
  printf("%d\\n", ans);
}
Problem p02364 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
#define MAX_E 100000
#define MAX_V 10000

struct edge {
  int u, v, cost;
};

int V, E;
edge es[MAX_E];
int par[MAX_V];
int r[MAX_V];

void init_union_find(int v) {
  for (int i = 0; i < v; i++) {
    par[i] = i;
    r[i] = 0;
  }
}

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);
  if (x == y)
    return;

  if (r[x] < r[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (r[x] == r[y])
      r[x]++;
  }
}

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

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

int kruskal() {
  sort(es, es + E, comp);
  init_union_find(V);
  int res = 0;
  for (int i = 0; i < E; i++) {
    edge e = es[i];
    if (!same(e.u, e.v)) {
      unite(e.u, e.v);
      res += e.cost;
    }
  }
  return res;
}

int main() {
  cin >> V >> E;

  for (int i = 0; i < E; i++) {
    int v1, v2, c;
    cin >> v1 >> v2 >> c;
    es[i].u = v1;
    es[i].v = v2;
    es[i].cost = c;
  }

  cout << kruskal() << endl;
}

Error: MLE

Fixed submission:

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

#define MAX_V 10000
#define INF 10001

int V, E;
int cost[MAX_V][MAX_V];
int mincost[MAX_V];
bool used[MAX_V];

void add_edge(int from, int to, int c) {
  cost[from][to] = c;
  cost[to][from] = c;
}

int prim() {
  for (int i = 0; i < V; i++) {
    mincost[i] = INF;
    used[i] = false;
  }

  mincost[0] = 0;
  int res = 0;

  while (true) {
    int v = -1;
    for (int u = 0; u < V; u++) {
      if (!used[u] && (v == -1 || mincost[u] < mincost[v]))
        v = u;
    }

    if (v == -1)
      break;
    used[v] = true;
    res += mincost[v];

    for (int u = 0; u < V; u++) {
      if (!used[u] && mincost[u] > cost[v][u])
        mincost[u] = cost[v][u];
    }
  }
  return res;
}

int main() {
  cin >> V >> E;

  for (int i = 0; i < V; i++) {
    for (int j = 0; j < V; j++) {
      cost[i][j] = INF;
    }
  }

  for (int i = 0; i < E; i++) {
    int v1, v2, c;
    cin >> v1 >> v2 >> c;
    add_edge(v1, v2, c);
  }

  cout << prim() << endl;
}
Problem p02364 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;
struct my {
  int ding, hou, key;
  bool operator<(const my &p) const { return key < p.key; }
};
int n;
vector<my> G;
int d[100000];
struct zhishu {
  vector<int> p, high;
  void make(int x) {
    p.resize(x, 0);
    high.resize(x, 0);
    for (int i = 0; i < x; i++) {
      high[i] = 0;
      p[i] = i;
    }
  }
  void unite(int x, int y) { link(findside(x), findside(y)); }
  bool same(int x, int y) { return findside(x) == findside(y); }
  void link(int x, int y) {
    if (high[x] > high[y])
      p[y] = x;
    else {
      p[x] = y;
      if (high[x] == high[y])
        high[y]++;
    }
  }
  int findside(int x) {
    if (x != p[x]) {
      p[x] = findside(p[x]);
    }
    return p[x];
  }
};
int main() {
  my ans;
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &ans.ding, &ans.hou, &ans.key);
    G.push_back(ans);
  }
  zhishu l;
  int sum = 0;
  sort(G.begin(), G.end());
  l.make(n + 1);
  for (int i = 0; i < G.size(); i++) {
    if (!l.same(G[i].ding, G[i].hou)) {
      sum += G[i].key;
      l.unite(G[i].ding, G[i].hou);
    }
  }
  printf("%d\\n", sum);
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
int N;
struct my {
  int ding, hou, cost;
  bool operator<(const my &q) const { return cost < q.cost; }
};
vector<my> G;
struct huzhi {
  vector<int> p, myrank;
  void make() {
    myrank.resize(N + 1, 0);
    myrank.resize(N + 1, 0);
    for (int i = 0; i < N; i++) {
      myrank[i] = 0;
      p[i] = i;
    }
  }
  bool same(int x, int y) { return findside(x) == findside(y); }
  void unite(int x, int y) { link(findside(x), findside(y)); }
  void link(int x, int y) {
    if (myrank[x] > myrank[y]) {
      p[y] = x;
    } else {
      p[x] = y;
      if (myrank[x] == myrank[y]) {
        myrank[y]++;
      }
    }
  }
  int findside(int x) {
    if (x != p[x]) {
      p[x] = findside(p[x]);
    }
    return p[x];
  }
};
int mmp() {
  int ans = 0;
  sort(G.begin(), G.end());
  huzhi l;
  l.make();
  for (int i = 0; i < G.size(); i++) {
    if (!l.same(G[i].ding, G[i].hou)) {
      ans += G[i].cost;
      l.unite(G[i].ding, G[i].hou);
    }
  }
  return ans;
}
int main() {
  my s;
  int m;
  scanf("%d%d", &N, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &s.ding, &s.hou, &s.cost);
    G.push_back(s);
  }
  printf("%d\\n", mmp());
  return 0;
}
Problem p02364 (C++) — buggy submission, verdict: Runtime Error

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int num = 10000;
const int INF = 2000000000;
class Disjointset {
public:
  vector<int> rank, p;
  Disjointset(int n) {
    rank.resize(n, 0);
    for (int i = 0; i < n; i++)
      p.push_back(i);
  }
  int find(int x) {
    if (x != p[x])
      return p[x] = find(p[x]);
    else
      return x;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  void unite(int x, int y) {
    int px = find(x);
    int py = find(y);
    if (rank[px] > rank[py]) {
      p[py] = px;
    } else {
      p[px] = py;
      if (rank[px] == rank[py])
        rank[py]++;
    }
  }
};
class Edge {
public:
  int fr, to, co;
  Edge(int f, int t, int c) : fr(f), to(t), co(c) {}
  bool operator<(const Edge &e) const { return co > e.co; }
};
int main() {
  int n, e;
  cin >> n >> e;
  priority_queue<Edge> q;
  for (int i = 0; i < e; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    Edge ed(s, t, w);
    q.push(ed);
  }
  Disjointset d(n);
  int sum = 0;
  while (!q.empty()) {
    Edge e = q.top();
    q.pop();
    if (d.same(e.fr, e.to))
      continue;
    else {
      d.unite(e.fr, e.to);
      sum += e.co;
    }
  }
  cout << sum << endl;
  return 0;
}

Error: -11

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int num = 10000;
const int INF = 2000000000;
class Disjointset {
public:
  vector<int> rank, p;
  Disjointset(int n) {
    rank.resize(n, 0);
    for (int i = 0; i < n; i++)
      p[i] = i;
  }
  int find(int x) {
    if (x != p[x])
      return p[x] = find(p[x]);
    else
      return x;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  void unite(int x, int y) {
    int px = find(x);
    int py = find(y);
    if (rank[px] > rank[py]) {
      p[py] = px;
    } else {
      p[px] = py;
      if (rank[px] == rank[py])
        rank[py]++;
    }
  }
};
class Edge {
public:
  int fr, to, co;
  Edge(int f, int t, int c) : fr(f), to(t), co(c) {}
  bool operator<(const Edge &e) const { return co > e.co; }
};
int main() {
  int n;
  cin >> n;
  priority_queue<Edge> q;
  for (int i = 0; i < n; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    Edge ed(s, t, w);
    q.push(ed);
  }
  Disjointset d(n);
  int sum = 0;
  while (!q.empty()) {
    Edge e = q.top();
    q.pop();
    if (d.same(e.fr, e.to))
      continue;
    else {
      d.unite(e.fr, e.to);
      sum += e.co;
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02364 (C++) — buggy submission, verdict: Runtime Error

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_3_A
#include <climits>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int main(int arg, char **argv) {
  int V, E;
  cin >> V >> E;
  vector<vector<pair<int, int>>> A(V);
  for (int i = 0; i < E; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    A[s].push_back({w, t});
    A[t].push_back({w, s});
  }
  vector<bool> U(V);
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  q.push({0, 0});
  int sum = 0;
  while (!q.empty()) {
    auto c = q.top();
    q.pop();
    if (U[c.second])
      continue;
    U[c.second] = true;
    sum += c.first;
    for (auto &a : A[c.second])
      q.push(a);
  }
  cout << sum << endl;
  return 0;
}

Error: 0

Fixed submission:

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_3_A
#include <climits>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main(int arg, char **argv) {
  int V, E;
  cin >> V >> E;
  vector<vector<pair<int, int>>> A(E);
  for (int i = 0; i < E; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    A[s].push_back({w, t});
    A[t].push_back({w, s});
  }
  vector<bool> U(V);
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  q.push({0, 0});
  int sum = 0;
  while (!q.empty()) {
    auto c = q.top();
    q.pop();
    if (U[c.second])
      continue;
    U[c.second] = true;
    sum += c.first;
    for (auto &a : A[c.second])
      q.push(a);
  }
  cout << sum << endl;
  return 0;
}