Problem p00929 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) _range(i, 0, n)
#define _range(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__)

#define _rrep(i, n) _rrange(i, n, 0)
#define _rrange(i, a, b) for (int i = (int)(a)-1; i >= (int)(b); --i)
#define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__)

#define _all(arg) begin(arg), end(arg)
#define uniq(arg) sort(_all(arg)), (arg).erase(unique(_all(arg)), end(arg))
#define getidx(ary, key) lower_bound(_all(ary), key) - begin(ary)
#define clr(a, b) memset((a), (b), sizeof(a))
#define bit(n) (1LL << (n))

// #define DEBUG

#ifdef DEBUG
#define dump(...) fprintf(stderr, __VA_ARGS__)
#else
#define dump(...)
#endif

template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}

using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;

const double EPS = 1e-10;
const double PI = acos(-1.0);
const ll inf = 1LL << 62;
const ll mod = 1000000007LL;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

ll extgcd(ll a, ll b, ll &x, ll &y) {
  x = 1, y = 0;
  ll g = a;
  if (b != 0)
    g = extgcd(b, a % b, y, x), y -= a / b * x;
  return g;
}
ll ADD(const ll &a, const ll &b, const ll &mod) { return (a + b) % mod; }
ll SUB(const ll &a, const ll &b, const ll &mod) { return (a - b + mod) % mod; }
ll MUL(const ll &a, const ll &b, const ll &mod) { return (1LL * a * b) % mod; }
ll DIV(const ll &a, const ll &b, const ll &mod) {
  ll x, y;
  extgcd(b, mod, x, y);
  return MUL(a, (x + mod) % mod, mod);
}

random_device rd;
mt19937 mt(rd());
uniform_int_distribution<int> dice(1, 6);
uniform_real_distribution<double> score(0.0, 10.0);

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(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;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};

using Edge = tuple<int, int, int>;

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

  vector<Edge> edges(m);
  for (auto &e : edges) {
    int a, b, cost;
    cin >> a >> b >> cost;
    a--, b--;
    e = Edge(cost, a, b);
  }
  sort(_all(edges));

  int resn = 0, resc = 0;
  int min_cost = 0;
  vi used(m);
  rep(target, -1, m) {
    if (target != -1 and not used[target])
      continue;

    UnionFind uf(n);

    int sum = 0;
    rep(i, m) {
      if (i == target)
        continue;
      int a, b, cost;
      tie(cost, a, b) = edges[i];
      if (uf.findSet(a, b))
        continue;
      if (target == -1)
        used[i] = true;

      uf.unionSet(a, b);
      sum += cost;
    }

    if (target == -1) {
      min_cost = sum;
      continue;
    }

    if (sum == min_cost)
      continue;
    resn++;
    resc += get<0>(edges[target]);
  }

  cout << resn << " " << resc << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) _range(i, 0, n)
#define _range(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__)

#define _rrep(i, n) _rrange(i, n, 0)
#define _rrange(i, a, b) for (int i = (int)(a)-1; i >= (int)(b); --i)
#define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__)

#define _all(arg) begin(arg), end(arg)
#define uniq(arg) sort(_all(arg)), (arg).erase(unique(_all(arg)), end(arg))
#define getidx(ary, key) lower_bound(_all(ary), key) - begin(ary)
#define clr(a, b) memset((a), (b), sizeof(a))
#define bit(n) (1LL << (n))

// #define DEBUG

#ifdef DEBUG
#define dump(...) fprintf(stderr, __VA_ARGS__)
#else
#define dump(...)
#endif

template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}

using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;

const double EPS = 1e-10;
const double PI = acos(-1.0);
const int inf = 1 << 30;
const ll mod = 1000000007LL;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

ll extgcd(ll a, ll b, ll &x, ll &y) {
  x = 1, y = 0;
  ll g = a;
  if (b != 0)
    g = extgcd(b, a % b, y, x), y -= a / b * x;
  return g;
}
ll ADD(const ll &a, const ll &b, const ll &mod) { return (a + b) % mod; }
ll SUB(const ll &a, const ll &b, const ll &mod) { return (a - b + mod) % mod; }
ll MUL(const ll &a, const ll &b, const ll &mod) { return (1LL * a * b) % mod; }
ll DIV(const ll &a, const ll &b, const ll &mod) {
  ll x, y;
  extgcd(b, mod, x, y);
  return MUL(a, (x + mod) % mod, mod);
}

random_device rd;
mt19937 mt(rd());
uniform_int_distribution<int> dice(1, 6);
uniform_real_distribution<double> score(0.0, 10.0);

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(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;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};

// (cost, to, label)
using Edge = tuple<int, int, int>;

// (cost, v)
using Elem = tuple<int, int>;

int n;

int prim(vector<vector<Edge>> &graph, int target) {
  priority_queue<Elem, vector<Elem>, greater<Elem>> q;
  q.push(Elem(0, 0));
  int ret = 0;

  vi min_cost(n, inf);

  while (q.size()) {
    int cost, v;
    tie(cost, v) = q.top();
    q.pop();

    if (not chmin(min_cost[v], cost))
      continue;
    ret += min_cost[v];

    for (auto &e : graph[v]) {
      int ncost, nv, label;
      tie(ncost, nv, label) = e;
      if (label == target)
        continue;

      if (min_cost[nv] != inf)
        continue;
      q.push(Elem(ncost, nv));
    }
  }

  return ret;
}

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

  vector<vector<Edge>> graph(n);
  vi costs(m);
  rep(i, m) {
    int a, b, cost;
    cin >> a >> b >> cost;
    a--, b--;
    graph[a].push_back(Edge(cost, b, i));
    graph[b].push_back(Edge(cost, a, i));

    costs[i] = cost;
  }

  int resn = 0, resc = 0;
  int min_cost = 0;
  rep(target, -1, m) {
    int sum = prim(graph, target);

    if (target == -1) {
      min_cost = sum;
      continue;
    }

    if (sum == min_cost)
      continue;
    resn++;
    resc += costs[target];
  }

  cout << resn << " " << resc << endl;

  return 0;
}