結果
問題 |
No.1813 Magical Stones
|
ユーザー |
![]() |
提出日時 | 2021-12-13 12:14:44 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 206 ms / 2,000 ms |
コード長 | 1,284 bytes |
コンパイル時間 | 4,202 ms |
コンパイル使用メモリ | 257,452 KB |
最終ジャッジ日時 | 2025-01-26 09:37:13 |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 40 |
ソースコード
#include <bits/stdc++.h> #include <atcoder/all> #define rep(i, l, n) for (int i = (l); i < (n); i++) using namespace std; using namespace atcoder; template <class T> using V = vector<T>; template <class T> using VV = V<V<T> >; int bool_sum(V<bool>&a) { int res = 0; rep(i, 0, a.size()) { if (a[i]) { res++; } } return res; } VV<int> SCC_to_DAG(int n, VV<int>&route, VV<int>&scc) { VV<int> dag(scc.size(), V<int>(0)); V<int> group(n); rep(i, 0, scc.size()) { rep(j, 0, scc[i].size()) { group[scc[i][j]] = i; } } rep(i, 0, n) { rep(j, 0, route[i].size()) { int a = group[i]; int b = group[route[i][j]]; if (a != b) { dag[a].push_back(b); } } } return dag; } int main(void) { int n, m; cin >> n >> m; VV<int> route(n, V<int>(0)); scc_graph ss(n); rep(i, 0, m) { int a, b; cin >> a >> b; a--; b--; ss.add_edge(a, b); route[a].push_back(b); } VV<int> scc = ss.scc(); if (scc.size() <= 1) { cout << 0 << endl; return 0; } VV<int> dag = SCC_to_DAG(n, route, scc); int ds = dag.size(); V<bool> source(ds, true); V<bool> sink(ds, true); rep(i, 0, ds) { rep(j, 0, dag[i].size()) { sink[i] = source[dag[i][j]] = false; } } int ans = max(bool_sum(source), bool_sum(sink)); cout << ans << endl; return 0; }