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

// #include "bits/stdc++.h"
// using namespace std;
// typedef long long ll;
// typedef pair<int,int> pii;
// #define rep(i,n) for(ll i=0;i<(ll)(n);i++)
// #define all(a)  (a).begin(),(a).end()
// #define pb push_back
// #define INF 999999999
//

#include "bits/stdc++.h"
using namespace std;
typedef pair<int, int> pii;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define MAX_N 10001
#define INF 999999999

vector<pii> G[MAX_N];

int prim(int n) { // n : ???????????°
  bool used[MAX_N];
  int mincost[MAX_N];

  rep(i, n) {
    mincost[i] = INF;
    used[i] = false;
  }
  mincost[0] = 0;
  int res = 0;

  while (1) {
    int v = -1;
    rep(u, n) {
      if (used[u] == false && (v == -1 || mincost[u] < mincost[v]))
        v = u;
    }
    if (v == -1)
      break;
    used[v] = true;
    res += mincost[v];

    rep(i, G[v].size()) {
      int u = G[v][i].first;
      mincost[u] = min(mincost[u], G[v][i].second);
    }
  }
  return res;
}

int main() {
  int v, e;
  cin >> v >> e;
  rep(i, e) {
    int a, b, c;
    cin >> a >> b >> c;
    G[a].push_back(pii(b, c));
    G[b].push_back(pii(a, c));
  }
  cout << prim(v) << endl;
}

Error: MLE

Fixed submission:

// #include "bits/stdc++.h"
// using namespace std;
// typedef long long ll;
// typedef pair<int,int> pii;
// #define rep(i,n) for(ll i=0;i<(ll)(n);i++)
// #define all(a)  (a).begin(),(a).end()
// #define pb push_back
// #define INF 999999999
//

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define MAX_N 10001
#define INF 999999999

int edge[MAX_N][MAX_N];

int prim(int n) { // n : ???????????°
  bool used[MAX_N];
  int mincost[MAX_N];

  rep(i, n) {
    mincost[i] = INF;
    used[i] = false;
  }
  mincost[0] = 0;
  int res = 0;

  while (1) {
    int v = -1;
    rep(u, n) {
      if (used[u] == false && (v == -1 || mincost[u] < mincost[v]))
        v = u;
    }
    if (v == -1)
      break;
    used[v] = true;
    res += mincost[v];

    rep(u, n) { mincost[u] = min(mincost[u], edge[v][u]); }
  }
  return res;
}

int main() {
  rep(i, MAX_N) rep(j, MAX_N) edge[i][j] = INF;
  int v, e;
  cin >> v >> e;
  rep(i, e) {
    int a, b, c;
    cin >> a >> b >> c;
    edge[a][b] = edge[b][a] = c;
  }
  cout << prim(v) << endl;
}
Problem p02364 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

#define EMAX 100000
#define NOT -1

typedef long long LL;

/////Union-Find
LL par[EMAX];
LL rnk[EMAX];

// n要素で初期化
void UNinit(LL n) {
  for (LL i = 0; i <= n; i++) {
    par[i] = i;
    rnk[i] = 0;
  }
}

// 木の根を求める
LL UNfind(LL x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = UNfind(par[x]);
  }
}

// xとyの属する集合を併合
void UNunite(LL x, LL y) {
  x = UNfind(x);
  y = UNfind(y);
  if (x == y)
    return;
  if (rnk[x] < rnk[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (rnk[x] == rnk[y])
      rnk[x]++;
  }
}

// xとyが同じ集合に属するか否か
bool UNsame(LL x, LL y) { return UNfind(x) == UNfind(y); }

struct ed {
  int u, v, co;
};

bool comp(const ed &e1, const ed &e2) { return e1.co < e2.co; }

int main(void) {

  int V, E;
  int s, t, w;
  ed es[EMAX];

  cin >> V >> E;

  for (int i = 0; i < E; i++) {
    cin >> s >> t >> w;
    es[i].co = w;
    es[i].u = s;
    es[i].v = t;
  }

  sort(es, es + E, comp);
  UNinit(V);
  int res = 0;
  for (int i = 0; i < E; i++) {
    ed e = es[i];
    if (!UNsame(e.u, e.v)) {
      UNunite(e.u, e.v);
      res += e.co;
    }
  }
  cout << res << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

#define VMAX 10000
#define NOT -1

int main(void) {

  int V, E;
  int s, t, w;
  vector<int> ed[VMAX];
  vector<int> edco[VMAX];
  int min, mx, my;
  int cost;
  bool use[VMAX];

  cin >> V >> E;

  for (int i = 0; i < V; i++)
    use[i] = false;

  for (int i = 0; i < E; i++) {
    cin >> s >> t >> w;
    ed[s].push_back(t);
    ed[t].push_back(s);
    edco[s].push_back(w);
    edco[t].push_back(w);
  }

  use[0] = true;
  cost = 0;
  while (1) {
    min = NOT;
    for (int i = 0; i < V; i++) {
      if (use[i] == true) {
        for (int j = 0; j < ed[i].size(); j++) {
          if (use[ed[i][j]] == false) {
            if (min == NOT || min > edco[i][j])
              min = edco[i][j], mx = j, my = i;
          }
        }
      }
    }
    if (min == NOT)
      break;
    use[my] = true, use[ed[my][mx]] = true;
    cost += edco[my][mx];
  }
  cout << cost << endl;
}
Problem p02364 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
const int maxx = 100010;
const int inf = 0x7fffffff;
int v, e, ans = 0;
class edge {
public:
  int source, target, cost;
  edge(int source, int target, int cost)
      : source(source), target(target), cost(cost) {}
  bool operator<(const edge &e) const { return cost < e.cost; }
};

vector<edge> A;
int pre[maxx];

int find(int u) {
  if (pre[u] != u)
    pre[u] = find(pre[u]);
  return pre[u];
}

void hebing(int x, int y) {
  if (find(x) != find(y))
    pre[find(y)] = find(x);
  find(y);
  return;
}

int main() {
  cin >> v >> e;
  if (e == 0) {
    cout << "0" << endl;
    return 0;
  }
  int x, y, z;
  while (e--) {
    cin >> x >> y >> z;
    A.push_back(edge(x, y, z));
  }
  sort(A.begin(), A.end());
  for (int i = 0; i < v; i++)
    pre[i] = i;
  for (int i = 0; i < A.size(); i++) {
    //		for(int i=0;i<v;i++){
    //			cout<<i<<"  "<<pre[i]<<endl;
    //		}cout<<endl;
    if (find(A[i].source) != find(A[i].target)) {
      hebing(A[i].source, A[i].target);
      ans += A[i].cost;
      // cout<<A[i].source<<"  "<<A[i].target<<"  "<<A[i].cost<<endl;
    }
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
const int maxx = 100010;
const int inf = 0x7fffffff;
int v, e, ans = 0;
class edge {
public:
  int source, target, cost;
  edge(int source, int target, int cost)
      : source(source), target(target), cost(cost) {}
  bool operator<(const edge &e) const { return cost < e.cost; }
};

vector<edge> A;
int pre[maxx];

int find(int u) {
  if (pre[u] != u)
    pre[u] = find(pre[u]);
  return pre[u];
}

void hebing(int x, int y) {
  if (find(x) != find(y))
    pre[y] = pre[x];
  return;
}

int main() {
  for (int i = 0; i < maxx; i++)
    pre[i] = i;
  cin >> v >> e;
  int x, y, z;
  while (e--) {
    cin >> x >> y >> z;
    A.push_back(edge(x, y, z));
  }
  sort(A.begin(), A.end());
  for (int i = 0; i < v; i++) {
    if (find(A[i].source) != find(A[i].target)) {
      hebing(A[i].source, A[i].target);
      ans += A[i].cost;
      //			cout<<A[i].cost<<"  ";
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02364 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int maxn = 200000 + 5;

struct node {
  int x;
  int y;
  int v;
} r[maxn];

int n, m;
int f[maxn];

bool cmp(node a, node b) { return a.v < b.v; }

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

int Kruskal() {
  int ans = 0;
  for (int i = 1; i <= n; i++)
    f[i] = i;
  sort(r + 1, r + m + 1, cmp);
  for (int i = 1; i <= m; i++) {
    int x = find(r[i].x), y = find(r[i].y);
    if (x != y) {
      ans += r[i].v;
      f[x] = y;
    }
  }
  return ans;
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++)
    scanf("%d%d%d", &r[i].x, &r[i].y, &r[i].v);
  int ans = Kruskal();
  printf("%d\\n", ans);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
// int a[10000][10000];
bool isPass[10000];
class Edge {
public:
  int w;
  int x;
  int y;
};
vector<Edge> vec;
int main() {
  int k, n;
  scanf("%d %d", &k, &n);
  for (int i = 0; i < n; ++i) {
    int x, y, z;
    scanf("%d %d %d", &y, &z, &x);
    /*if(a[y][z]>x)
    {
        a[y][z]=x;
        a[z][y]=x;
    }*/
    Edge e;
    e.w = x;
    e.x = y;
    e.y = z;
    vec.push_back(e);
  }
  for (int i = 0; i < n - 1; ++i)
    for (int j = 0; j < n - 1 - i; ++j)
      if (vec[j].w > vec[j + 1].w)
        swap(vec[j], vec[j + 1]);
  int sum = 0, cnt = 0;
  /*sum+=vec[0].w;
  isPass[vec[0].x=true];
  isPass[vec[0].y=true];*/
  for (int i = 0; cnt != k - 1; ++i) {
    if (isPass[vec[i].x] == false || isPass[vec[i].y] == false) {
      // cout<<vec[i].w<<"= "<<vec[i].x<<" "<<vec[i].y<<endl;
      sum += vec[i].w;
      ++cnt;
      isPass[vec[i].x = true];
      isPass[vec[i].y = true];
    }
  }
  printf("%d\\n", sum);
}
Problem p02364 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

using namespace std;
struct edge {
  int cost, u, v;
};
int INF = 100000000;
int V, E;
bool comp(const edge &e1, const edge &e2) { return e1.cost < e2.cost; }
vector<edge> es;
vector<int> par;
vector<int> depth;
void init() {
  par.resize(V);
  depth.resize(V, 0);
  REP(i, V) par[i] = i;
}

int find(int x) {
  if (x == par[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 (depth[x] < depth[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (depth[x] == depth[y])
      depth[x]++;
  }
}

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

int kruskal() {
  sort(es.begin(), es.end(), comp);
  init();
  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.tie(0);
  ios::sync_with_stdio(false);
  cin >> V >> E;
  es.resize(E);
  REP(i, E) {
    int s, t, c;
    cin >> s >> t >> c;
    es[i].u = s;
    es[i].v = t;
    es[i].cost = c;
  }
  cout << kruskal() << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

using namespace std;
int INF = 100000000;
int V, E;
vector<vector<int>> cost;

ll prim() {
  vector<int> mincost(V, INF);
  vector<bool> used(V, false);
  mincost[0] = 0;
  ll 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++) {
      mincost[u] = min(mincost[u], cost[v][u]);
    }
  }
  int a = 0;
  REP(i, V) a += mincost[i];
  return a;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> V >> E;
  cost.resize(V, vector<int>(V, INF));
  REP(i, E) {
    int s, t;
    cin >> s >> t;
    int c;
    cin >> c;
    cost[s][t] = c;
    cost[t][s] = c;
  }
  cout << prim() << endl;

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

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using pii = pair<int, int>;
constexpr int INF = 1e9;

struct edge {
  int from, to, cost;
  bool operator>(const edge &other) const { return cost > other.cost; }
};

using edges = vector<edge>;
using graph = vector<edges>;

void add_edge(graph &g, int from, int to, int cost) {
  g[from].push_back(edge{from, to, cost});
  g[to].push_back(edge{to, from, cost});
}

edges prim(const graph &g) {
  const int N = g.size();
  vector<bool> used(N);
  used[0] = true;
  priority_queue<edge, vector<edge>, greater<edge>> que;
  for (auto e : g[0]) {
    que.push(e);
  }
  edges mst;
  while (!que.empty()) {
    auto e = que.top();
    que.pop();
    if (used[e.to]) {
      continue;
    }
    mst.push_back(e);

    used[e.to] = true;
    for (auto new_e : g[e.to]) {
      que.push(new_e);
    }
  }
  return mst;
}

int main() {
  int V, E;
  cin >> V >> E;
  graph g(V);
  for (int i = 0; i < E; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    add_edge(g, s, t, w);
  }
  const auto mst = prim(g);
  int ans = 0;
  for (auto &e : mst) {
    ans += e.cost;
  }
  cout << ans << endl;
}


Error: MLE

Fixed submission:

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_2_A&lang=jp

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define MAX_V 10000
#define INF 10000
#define MAX_E 100000
int cost[MAX_V][MAX_V];
int mincost[MAX_V];
bool used[MAX_V];
int V;

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++) {
      mincost[u] = min(mincost[u], cost[v][u]);
    }
  }
  return res;
}

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

int main() {

  cin >> V;
  for (int i = 0; i < V; i++) {
    for (int j = 0; j < V; j++) {
      cost[i][j] = INF;
    }
  }
  int E;
  cin >> E;
  edge es[MAX_E];

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

    cost[es[i].u][es[i].v] = es[i].cost;
    cost[es[i].v][es[i].u] = es[i].cost;
  }

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

#include <algorithm>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <utility>
template <class T> class Node {
public:
  Node() : parent(0), child(0){};
  Node *parent;
  Node *child;
  T para;
};
template <class T> class UnionFind {
public:
  void Connect(Node<T> *a, Node<T> *b) { Vertex(a)->parent = Vertex(b); };
  bool Same(Node<T> *a, Node<T> *b) { return Vertex(a) == Vertex(b); };

private:
  Node<T> *Vertex(Node<T> *_obj) {
    if (_obj->parent == 0) {
      return _obj;
    } else {
      return _obj->parent = Vertex(_obj->parent);
    }
  };
};

using namespace std;
typedef pair<int, int> P;
typedef priority_queue<int> PQ;
#define _CRT_SECURE_NO_WARNINGS

int V, E;
struct Load {
  int start;
  int goal;
  int cost;
  void Set(int s, int g, int c) {
    start = s;
    goal = g;
    cost = c;
  }
  bool operator>(const Load &_l) const { return cost > _l.cost; }
  bool operator<(const Load &_l) const { return cost < _l.cost; }
} load[100010];

int main() {

  scanf("%d %d", &V, &E);

  if (E == 0) {
    printf("0\\n");
    return 0;
  }

  for (int i = 0; i < E; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    load[i].Set(a, b, c);
  }
  sort(load, &load[E]);

  UnionFind<int> UF;
  Node<int> *nodes = new Node<int>[V + 2];
  long long ans = 0;
  for (int i = 0; i < E; i++) {
    if (!UF.Same(&nodes[load[i].start], &nodes[load[i].goal])) {
      UF.Connect(&nodes[load[i].start], &nodes[load[i].goal]);
      ans += load[i].cost;
    }
  }

  printf("%lld\\n", ans);

  return 0;
}

Error: 0

Fixed submission:


#include <algorithm>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <utility>
template <class T> class Node {
public:
  Node() : parent(0), child(0){};
  Node *parent;
  Node *child;
  T para;
};
template <class T> class UnionFind {
public:
  void Connect(Node<T> *a, Node<T> *b) { Vertex(a)->parent = Vertex(b); };
  bool Same(Node<T> *a, Node<T> *b) { return Vertex(a) == Vertex(b); };

private:
  Node<T> *Vertex(Node<T> *_obj) {
    if (_obj->parent == 0) {
      return _obj;
    } else {
      return _obj->parent = Vertex(_obj->parent);
    }
  };
};

using namespace std;
typedef pair<int, int> P;
typedef priority_queue<int> PQ;
#define _CRT_SECURE_NO_WARNINGS

int V, E;
struct Load {
  int start;
  int goal;
  int cost;
  void Set(int s, int g, int c) {
    start = s;
    goal = g;
    cost = c;
  }
  bool operator>(const Load &_l) const { return cost > _l.cost; }
  bool operator<(const Load &_l) const { return cost < _l.cost; }
} load[100010];

int main() {

  scanf("%d %d", &V, &E);

  for (int i = 0; i < E; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    load[i].Set(a, b, c);
  }
  sort(load, &load[E - 1]);

  UnionFind<int> UF;
  Node<int> *nodes = new Node<int>[V];
  int ans = 0;
  for (int i = 0; i < E; i++) {
    if (!UF.Same(&nodes[load[i].start], &nodes[load[i].goal])) {
      UF.Connect(&nodes[load[i].start], &nodes[load[i].goal]);
      ans += load[i].cost;
    }
  }

  printf("%d\\n", ans);

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
using namespace std;
const int INF = 1145141919, MOD = 1e9 + 7;
const long long LINF = 8931145141919364364, LMOD = 998244353;
// const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};

const int VMAX = 10010;
int V, E;
vector<vector<pair<int, int>>> edge;
bool used[VMAX];
long long MST(int start) {
  if (V == 1)
    return 0;
  rep(i, VMAX) used[i] = 0;
  // {cost,{from,to}}
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      pq;
  for (auto e : edge[start]) {
    pq.push({e.second, {start, e.first}});
  }
  long long ret = 0;
  int cnt = 1;
  while (pq.size() || cnt < V) {
    auto e = pq.top();
    pq.pop();
    int cost = e.first, from = e.second.first, to = e.second.second;
    used[from] = 1;
    if (!used[to]) {
      ret += cost;
      used[to] = 1;
      cnt++;
      for (auto e : edge[to]) {
        pq.push({e.second, {to, e.first}});
      }
    }
  }
  return (cnt == V ? ret : -1);
}
int main() {
  cin >> V >> E;
  edge.resize(V);
  int start;
  rep(i, E) {
    int from, to, cost;
    cin >> from >> to >> cost;
    start = from;
    edge[from].push_back({to, cost});
    edge[to].push_back({from, cost});
  }
  cout << MST(start) << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
using namespace std;
const int INF = 1145141919, MOD = 1e9 + 7;
const long long LINF = 8931145141919364364, LMOD = 998244353;
// const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};

const int VMAX = 10010;
int V, E;
vector<vector<pair<int, int>>> edge;
bool used[VMAX];
long long MST(int start) {
  rep(i, VMAX) used[i] = 0;
  // {cost,{from,to}}
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      pq;
  for (auto e : edge[start]) {
    pq.push({e.second, {start, e.first}});
  }
  long long ret = 0;
  int cnt = 1;
  while (pq.size() || cnt < V) {
    auto e = pq.top();
    pq.pop();
    int cost = e.first, from = e.second.first, to = e.second.second;
    used[from] = 1;
    if (!used[to]) {
      ret += cost;
      used[to] = 1;
      cnt++;
      for (auto e : edge[to]) {
        pq.push({e.second, {to, e.first}});
      }
    }
  }
  return (cnt == V ? ret : -1);
}
int main() {
  cin >> V >> E;
  edge.resize(V);
  int start;
  rep(i, E) {
    int from, to, cost;
    cin >> from >> to >> cost;
    start = from;
    edge[from].push_back({to, cost});
    edge[to].push_back({from, cost});
  }
  cout << MST(start) << endl;
  return 0;
}
Problem p02364 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using edge = tuple<int, int, int>;

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
  }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int V, E;
  cin >> V >> E;
  if (V == 1) {
    cout << 0 << endl;
    return 0;
  }
  UnionFind uf(V);
  vector<edge> edges;
  while (E--) {
    int s, t, w;
    cin >> s >> t >> w;
    edges.emplace_back(w, s, t);
  }
  sort(begin(edges), end(edges));
  int total_weight = 0;
  int pos = 0;
  int u, v, w;
  tie(w, u, v) = edges[pos];
  for (int i = 1; i < V; ++i) {
    while (uf.root(u) == uf.root(v)) {
      ++pos;
      w = get<0>(edges[pos]);
      u = get<1>(edges[pos]);
      v = get<2>(edges[pos]);
    }
    uf.unite(u, v);
    total_weight += w;
  }
  cout << total_weight << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using edge = tuple<int, int, int>;

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
  }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int V, E;
  cin >> V >> E;
  UnionFind uf(V);
  vector<edge> edges;
  while (E--) {
    int s, t, w;
    cin >> s >> t >> w;
    edges.emplace_back(w, s, t);
  }
  sort(begin(edges), end(edges));
  int total_weight = 0;
  int pos = 0;
  int u, v, w;
  tie(w, u, v) = edges[pos];
  for (int i = 1; i < V; ++i) {
    while (uf.root(u) == uf.root(v)) {
      ++pos;
      w = get<0>(edges[pos]);
      u = get<1>(edges[pos]);
      v = get<2>(edges[pos]);
    }
    uf.unite(u, v);
    total_weight += w;
  }
  cout << total_weight << endl;
  return 0;
}
Problem p02364 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
struct EDGE {
  int from, to, cost;
};
bool operator<(const EDGE &a, const EDGE &b) { return a.cost < b.cost; }

vector<EDGE> g;
int par[10000];

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

bool _union(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return false;
  par[x] = y;
  return true;
}

int main() {
  int V, E;
  scanf("%d %d", &V, &E);

  for (int i = 0; i < V; i++)
    par[i] = i;

  for (int i = 0; i < E; i++) {
    int s, t, w;
    scanf("%d %d %d", &s, &t, &w);
    g.push_back((EDGE){s, t, w});
  }

  sort(g.begin(), g.end());

  int res = 0;
  for (int i = 0; i < g.size(); i++) {
    EDGE &e = g[i];
    if (_union(e.from, e.to))
      res += e.cost;
  }

  printf("%d\\n", res);

  return 0;
}

Error: 0

Fixed submission:

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

const int MAX_V = 10000;
const int MAX_E = 100000;
int finde[MAX_V];

struct Edge {
  int first;
  int second;
  int weight;
};
vector<Edge> e;

bool operator<(const Edge &a, const Edge &b) { return a.weight < b.weight; }

bool Judge(int m, int n) {
  while (finde[m] != m)
    m = finde[m];
  while (finde[n] != n)
    n = finde[n];
  if (m == n) {
    return false;
  }
  m = finde[n];
  return true;
}

int min_span_tree(int n, int m) {
  sort(e.begin(), e.end());
  for (int i = 0; i < n; i++)
    finde[i] = i;
  int count = 0;
  int ret = 0;
  int f, s;
  for (int i = 0; count < m - 1; i++) {
    f = e[i].first;
    s = e[i].second;
    if (Judge(f, s)) {
      ret += e[i].weight;
      count++;
    }
  }
  return ret;
}
int main() {
  int n, m;
  cin >> m >> n;
  for (int i = 0; i < n; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    e.push_back(Edge{s, t, w});
  }
  int ret = min_span_tree(n, m);
  cout << ret << endl;
  return 0;
}