From 6e7f303b55f7f54d6b136c8466926808cbe57027 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Fri, 7 Jan 2022 21:51:52 +0800 Subject: [PATCH] P2863 [USACO06JAN]The Cow Prom S R66381808 --- Luogu/P2863/P2863.cpp | 59 +++++++++++++++++++++++++++++++++++ Luogu/P2863/data/P2863_11.in | 3 ++ Luogu/P2863/data/P2863_11.out | 3 ++ 3 files changed, 65 insertions(+) create mode 100644 Luogu/P2863/P2863.cpp create mode 100644 Luogu/P2863/data/P2863_11.in create mode 100644 Luogu/P2863/data/P2863_11.out diff --git a/Luogu/P2863/P2863.cpp b/Luogu/P2863/P2863.cpp new file mode 100644 index 00000000..2594bb3b --- /dev/null +++ b/Luogu/P2863/P2863.cpp @@ -0,0 +1,59 @@ +#include +#include +#include + +using std::cin; +using std::cout; +using std::endl; + +const int N = 10005; + +int n, m, a, b, ans; +std::vector g[N]; + +// Tarjan +int cnt, dfn[N], low[N]; +int scc_cnt, id[N], siz[N]; +std::stack st; +bool vis[N]; + +void tarjan(int u) { + dfn[u] = low[u] = ++cnt; + st.push(u); + vis[u] = true; + for (int v : g[u]) { + if (!dfn[v]) { + tarjan(v); + low[u] = std::min(low[u], low[v]); + } else if (vis[v]) { + low[u] = std::min(low[u], dfn[v]); + } + } + if (low[u] == dfn[u]) { + scc_cnt++; + int v; + do { + v = st.top(); + st.pop(); + vis[v] = false; + id[v] = scc_cnt; + siz[scc_cnt]++; + } while (v != u); + } +} + +int main() { + cin >> n >> m; + for (int i = 1; i <= m; i++) { + cin >> a >> b; + g[a].push_back(b); + } + for (int i = 1; i <= n; i++) { + if (!dfn[i]) tarjan(i); + } + for (int i = 1; i <= scc_cnt; i++) { + if (siz[i] > 1) ans++; + } + cout << ans << endl; + return 0; +} diff --git a/Luogu/P2863/data/P2863_11.in b/Luogu/P2863/data/P2863_11.in new file mode 100644 index 00000000..d0a613a1 --- /dev/null +++ b/Luogu/P2863/data/P2863_11.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:93074d19303a9fb778f65676f29e47fa05e212d4b9e6e953198b7c8bf58ff8cd +size 109 diff --git a/Luogu/P2863/data/P2863_11.out b/Luogu/P2863/data/P2863_11.out new file mode 100644 index 00000000..252faa6b --- /dev/null +++ b/Luogu/P2863/data/P2863_11.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35 +size 1