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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define all(v) begin(v), end(v)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000
#define int long long int

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).X, (b).X) && EQ((a).Y, (b).Y))
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
int const MOD = 1000000007;

namespace std {
bool operator<(const P &a, const P &b) {
  return a.X != b.X ? a.X < b.X : a.Y < b.Y;
}
} // namespace std

// ?§?????????¨????????¨???????????????????¨???????????§???????
template <typename T> struct Edge {
  int from, to;
  T cost;
  Edge(int s, T d) : to(s), cost(d) {}
  Edge(int f, int s, T d) : from(f), to(s), cost(d) {}

  bool operator<(const Edge &e) const { return cost < e.cost; }
  bool operator>(const Edge &e) const { return cost > e.cost; }
};
#define Graph(type) vector<vector<Edge<type>>>

/*
template <typename T>
pair<int, vector< Edge<T> > > prim(vector< vector< Edge<T> > > &G) {
    int V = G.size(); T ans = 0;
    vector< Edge<T> > ans_e;
    vector<bool> used(V, false);
    priority_queue< Edge<T>, vector< Edge<T> >, greater< Edge<T> > > q;
    q.push(Edge<T>(-1, 0, 0));
    while(!q.empty()) {
        Edge<T> temp = q.top(); q.pop();
        if(used[temp.to]) continue;
        used[temp.to] = true;
        ans += temp.cost; if(temp.from >= 0) ans_e.pb(temp);
        rep(i,0,G[temp.to].size()) q.push(G[temp.to][i]);
    }
    rep(i,0,V) if(!used[i]) ans = numeric_limits<T>::max();
    return pair<int, vector< Edge<T> > >(ans, ans_e);
}
*/

template <typename T>
pair<int, vector<Edge<T>>> prim(vector<vector<Edge<T>>> &G, int n1, int n2) {
  int V = G.size();
  T ans = 0;
  vector<Edge<T>> ans_e;
  vector<bool> used(V, false);
  priority_queue<Edge<T>, vector<Edge<T>>, greater<Edge<T>>> q;
  q.push(Edge<T>(-1, 0, 0));
  while (!q.empty()) {
    Edge<T> temp = q.top();
    q.pop();
    if (used[temp.to])
      continue;
    if (temp.from == n1 && temp.to == n2)
      continue;
    if (temp.to == n1 && temp.from == n1)
      continue;
    used[temp.to] = true;
    ans += temp.cost;
    if (temp.from >= 0)
      ans_e.pb(temp);
    rep(i, 0, G[temp.to].size()) q.push(G[temp.to][i]);
  }
  rep(i, 0, V) if (!used[i]) ans = numeric_limits<T>::max();
  return pair<int, vector<Edge<T>>>(ans, ans_e);
}

// Union-Find ??¨ (Verified: AtCoder Typical Contest 001 B)
struct UnionFind {
private:
  const int n;
  int __size;
  vector<int> uf;

public:
  // ????????? UnionFind uni(n) ??????????????£?¨?????????°??????
  UnionFind(int _n) : n(_n), __size(_n), uf(_n, -1) {}
  // find (??¨??????????±???????)
  int find(int x) { return (uf[x] < 0) ? x : uf[x] = find(uf[x]); }
  // x ??¨ y ???????????????????±???????????????????
  bool same(int x, int y) { return find(x) == find(y); }
  // x ????±????????????????????´???°
  int size(int x) { return -uf[find(x)]; }
  // ???????????????????????????
  int size() { return __size; }
  // x ??¨ y ????±??????????????????????
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    __size--;
    if (uf[y] < uf[x])
      swap(x, y);
    uf[x] += uf[y];
    uf[y] = x;
  }
  // ???????????°??¨ (???????????????)
  void debug() {
    for (auto e : uf)
      printf("UnionFind_debug: %lld\\n", e);
  }
};

signed main() {
  int n, m;
  cin >> n >> m;
  Graph(int) G(n);
  vector<Edge<int>> es;
  int s, d, c;
  rep(i, 0, m) {
    cin >> s >> d >> c;
    s--;
    d--;
    G[s].pb(Edge<int>(s, d, c));
    G[d].pb(Edge<int>(d, s, c));
    es.pb(Edge<int>(d, s, c));
    es.pb(Edge<int>(s, d, c));
  }
  pii ans = pii(0, 0);
  pair<int, vector<Edge<int>>> v = prim(G, -1, -1);
  sort(es.begin(), es.end());
  int E = es.size();
  rep(i, 0, v.sc.size()) {
    UnionFind uf(n);
    int res = 0;
    for (int j = 0; j < E; j++) {
      Edge<int> e = es[j];
      if (e.to == v.sc[i].from && e.from == v.sc[i].to)
        continue;
      if (e.to == v.sc[i].to && e.from == v.sc[i].from)
        continue;
      if (!uf.same(e.from, e.to)) {
        uf.unite(e.from, e.to);
        res += e.cost;
      }
    }

    int par = -1;
    for (int j = 0; j < n; j++) {
      if (par == -1)
        par = uf.find(j);
      else if (par != uf.find(j))
        res = INT_MAX;
    }

    if (res > v.fr) {
      ans.fr++;
      ans.sc += v.sc[i].cost;
    }
  }
  printf("%lld %lld\\n", ans.fr, ans.sc);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define all(v) begin(v), end(v)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000
#define int long long int

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).X, (b).X) && EQ((a).Y, (b).Y))
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
int const MOD = 1000000007;

namespace std {
bool operator<(const P &a, const P &b) {
  return a.X != b.X ? a.X < b.X : a.Y < b.Y;
}
} // namespace std

// ?§?????????¨????????¨???????????????????¨???????????§???????
template <typename T> struct Edge {
  int from, to;
  T cost;
  Edge(int s, T d) : to(s), cost(d) {}
  Edge(int f, int s, T d) : from(f), to(s), cost(d) {}

  bool operator<(const Edge &e) const { return cost < e.cost; }
  bool operator>(const Edge &e) const { return cost > e.cost; }
};
#define Graph(type) vector<vector<Edge<type>>>

/*
template <typename T>
pair<int, vector< Edge<T> > > prim(vector< vector< Edge<T> > > &G) {
    int V = G.size(); T ans = 0;
    vector< Edge<T> > ans_e;
    vector<bool> used(V, false);
    priority_queue< Edge<T>, vector< Edge<T> >, greater< Edge<T> > > q;
    q.push(Edge<T>(-1, 0, 0));
    while(!q.empty()) {
        Edge<T> temp = q.top(); q.pop();
        if(used[temp.to]) continue;
        used[temp.to] = true;
        ans += temp.cost; if(temp.from >= 0) ans_e.pb(temp);
        rep(i,0,G[temp.to].size()) q.push(G[temp.to][i]);
    }
    rep(i,0,V) if(!used[i]) ans = numeric_limits<T>::max();
    return pair<int, vector< Edge<T> > >(ans, ans_e);
}
*/

template <typename T>
pair<int, vector<Edge<T>>> prim(vector<vector<Edge<T>>> &G, int n1, int n2) {
  int V = G.size();
  T ans = 0;
  vector<Edge<T>> ans_e;
  vector<bool> used(V, false);
  priority_queue<Edge<T>, vector<Edge<T>>, greater<Edge<T>>> q;
  q.push(Edge<T>(-1, 0, 0));
  while (!q.empty()) {
    Edge<T> temp = q.top();
    q.pop();
    if (used[temp.to])
      continue;
    if (temp.from == n1 && temp.to == n2)
      continue;
    if (temp.to == n1 && temp.from == n1)
      continue;
    used[temp.to] = true;
    ans += temp.cost;
    if (temp.from >= 0)
      ans_e.pb(temp);
    rep(i, 0, G[temp.to].size()) q.push(G[temp.to][i]);
  }
  rep(i, 0, V) if (!used[i]) ans = numeric_limits<T>::max();
  return pair<int, vector<Edge<T>>>(ans, ans_e);
}

signed main() {
  int n, m;
  cin >> n >> m;
  Graph(int) G(n);
  int s, d, c;
  rep(i, 0, m) {
    cin >> s >> d >> c;
    s--;
    d--;
    G[s].pb(Edge<int>(s, d, c));
    G[d].pb(Edge<int>(d, s, c));
  }
  pii ans = pii(0, 0);
  pair<int, vector<Edge<int>>> v = prim(G, -1, -1);
  rep(i, 0, v.sc.size()) {
    // printf("edge: %lld -> %lld\\n", v.sc[i].from, v.sc[i].to);
    pair<int, vector<Edge<int>>> pv = prim(G, v.sc[i].from, v.sc[i].to);
    // printf("debug: pv = %lld, v = %lld\\n", pv.fr, v.fr);
    if (pv.fr > v.fr) {
      ans.fr++;
      ans.sc += v.sc[i].cost;
    }
  }
  printf("%lld %lld\\n", ans.fr, ans.sc);
  return 0;
}