求条玄关
查看原帖
求条玄关
1666970
Wyh_dailyAC楼主2025/8/29 13:30
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;

int n, m, cnt;
int dfn[N], low[N];
vector<int> E[N];
stack<int> stk;
int ans;
bool ins[N];

void tarjan(int u) {
    dfn[u] = low[u] = ++cnt;
    stk.emplace(u);
    // cerr << u << " ovo\n";
    ins[u] = true;
    for (auto v : E[u]) {
        if (!dfn[v]) {
            tarjan(v);
            low[u] = min(low[u], low[v]);
        } else if (ins[u]) {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if (dfn[u] == low[u]) {
        int tmp = 1;
        while (stk.top() != u) {
            // cerr << stk.top() << " ";
            ++tmp, ins[stk.top()] = false;
            stk.pop();
        }
        ins[u] = false;
        stk.pop();
        // cerr << u << "\n";
        if (tmp > 1) ans++;
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> m;
    for (int i = 1, u, v; i <= m; ++i) {
        cin >> u >> v;
        E[u].emplace_back(v);
    }
    for (int i = 1; i <= n; ++i) {
        if (!dfn[i]) tarjan(i);
    }
    cout << ans << "\n";
}

qwq

2025/8/29 13:30
加载中...