結果

問題 No.1813 Magical Stones
ユーザー Nikita SkybytskyiNikita Skybytskyi
提出日時 2022-01-14 23:13:50
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,907 bytes
コンパイル時間 3,178 ms
コンパイル使用メモリ 165,760 KB
実行使用メモリ 20,232 KB
最終ジャッジ日時 2024-04-30 17:09:11
合計ジャッジ時間 6,992 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 71 ms
18,388 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 2 ms
6,940 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 71 ms
18,380 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 99 ms
19,748 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 1 ms
6,940 KB
testcase_33 AC 1 ms
6,944 KB
testcase_34 WA -
testcase_35 AC 9 ms
6,944 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 4 ms
6,940 KB
testcase_42 AC 9 ms
6,940 KB
testcase_43 WA -
権限があれば一括ダウンロードができます

ソースコード

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 << min(cnt_out, cnt_in) << "\n";
  }

  return 0;
}
0