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

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

struct Edge {
  int cost, from, to;
  Edge(int cost, int from, int to) : cost(cost), from(from), to(to) {}
};

bool operator<(const Edge &a, const Edge &b) {
  return a.cost != b.cost   ? a.cost < b.cost
         : a.from != b.from ? a.from < b.from
                            : a.to < b.to;
}

struct UnionFind {
  vector<int> data;
  UnionFind(int n) : data(n, -1) {}
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y]; // 高さを更新
      data[y] = x;        // 親を更新
    }
    return x != y;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int find(int x) {
    if (data[x] < 0)
      return x;
    return data[x] = find(data[x]);
  }
};

int kruskal(vector<Edge> &E, int N) {
  UnionFind uf(N);
  int ret = 0, t = 0;
  for (int i = 0; i < E.size(); ++i) {
    int c = E[i].cost, s = E[i].from, d = E[i].to;
    if (!uf.same(s, d)) {
      ret += c;
      uf.unite(s, d);
      ++t;
      if (t == N - 1)
        break;
    }
  }
  return ret;
}

pair<int, vector<int>> mst(vector<Edge> &E, int N) {
  UnionFind uf(N);
  int ret = 0, t = 0;
  vector<int> V;
  for (int i = 0; i < E.size(); ++i) {
    int c = E[i].cost, s = E[i].from, d = E[i].to;
    if (!uf.same(s, d)) {
      ret += c;
      uf.unite(s, d);
      ++t;
      V.push_back(i);
      if (t == N - 1)
        break;
    }
  }
  return make_pair(ret, V);
}

pair<int, int> solve(vector<Edge> &E, int N) {
  pair<int, vector<int>> p = mst(E, N);
  int min_cost = p.first;
  vector<int> V = p.second;
  int ans = 0, INF = 1e9, c = 0;
  for (int i = 0; i < N - 1; ++i) {
    vector<Edge> E_ = E;
    E_[V[i]].cost = INF;
    sort(E_.begin(), E_.end());
    if (kruskal(E_, N) != min_cost) {
      ++ans;
      c += E[V[i]].cost;
    }
  }
  return make_pair(ans, c);
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<Edge> E;
  int s, d, c;
  for (int i = 0; i < M; ++i) {
    cin >> s >> d >> c;
    --s;
    --d;
    E.push_back(Edge(c, s, d));
  }
  sort(E.begin(), E.end());
  pair<int, int> ans = solve(E, N);
  cout << ans.first << " " << ans.second << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int prim(vector<vector<int>> &G, int N) {
  int ret = 0;
  priority_queue<pair<int, int>> wait;
  wait.push(make_pair(0, 0));
  vector<bool> connected(N, false);
  while (!wait.empty()) {
    int cost = -wait.top().first, v = wait.top().second;
    wait.pop();
    if (connected[v])
      continue;
    ret += cost;
    connected[v] = true;
    for (int i = 0; i < N; ++i) {
      int c = G[v][i];
      if (!connected[i]) {
        wait.push(make_pair(-c, i));
      }
    }
  }
  return ret;
}

vector<pair<int, int>> mst(vector<vector<int>> &G, int N) {
  vector<pair<int, int>> ret;
  priority_queue<pair<int, pair<int, int>>> wait;
  wait.push(make_pair(0, make_pair(0, 0)));
  vector<bool> connected(N, false);
  while (!wait.empty()) {
    int cost = -wait.top().first, s = wait.top().second.first,
        t = wait.top().second.second;
    wait.pop();
    if (connected[t])
      continue;
    ret.push_back(make_pair(s, t));
    connected[t] = true;
    for (int i = 0; i < N; ++i) {
      int c = G[t][i];
      if (!connected[i]) {
        wait.push(make_pair(-c, make_pair(t, i)));
      }
    }
  }
  return ret;
}

pair<int, int> solve(vector<vector<int>> &G, int N) {
  int mst_cost = prim(G, N), INF = 1000000000;
  pair<int, int> ret(0, 0);
  vector<pair<int, int>> V = mst(G, N);
  for (int i = 0; i < V.size(); ++i) {
    int s = V[i].first, t = V[i].second, c = G[s][t];
    // cout << v << " " << G[v][j].first << " " << G[v][j].second << endl;
    G[s][t] = INF;
    G[t][s] = INF;
    // cout << prim(G,N) << endl;
    if (mst_cost != prim(G, N)) {
      ++ret.first;
      ret.second += c;
    }
    G[s][t] = c;
    G[t][s] = c;
  }
  return ret;
}

int main() {
  int N, M, INF = 1000000000;
  cin >> N >> M;
  vector<vector<int>> G(N, vector<int>(N, INF));
  int s, d, c;
  for (int i = 0; i < M; ++i) {
    cin >> s >> d >> c;
    --s;
    --d;
    G[s][d] = c;
    G[d][s] = c;
  }
  pair<int, int> ans = solve(G, N);
  cout << ans.first << " " << ans.second << endl;
  return 0;
}