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

#include "bits/stdc++.h"
#include <fstream>
#include <sys/timeb.h>

using namespace std;

#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define replrev(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define reprev(i, n) replrev(i, 0, n)
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
#define all(a) a.begin(), a.end()
#define mp make_pair
#define mt make_tuple
#define INF 2000000000
#define INFL 1000000000000000000LL
#define EPS (1e-10)
#define MOD 1000000007
#define PI 3.1415926536
#define RMAX 4294967295

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<P> vP;
typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;
typedef vector<vector<ll>> vvll;
typedef vector<vector<char>> vvc;
typedef vector<vector<string>> vvs;
typedef vector<vector<double>> vvd;
typedef vector<vector<P>> vvP;
typedef priority_queue<int, vector<int>, greater<int>> pqli;
typedef priority_queue<ll, vector<ll>, greater<ll>> pqlll;
typedef priority_queue<P, vector<P>, greater<P>> pqlP;
struct Edge {
  int from, to, cost;

  bool operator<(Edge e) { return cost < e.cost; }
};

int N, M;
vector<Edge> edges;
vi used;

class UnionFind {
public:
  vector<int> par, rank, _size;
  int num;

  UnionFind(int n) {
    par = vector<int>(n);
    rank = vector<int>(n);
    _size = vector<int>(n);
    for (int i = 0; i < n; i++) {
      par[i] = i;
      rank[i] = 0;
      _size[i] = 1;
    }
    num = n;
  }

  // ??¨??????????±???????
  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }

  // x??¨y????±??????????????????????
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rank[x] < rank[y]) {
      par[x] = y;
      _size[y] += _size[x];
    } else {
      par[y] = x;
      _size[x] += _size[y];
      if (rank[x] == rank[y])
        rank[x]++;
    }
    num--;
  }

  // x??¨y???????????????????±????????????????
  bool same(int x, int y) { return find(x) == find(y); }

  int size(int x) { return _size[find(x)]; }
};

int Kruskal(int del) {
  UnionFind uf(N);
  int num = 0;
  int ans = 0;
  bool flag = false;
  rep(i, M) {
    if (i == del)
      continue;
    int from = edges[i].from;
    int to = edges[i].to;
    int cost = edges[i].cost;
    if (uf.same(from, to))
      continue;
    if (del == -1) {
      used.push_back(i);
    }
    uf.unite(from, to);
    ans += cost;
    if (uf.num == 1) {
      flag = true;
      break;
    }
  }
  if (!flag) {
    return INF;
  } else {
    return ans;
  }
}

int main() {
  cin >> N >> M;
  edges = vector<Edge>(M);
  used = vi();
  rep(i, M) {
    int s, d, c;
    cin >> s >> d >> c;
    s--, d--;
    edges[i] = Edge{s, d, c};
  }
  sort(all(edges));
  int opt = Kruskal(-1);
  int num = 0;
  int cost = 0;
  rep(i, used.size()) {
    if (Kruskal(used[i]) != opt) {
      num++;
      cost += edges[used[i]].cost;
    }
  }

  cout << num << " " << cost << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <fstream>
#include <sys/timeb.h>

using namespace std;

#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define replrev(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define reprev(i, n) replrev(i, 0, n)
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
#define all(a) a.begin(), a.end()
#define mp make_pair
#define mt make_tuple
#define INF 2000000000
#define INFL 1000000000000000000LL
#define EPS (1e-10)
#define MOD 1000000007
#define PI 3.1415926536
#define RMAX 4294967295

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<P> vP;
typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;
typedef vector<vector<ll>> vvll;
typedef vector<vector<char>> vvc;
typedef vector<vector<string>> vvs;
typedef vector<vector<double>> vvd;
typedef vector<vector<P>> vvP;
typedef priority_queue<int, vector<int>, greater<int>> pqli;
typedef priority_queue<ll, vector<ll>, greater<ll>> pqlll;
typedef priority_queue<P, vector<P>, greater<P>> pqlP;
struct Edge {
  int from, to, cost;

  bool operator<(Edge e) { return cost < e.cost; }
};

int N, M;
vector<Edge> edges;
vvi G;

class UnionFind {
public:
  vector<int> par, rank, _size;
  int num;

  UnionFind(int n) {
    par = vector<int>(n);
    rank = vector<int>(n);
    _size = vector<int>(n);
    for (int i = 0; i < n; i++) {
      par[i] = i;
      rank[i] = 0;
      _size[i] = 1;
    }
    num = n;
  }

  // ??¨??????????±???????
  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }

  // x??¨y????±??????????????????????
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rank[x] < rank[y]) {
      par[x] = y;
      _size[y] += _size[x];
    } else {
      par[y] = x;
      _size[x] += _size[y];
      if (rank[x] == rank[y])
        rank[x]++;
    }
    num--;
  }

  // x??¨y???????????????????±????????????????
  bool same(int x, int y) { return find(x) == find(y); }

  int size(int x) { return _size[find(x)]; }
};

int Prim(int del) {
  UnionFind uf(N);
  int ans = 0;
  bool flag = false;

  pqlP Q;
  rep(i, G[0].size()) {
    if (G[0][i] == del)
      continue;
    Edge e = edges[G[0][i]];
    Q.push(mp(e.cost, e.from + e.to));
  }
  while (uf.num != 1 && !Q.empty()) {
    int p = Q.top().second;
    int c = Q.top().first;
    Q.pop();
    if (uf.same(0, p))
      continue;
    uf.unite(0, p);
    ans += c;
    rep(i, G[p].size()) {
      if (G[p][i] == del)
        continue;
      Edge e = edges[G[p][i]];
      Q.push(mp(e.cost, e.from + e.to - p));
    }
  }
  if (uf.num != 1) {
    return INF;
  } else {
    return ans;
  }
}

int main() {
  cin >> N >> M;
  G = vvi(N);
  edges = vector<Edge>(M);
  rep(i, M) {
    int s, d, c;
    cin >> s >> d >> c;
    s--;
    d--;
    edges[i] = Edge{s, d, c};
    G[s].push_back(i);
    G[d].push_back(i);
  }

  int opt = Prim(-1);
  int num = 0;
  int cost = 0;
  rep(i, M) {
    if (Prim(i) != opt) {
      num++;
      cost += edges[i].cost;
    }
  }

  cout << num << " " << cost << endl;

  return 0;
}