結果

問題 No.1813 Magical Stones
ユーザー Nikita SkybytskyiNikita Skybytskyi
提出日時 2022-01-14 23:16:05
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 225 ms / 2,000 ms
コード長 1,907 bytes
コンパイル時間 2,497 ms
コンパイル使用メモリ 166,272 KB
実行使用メモリ 20,200 KB
最終ジャッジ日時 2024-04-30 17:12:28
合計ジャッジ時間 7,184 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 84 ms
18,384 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 6 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 AC 3 ms
6,940 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 40 ms
8,320 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 88 ms
18,296 KB
testcase_15 AC 210 ms
20,104 KB
testcase_16 AC 217 ms
20,200 KB
testcase_17 AC 173 ms
19,728 KB
testcase_18 AC 215 ms
20,088 KB
testcase_19 AC 216 ms
20,032 KB
testcase_20 AC 221 ms
19,996 KB
testcase_21 AC 218 ms
20,068 KB
testcase_22 AC 221 ms
20,144 KB
testcase_23 AC 225 ms
20,076 KB
testcase_24 AC 3 ms
6,944 KB
testcase_25 AC 23 ms
6,940 KB
testcase_26 AC 8 ms
6,944 KB
testcase_27 AC 128 ms
15,804 KB
testcase_28 AC 104 ms
13,952 KB
testcase_29 AC 136 ms
16,392 KB
testcase_30 AC 120 ms
15,312 KB
testcase_31 AC 186 ms
19,384 KB
testcase_32 AC 2 ms
6,940 KB
testcase_33 AC 2 ms
6,944 KB
testcase_34 AC 5 ms
6,948 KB
testcase_35 AC 11 ms
6,944 KB
testcase_36 AC 5 ms
6,940 KB
testcase_37 AC 25 ms
6,944 KB
testcase_38 AC 61 ms
13,312 KB
testcase_39 AC 138 ms
16,512 KB
testcase_40 AC 5 ms
6,940 KB
testcase_41 AC 5 ms
6,940 KB
testcase_42 AC 12 ms
6,940 KB
testcase_43 AC 9 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
 * Author: nskybytskyi
 * Time:   2022-01-13 15:08:45
 */

#include <bits/stdc++.h>

using namespace std;

//https://cp-algorithms.com/graph/strongly-connected-components.html

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n, m;
  cin >> n >> m;

  vector<vector<int>> adj(n), adj_rev(n);
  vector<bool> used;
  vector<int> order, component;

  while (m--) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    adj[a].push_back(b);
    adj_rev[b].push_back(a);
  }

  function<void(int)> dfs1 = [&] (int v) -> void {
    used[v] = true;

    for (auto u: adj[v])
      if (!used[u])
        dfs1(u);

    order.push_back(v);
  };

  function<void(int)> dfs2 = [&] (int v) -> void {
    used[v] = true;
    component.push_back(v);

    for (auto u : adj_rev[v])
      if (!used[u])
        dfs2(u);
  };

  used.assign(n, false);

  for (int i = 0; i < n; ++i)
    if (!used[i])
      dfs1(i);

  used.assign(n, false);
  reverse(order.begin(), order.end());

  vector<int> roots(n, 0);
  vector<int> root_nodes;

  for (auto v: order)
    if (!used[v]) {
      dfs2(v);

      int root = component.front();
      for (auto u : component) roots[u] = root;
      root_nodes.push_back(root);

      component.clear();
    }

  map<int, int> scc_in, scc_out;
  for (int v = 0; v < n; ++v) {
    int root_v = roots[v];
    scc_in[root_v] = scc_out[root_v] = 0;
  }

  for (int v = 0; v < n; v++)
    for (auto u : adj[v]) {
      int root_v = roots[v],
        root_u = roots[u];

      if (root_u != root_v) {
        ++scc_out[root_v];
        ++scc_in[root_u];
      }
    }

  if (root_nodes.size() == 1) {
    cout << 0 << "\n";
  } else {
    int cnt_in = 0, cnt_out = 0;
    for (auto [k, v] : scc_in) {
      if (!v) {
        ++cnt_in;
      }
    }
    for (auto [k, v] : scc_out) {
      if (!v) {
        ++cnt_out;
      }
    }
    cout << max(cnt_out, cnt_in) << "\n";
  }

  return 0;
}
0