Problem p00929 (C++) — buggy submission, verdict: Runtime Error

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

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;
const int inf = 1 << 29;

struct Node {
  int s, t, c;
  bool operator<(Node a) const { return c < a.c; }
};

Node node[50010];
bool used[50010];

vector<pii> tree[510];
int n, m;

class UF {
public:
  void init(int n) {
    par.resize(n);
    rank.resize(n);
    rep(i, n) par[i] = i, rank[i] = 0;
    return;
  }
  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 (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }

private:
  vi par, rank;
};

bool dfs(int v, int p, int c, int r) {
  if (v == r)
    return true;
  bool ok = false;
  rep(i, tree[v].size()) {
    int v2 = tree[v][i].first;
    if (v2 != p) {
      bool ok2 = dfs(v2, v, c, r);
      if (ok2 && node[tree[v][i].second].c == c)
        used[tree[v][i].second] = false;
      ok |= ok2;
    }
  }
  return ok;
}

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

  rep(i, m) {
    cin >> node[i].s >> node[i].t >> node[i].c;
    node[i].s--;
    node[i].t--;
    if (node[i].t < node[i].s)
      swap(node[i].s, node[i].t);
  }

  sort(node, node + m);

  UF uf;
  uf.init(n);

  rep(i, m) {
    Node e = node[i];
    if (uf.same(e.s, e.t)) {
      dfs(e.t, -1, e.c, e.s);
    } else {
      uf.unite(e.s, e.t);
      pii in;
      in.first = e.t;
      in.second = i;
      tree[e.s].pb(in);
      in.first = e.s;
      tree[e.t].pb(in);
      used[i] = true;
    }
  }
  int ai = 0;
  int ac = 0;
  rep(i, m) {
    if (used[i]) {
      ai++;
      ac += node[i].c;
    }
  }
  cout << ai << " " << ac << endl;
  return 0;
}

Error: 0
e.s = 1
e.t = 2
v = 2
v = 0
v = 1


Fixed submission:

#include <bits/stdc++.h>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;
const int inf = 1 << 29;

struct Node {
  int s, t, c;
  bool operator<(Node a) const { return c < a.c; }
};

Node node[50010];
bool used[50010];
bool tree[50010];

pii graph[510][510];
int n, m;

class UF {
public:
  void init(int n) {
    par.resize(n);
    rank.resize(n);
    rep(i, n) par[i] = i, rank[i] = 0;

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

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

private:
  vi par, rank;
};

bool dfs(int v, int s, int t, int c, bool visited[510]) {
  dump(v) bool ok = false;
  visited[v] = true;
  if (v == t)
    return true;
  rep(i, n) {
    if (visited[i])
      continue;
    if (v == s && i == t)
      continue;
    if (graph[v][i].second >= 0 && tree[graph[v][i].second]) {
      ok |= dfs(i, s, t, c, visited);
      if (ok && node[graph[v][i].second].c == c)
        used[graph[v][i].second] = false;
    }
  }
  visited[v] = false;
  return ok;
}

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

  rep(i, n) rep(j, n) graph[i][j].first = 0, graph[i][j].second = -1;

  rep(i, m) {
    cin >> node[i].s >> node[i].t >> node[i].c;
    node[i].s--;
    node[i].t--;
    if (node[i].t < node[i].s)
      swap(node[i].s, node[i].t);
    graph[node[i].s][node[i].t].first = graph[node[i].t][node[i].s].first =
        node[i].c;
    graph[node[i].s][node[i].t].second = graph[node[i].t][node[i].s].second = i;
  }

  sort(node, node + m);

  UF uf;
  uf.init(n);

  rep(i, m) {
    Node e = node[i];
    if (uf.same(e.s, e.t)) {
      dump(e.s) dump(e.t) bool visited[510];
      rep(j, n) visited[j] = false;
      dfs(e.t, e.t, e.s, e.c, visited);
    } else {
      uf.unite(e.s, e.t);
      tree[i] = true;
      used[i] = true;
    }
  }

  int ai = 0;
  int ac = 0;

  rep(i, m) {
    if (used[i]) {
      ai++;
      ac += node[i].c;
    }
  }
  cout << ai << " " << ac << endl;
  return 0;
}