結果

問題 No.1813 Magical Stones
ユーザー 👑 emthrmemthrm
提出日時 2022-01-14 22:04:41
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 155 ms / 2,000 ms
コード長 2,818 bytes
コンパイル時間 2,873 ms
コンパイル使用メモリ 215,460 KB
実行使用メモリ 23,628 KB
最終ジャッジ日時 2023-08-12 19:46:06
合計ジャッジ時間 6,880 ms
ジャッジサーバーID
(参考情報)
judge2 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 12 ms
13,496 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 21 ms
8,144 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 12 ms
13,584 KB
testcase_15 AC 151 ms
22,772 KB
testcase_16 AC 139 ms
22,776 KB
testcase_17 AC 153 ms
23,628 KB
testcase_18 AC 147 ms
22,696 KB
testcase_19 AC 138 ms
22,836 KB
testcase_20 AC 150 ms
22,604 KB
testcase_21 AC 149 ms
22,596 KB
testcase_22 AC 150 ms
22,472 KB
testcase_23 AC 155 ms
22,740 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 14 ms
6,376 KB
testcase_26 AC 5 ms
4,380 KB
testcase_27 AC 95 ms
17,276 KB
testcase_28 AC 118 ms
15,652 KB
testcase_29 AC 118 ms
18,272 KB
testcase_30 AC 103 ms
17,460 KB
testcase_31 AC 139 ms
21,384 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 5 ms
4,376 KB
testcase_35 AC 10 ms
4,380 KB
testcase_36 AC 3 ms
4,376 KB
testcase_37 AC 24 ms
6,120 KB
testcase_38 AC 15 ms
10,720 KB
testcase_39 AC 91 ms
17,204 KB
testcase_40 AC 4 ms
4,376 KB
testcase_41 AC 5 ms
4,376 KB
testcase_42 AC 11 ms
4,476 KB
testcase_43 AC 8 ms
4,472 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);
    }
  }
};

int main() {
  int n, m; cin >> n >> m;
  vector<vector<int>> graph(n);
  while (m--) {
    int a, b; cin >> a >> b; --a; --b;
    graph[a].emplace_back(b);
  }
  StronglyConnectedComponents scc(graph);
  const int x = scc.comp.size();
  if (x == 1) {
    cout << 0 << '\n';
    return 0;
  }
  vector<int> is_root(x, true);
  int leaf = 0;
  REP(i, x) {
    for (int j : scc.comp[i]) is_root[j] = false;
    leaf += scc.comp[i].size() == 0;
  }
  cout << max(static_cast<int>(count(ALL(is_root), true)), leaf) << '\n';
  return 0;
}
0