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

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

using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()

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)]; }
};

typedef int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight < f.weight : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

typedef vector<Weight> Array;
typedef vector<Array> Matrix;

pair<Weight, Edges> MST(int n, const Edges &edges, Edge *unused) {
  UnionFind uf(n);
  priority_queue<Edge> Q;

  Weight total = 0;
  Edges F;

  FOR(e, edges) {
    if (F.size() >= n - 1) {
      break;
    }

    if (unused != NULL && unused->src == e->src && unused->dst == e->dst) {
      continue;
    }

    if (uf.unionSet(e->src, e->dst)) {
      F.push_back(*e);
      total += e->weight;
    }
  }

  return pair<Weight, Edges>(total, F);
}

int main() {

  long long int n, m;
  cin >> n >> m;

  Edges edges;

  for (long long int i = 0; i < m; i++) {

    long long int s, d, c;
    cin >> s >> d >> c;
    s--;
    d--;

    Weight w = c;
    Edge e(s, d, w);

    edges.push_back(e);
  }

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

  /*
  for(int i = 0; i < n; i++){
    for(int j = 0; j < g[i].size(); j++){
      //cout << g[i][j].src << " " << g[i][j].dst << " " << g[i][j].weight <<
  endl;
    }
  }
  */

  pair<Weight, Edges> mst = MST(n, edges, NULL);

  int count = 0, costs = 0;
  FOR(e, mst.second) {
    pair<Weight, Edges> another_mst = MST(n, edges, &(*e));

    if (mst.first != another_mst.first) {
      // cout << e->src << " " << e->dst << " " << e->weight << endl;
      count++;
      costs += e->weight;
    }
  }

  cout << count << " " << costs << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()

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)]; }
};

typedef int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

typedef vector<Weight> Array;
typedef vector<Array> Matrix;

pair<Weight, Edges> MST(const Graph &g, Edge *unused) {
  int n = g.size();

  UnionFind uf(n);
  priority_queue<Edge> Q;

  REP(u, n) FOR(e, g[u]) if (u < e->dst) Q.push(*e);

  Weight total = 0;
  Edges F;
  while (F.size() < n - 1 && !Q.empty()) {
    Edge e = Q.top();
    Q.pop();

    if (unused != NULL && unused->src == e.src && unused->dst == e.dst) {
      continue;
    }

    // cout << e.src << " " << e.dst << " " << e.weight << endl;
    if (uf.unionSet(e.src, e.dst)) {
      F.push_back(e);
      total += e.weight;
    }
  }
  return pair<Weight, Edges>(total, F);
}

int main() {

  long long int n, m;
  cin >> n >> m;

  Graph g;
  for (int i = 0; i < n; i++) {
    g.push_back(Edges());
  }

  for (long long int i = 0; i < m; i++) {

    long long int s, d, c;
    cin >> s >> d >> c;
    s--;
    d--;

    Weight w = c;
    Edge e(s, d, w);

    g[s].push_back(e);
  }

  /*
  for(int i = 0; i < n; i++){
    for(int j = 0; j < g[i].size(); j++){
      //cout << g[i][j].src << " " << g[i][j].dst << " " << g[i][j].weight <<
  endl;
    }
  }
  */

  pair<Weight, Edges> mst = MST(g, NULL);

  int count = 0, costs = 0;
  FOR(e, mst.second) {
    pair<Weight, Edges> another_mst = MST(g, &(*e));

    if (mst.first != another_mst.first) {
      // cout << e->src << " " << e->dst << " " << e->weight << endl;
      count++;
      costs += e->weight;
    }
  }

  cout << count << " " << costs << endl;

  return 0;
}