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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

const int maxN = 510, maxM = maxN * maxN;

#define FORU(i, l, r) for (int i = l; i <= r; ++i)
#define FORD(i, r, l) for (int i = r; i >= l; --i)
#define REPU(i, r) for (int i = 0; i < r; ++i)
#define LL long long
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define PINT pair<ll, int>
#define BIT(x, i) ((x >> i) & 1)
#define NUM_SET_BIT __builtin_popcountll

struct edge {
  int u, v, w;
} e[maxM];

int n, m, res, ans, parent[maxN], d;
vector<int> a;

bool cmp(edge i, edge j) { return (i.w < j.w); }

int get_root(int u) {
  ++d;
  if (u == parent[u])
    return u;
  return (parent[u] = get_root(parent[u]));
}

bool unite(int u, int v) {
  d = 0;
  u = get_root(u);
  int du = d;
  d = 0;
  v = get_root(v);
  int dv = d;
  if (u == v)
    return false;
  if (du > dv)
    parent[u] = v;
  else
    parent[v] = u;
  return true;
}

int main() {
  cin >> n >> m;
  FORU(i, 1, m)
  cin >> e[i].u >> e[i].v >> e[i].w;
  sort(e + 1, e + m + 1, cmp);
  int sum = 0;
  FORU(i, 1, n) parent[i] = i;
  FORU(i, 1, m) {
    if (unite(e[i].u, e[i].v)) {
      a.PB(i);
      sum += e[i].w;
    }
  }
  REPU(j, a.size()) {
    auto ss = 0;
    FORU(i, 1, n) parent[i] = i;
    FORU(i, 1, m) {
      if (i == a[j])
        continue;
      if (unite(e[i].u, e[i].v))
        ss += e[i].w;
    }
    bool check = true;
    FORU(i, 1, n)
    if (get_root(i) != get_root(1))
      check = false;
    if (!check || ss != sum)
      ++res, ans += e[a[j]].w;
  }
  cout << res << " " << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

const int maxN = 510, maxM = maxN * maxN;

#define FORU(i, l, r) for (int i = l; i <= r; ++i)
#define FORD(i, r, l) for (int i = r; i >= l; --i)
#define REPU(i, r) for (int i = 0; i < r; ++i)
#define LL long long
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define PINT pair<ll, int>
#define BIT(x, i) ((x >> i) & 1)
#define NUM_SET_BIT __builtin_popcountll

struct edge {
  int u, v, w;
} e[maxM];

int n, m, res, ans, parent[maxN], d;
vector<int> a;

bool cmp(edge i, edge j) { return (i.w < j.w); }

int get_root(int u) {
  ++d;
  return (u == parent[u]) ? u : get_root(parent[u]);
}

bool unite(int u, int v) {
  d = 0;
  u = get_root(u);
  int du = d;
  d = 0;
  v = get_root(v);
  int dv = d;
  if (u == v)
    return false;
  if (du > dv)
    parent[u] = v;
  else
    parent[v] = u;
  return true;
}

int main() {
  cin >> n >> m;
  FORU(i, 1, m)
  cin >> e[i].u >> e[i].v >> e[i].w;
  sort(e + 1, e + m + 1, cmp);
  int sum = 0;
  FORU(i, 1, n) parent[i] = i;
  FORU(i, 1, m) {
    if (unite(e[i].u, e[i].v)) {
      a.PB(i);
      sum += e[i].w;
    }
  }
  REPU(j, a.size()) {
    auto ss = 0;
    FORU(i, 1, n) parent[i] = i;
    FORU(i, 1, m) {
      if (i == a[j])
        continue;
      if (unite(e[i].u, e[i].v))
        ss += e[i].w;
    }
    bool check = true;
    FORU(i, 1, n)
    if (get_root(i) != get_root(1))
      check = false;
    if (!check || ss != sum)
      ++res, ans += e[a[j]].w;
  }
  cout << res << " " << ans << endl;
}