mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-12-24 03:31:59 +00:00
P2863 [USACO06JAN]The Cow Prom S
R66381808
This commit is contained in:
parent
a21419cfbb
commit
6e7f303b55
59
Luogu/P2863/P2863.cpp
Normal file
59
Luogu/P2863/P2863.cpp
Normal file
@ -0,0 +1,59 @@
|
||||
#include <iostream>
|
||||
#include <stack>
|
||||
#include <vector>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
const int N = 10005;
|
||||
|
||||
int n, m, a, b, ans;
|
||||
std::vector<int> g[N];
|
||||
|
||||
// Tarjan
|
||||
int cnt, dfn[N], low[N];
|
||||
int scc_cnt, id[N], siz[N];
|
||||
std::stack<int> 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;
|
||||
}
|
BIN
Luogu/P2863/data/P2863_11.in
(Stored with Git LFS)
Normal file
BIN
Luogu/P2863/data/P2863_11.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
Luogu/P2863/data/P2863_11.out
(Stored with Git LFS)
Normal file
BIN
Luogu/P2863/data/P2863_11.out
(Stored with Git LFS)
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user