結果

問題 No.1553 Lovely City
ユーザー 👑 emthrmemthrm
提出日時 2021-06-18 21:56:36
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 310 ms / 2,000 ms
コード長 4,488 bytes
コンパイル時間 3,092 ms
コンパイル使用メモリ 232,012 KB
実行使用メモリ 61,548 KB
最終ジャッジ日時 2023-09-04 22:59:25
合計ジャッジ時間 13,589 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 130 ms
60,760 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 171 ms
34,448 KB
testcase_09 AC 186 ms
36,172 KB
testcase_10 AC 217 ms
51,048 KB
testcase_11 AC 141 ms
33,560 KB
testcase_12 AC 240 ms
57,192 KB
testcase_13 AC 143 ms
29,976 KB
testcase_14 AC 172 ms
35,732 KB
testcase_15 AC 159 ms
38,172 KB
testcase_16 AC 203 ms
37,984 KB
testcase_17 AC 169 ms
39,960 KB
testcase_18 AC 295 ms
60,440 KB
testcase_19 AC 308 ms
61,548 KB
testcase_20 AC 302 ms
61,008 KB
testcase_21 AC 310 ms
60,172 KB
testcase_22 AC 310 ms
60,364 KB
testcase_23 AC 307 ms
60,668 KB
testcase_24 AC 289 ms
60,088 KB
testcase_25 AC 295 ms
60,284 KB
testcase_26 AC 293 ms
60,252 KB
testcase_27 AC 293 ms
59,872 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

struct StronglyConnectedComponents {
  std::vector<int> id;
  std::vector<std::vector<int>> vertices, comp;

  StronglyConnectedComponents(const std::vector<std::vector<int>> &graph, bool heavy = false) : graph(graph), heavy(heavy) {
    n = graph.size();
    rev_graph.resize(n);
    for (int i = 0; i < n; ++i) for (int e : graph[i]) rev_graph[e].emplace_back(i);
    used.assign(n, false);
    id.assign(n, -1);
    for (int i = 0; i < n; ++i) {
      if (!used[i]) dfs(i);
    }
    int now = 0;
    for (int i = n - 1; i >= 0; --i) {
      if (id[order[i]] == -1) {
        if (heavy) vertices.emplace_back();
        rev_dfs(order[i], now++);
      }
    }
    comp.resize(now);
    for (int i = 0; i < n; ++i) for (int e : graph[i]) {
      if (id[i] != id[e]) comp[id[i]].emplace_back(id[e]);
    }
    // if (heavy) {
    //   for (int i = 0; i < now; ++i) std::sort(vertices[i].begin(), vertices[i].end());
    // }
  }

private:
  bool heavy;
  int n;
  std::vector<std::vector<int>> graph, rev_graph;
  std::vector<bool> used;
  std::vector<int> order;

  void dfs(int ver) {
    used[ver] = true;
    for (int e : graph[ver]) {
      if (!used[e]) dfs(e);
    }
    order.emplace_back(ver);
  }

  void rev_dfs(int ver, int now) {
    id[ver] = now;
    if (heavy) vertices[now].emplace_back(ver);
    for (int e : rev_graph[ver]) {
      if (id[e] == -1) rev_dfs(e, now);
    }
  }
};

struct UnionFind {
  UnionFind(int n) : data(n, -1) {}

  int root(int ver) { return data[ver] < 0 ? ver : data[ver] = root(data[ver]); }

  bool unite(int u, int v) {
    u = root(u);
    v = root(v);
    if (u == v) return false;
    if (data[u] > data[v]) std::swap(u, v);
    data[u] += data[v];
    data[v] = u;
    return true;
  }

  bool same(int u, int v) { return root(u) == root(v); }

  int size(int ver) { return -data[root(ver)]; }

private:
  std::vector<int> data;
};

std::vector<int> topological_sort(const std::vector<std::vector<int>> &graph) {
  int n = graph.size();
  std::vector<int> deg(n, 0);
  for (int i = 0; i < n; ++i) {
    for (int e : graph[i]) ++deg[e];
  }
  std::queue<int> que;
  for (int i = 0; i < n; ++i) {
    if (deg[i] == 0) que.emplace(i);
  }
  std::vector<int> res;
  while (!que.empty()) {
    int ver = que.front(); que.pop();
    res.emplace_back(ver);
    for (int e : graph[ver]) {
      if (--deg[e] == 0) que.emplace(e);
    }
  }
  return res.size() == n ? res : std::vector<int>();
}

int main() {
  int n, m; cin >> n >> m;
  vector<vector<int>> graph(n);
  while (m--) {
    int u, v; cin >> u >> v; --u; --v;
    graph[u].emplace_back(v);
  }
  StronglyConnectedComponents scc(graph, true);
  const int x = scc.comp.size();
  UnionFind uf(x);
  REP(i, x) for (int e : scc.comp[i]) uf.unite(i, e);
  map<int, vector<int>> root;
  REP(i, x) root[uf.root(i)].emplace_back(i);
  vector<int> ts = topological_sort(scc.comp);
  assert(!ts.empty());
  vector<int> inv(x, -1);
  REP(i, x) inv[ts[i]] = i;
  vector<int> a, b;
  for (auto [_, v] : root) {
    sort(ALL(v), [&](int l, int r) -> bool { return inv[l] < inv[r]; });
    int last = -1;
    bool need_loop = false;
    for (int e : v) {
      for (int ver : scc.vertices[e]) {
        if (last != -1) {
          a.emplace_back(last);
          b.emplace_back(ver);
        }
        last = ver;
      }
      need_loop |= scc.vertices[e].size() > 1;
    }
    if (need_loop) {
      a.emplace_back(last);
      b.emplace_back(scc.vertices[v.front()].front());
    }
  }
  int k = a.size();
  cout << k << '\n';
  REP(i, k) cout << a[i] + 1 << ' ' << b[i] + 1 << '\n';
  return 0;
}
0