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

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

constexpr int INF = 1e9 + 7;

using Weight = int;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};

auto operator<(const Edge &e, const Edge &f) {
  if (e.weight != f.weight)
    return e.weight > f.weight;
  else if (e.src != f.src)
    return e.src < f.src;
  else
    return e.dst < f.dst;
}

using Edges = vector<Edge>;
using Graph = vector<Edges>;

long long ChuLiu_Edmonds(Graph &g, int root) {
  const int V = g.size();
  Edges mins(V, Edge(-1, -1, INF));
  //????????????????????\\??£?????????????°???????????????¶
  for (auto es : g) {
    for (auto e : es) {
      if (e.weight < mins[e.dst].weight) {
        mins[e.dst] = e;
      }
    }
  }
  mins[root] = Edge(-1, -1, -1);
  /*for(auto e:mins){
      if(e.weight==INF)return -1;
  }*/
  vector<int> group(V, 0);
  vector<bool> isCycle(V, false);
  int count = 0;
  vector<bool> used(V, false);
  //??°???????????????
  for (int i = 0; i < V; ++i) {
    if (used[i])
      continue;
    vector<int> chain;
    int cursor = i;
    //??????????????£?????????
    while (cursor != -1 && !used[cursor]) {
      used[cursor] = true;
      chain.push_back(cursor);
      cursor = mins[cursor].src;
    }
    //?????????????????§??????
    if (cursor != -1) {
      bool inCycle = false;
      for (int j = 0; j < chain.size(); ++j) {
        group[chain[j]] = count;
        if (chain[j] == cursor) {
          isCycle[count] = true;
          inCycle = true;
        }
        if (!inCycle)
          ++count;
      }
      if (inCycle)
        ++count;
    } else { //????????§??????
      for (int j = 0; j < chain.size(); ++j) {
        group[chain[j]] = count++;
      }
    }
  }
  //??°?????????????????????
  //?????????????????¨???
  if (count == V) {
    long long ans = 1;
    for (int i = 0; i < V; ++i) {
      ans += mins[i].weight;
    }
    return ans;
  }
  //????´?????????????????????????????¨?
  long long res = 0;
  for (int i = 0; i < V; ++i) {
    if (i != root && isCycle[group[i]]) {
      res += mins[i].weight;
    }
  }
  //???????????????????????????????????°????????????????????????
  Graph ng(count);
  for (auto es : g) {
    for (auto e : es) {
      int to = e.dst;
      int gfrom = group[e.src];
      int gto = group[e.dst];
      if (gfrom == gto) {
        continue;
      } else if (isCycle[gto]) {
        ng[gfrom].emplace_back(gfrom, gto, e.weight - mins[to].weight);
      } else {
        ng[gfrom].emplace_back(gfrom, gto, e.weight);
      }
    }
  }
  return res + ChuLiu_Edmonds(ng, group[root]);
}

int dijkstra(const Graph &g, int s, int t) {
  const int V = g.size();
  priority_queue<Edge> q;
  vector<Weight> dist(V, INF); //???????????§????????¢?????\\???
  dist[s] = 0;
  vector<int> prev(V, -1); //???????????????????????\\???
  for (q.emplace(-1, s, 0); !q.empty();) {
    Edge e = q.top();
    q.pop();
    if (prev[e.dst] != -1)
      continue;
    prev[e.dst] = e.src;
    for (auto ge : g[e.dst]) {
      if (dist[ge.dst] > e.weight + ge.weight) {
        dist[ge.dst] = e.weight + ge.weight;
        q.emplace(ge.src, ge.dst, e.weight + ge.weight);
      }
    }
    if (prev[t] != -1)
      return dist[t];
  }
  return dist[t];
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> in_e(N, 0), zero, to;
  Graph g(N);
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    g[a].emplace_back(a, b, 0);
    g[b].emplace_back(b, a, 1);
    in_e[b]++;
  }
  for (int i = 0; i < N; ++i) {
    if (!in_e[i])
      zero.push_back(i);
  }
  Graph ng(zero.size());
  for (int i = 0; i < zero.size(); ++i) {
    for (int j = 0; j < zero.size(); ++j) {
      if (i != j) {
        ng[i].emplace_back(i, j, dijkstra(g, zero[i], zero[j]));
      }
    }
  }
  vector<pair<ll, int>> hu;
  for (int i = 0; i < zero.size(); ++i) {
    hu.emplace_back(ChuLiu_Edmonds(ng, i), zero[i]);
  }
  stable_sort(hu.begin(), hu.end());
  ll ans = hu[0].first;
  for (auto x : hu) {
    if (x.first == ans)
      to.push_back(x.second);
    else
      break;
  }
  cout << to.size() << " " << ans << endl;
  for (int i = 0; i < to.size(); ++i) {
    if (!i)
      cout << to[i];
    else
      cout << " " << to[i];
  }
  cout << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

constexpr int INF = 1e9 + 7;

using Weight = int;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};

auto operator<(const Edge &e, const Edge &f) {
  if (e.weight != f.weight)
    return e.weight > f.weight;
  else if (e.src != f.src)
    return e.src < f.src;
  else
    return e.dst < f.dst;
}

using Edges = vector<Edge>;
using Graph = vector<Edges>;

long long ChuLiu_Edmonds(Graph &g, int root) {
  const int V = g.size();
  Edges mins(V, Edge(-1, -1, INF));
  //????????????????????\\??£?????????????°???????????????¶
  for (auto es : g) {
    for (auto e : es) {
      if (e.weight < mins[e.dst].weight) {
        mins[e.dst] = e;
      }
    }
  }
  mins[root] = Edge(-1, -1, -1);
  /*for(auto e:mins){
      if(e.weight==INF)return -1;
  }*/
  vector<int> group(V, 0);
  vector<bool> isCycle(V, false);
  int count = 0;
  vector<bool> used(V, false);
  //??°???????????????
  for (int i = 0; i < V; ++i) {
    if (used[i])
      continue;
    vector<int> chain;
    int cursor = i;
    //??????????????£?????????
    while (cursor != -1 && !used[cursor]) {
      used[cursor] = true;
      chain.push_back(cursor);
      cursor = mins[cursor].src;
    }
    //?????????????????§??????
    if (cursor != -1) {
      bool inCycle = false;
      for (int j = 0; j < chain.size(); ++j) {
        group[chain[j]] = count;
        if (chain[j] == cursor) {
          isCycle[count] = true;
          inCycle = true;
        }
        if (!inCycle)
          ++count;
      }
      if (inCycle)
        ++count;
    } else { //????????§??????
      for (int j = 0; j < chain.size(); ++j) {
        group[chain[j]] = count++;
      }
    }
  }
  //??°?????????????????????
  //?????????????????¨???
  if (count == V) {
    long long ans = 1;
    for (int i = 0; i < V; ++i) {
      ans += mins[i].weight;
    }
    return ans;
  }
  //????´?????????????????????????????¨?
  long long res = 0;
  for (int i = 0; i < V; ++i) {
    if (i != root && isCycle[group[i]]) {
      res += mins[i].weight;
    }
  }
  //???????????????????????????????????°????????????????????????
  Graph ng(count);
  for (auto es : g) {
    for (auto e : es) {
      int to = e.dst;
      int gfrom = group[e.src];
      int gto = group[e.dst];
      if (gfrom == gto) {
        continue;
      } else if (isCycle[gto]) {
        ng[gfrom].emplace_back(gfrom, gto, e.weight - mins[to].weight);
      } else {
        ng[gfrom].emplace_back(gfrom, gto, e.weight);
      }
    }
  }
  return res + ChuLiu_Edmonds(ng, group[root]);
}

int dijkstra(const Graph &g, int s, int t) {
  const int V = g.size();
  priority_queue<Edge> q;
  vector<Weight> dist(V, INF); //???????????§????????¢?????\\???
  dist[s] = 0;
  vector<int> prev(V, -1); //???????????????????????\\???
  for (q.emplace(-1, s, 0); !q.empty();) {
    Edge e = q.top();
    q.pop();
    if (prev[e.dst] != -1)
      continue;
    prev[e.dst] = e.src;
    for (auto ge : g[e.dst]) {
      if (dist[ge.dst] > e.weight + ge.weight) {
        dist[ge.dst] = e.weight + ge.weight;
        q.emplace(ge.src, ge.dst, e.weight + ge.weight);
      }
    }
  }
  return dist[t];
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> in_e(N, 0), zero, to;
  Graph g(N);
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    g[a].emplace_back(a, b, 0);
    g[b].emplace_back(b, a, 1);
    in_e[b]++;
  }
  for (int i = 0; i < N; ++i) {
    if (!in_e[i])
      zero.push_back(i);
  }
  Graph ng(zero.size());
  for (int i = 0; i < zero.size(); ++i) {
    for (int j = 0; j < zero.size(); ++j) {
      if (i != j) {
        ng[i].emplace_back(i, j, dijkstra(g, zero[i], zero[j]));
      }
    }
  }
  vector<pair<ll, int>> hu;
  for (int i = 0; i < zero.size(); ++i) {
    hu.emplace_back(ChuLiu_Edmonds(ng, i), zero[i]);
  }
  stable_sort(hu.begin(), hu.end());
  ll ans = hu[0].first;
  for (auto x : hu) {
    if (x.first == ans)
      to.push_back(x.second);
    else
      break;
  }
  cout << to.size() << " " << ans << endl;
  for (int i = 0; i < to.size(); ++i) {
    if (!i)
      cout << to[i];
    else
      cout << " " << to[i];
  }
  cout << endl;
}