洛谷 P2341 受歡迎的牛(tarjan縮點)

題目鏈接

題意:
有n頭牛,m個關係a,b,表示a牛喜歡b牛,現在規定一頭牛是明星的條件是除了他其他所有的牛都喜歡他,現在問你有多少頭牛可以當明星。

思路:
因爲n是1e4,所以floyd求傳遞閉包肯定是不行的,二維數組都開不了這麼大,複雜度也不對,然後就學了下tarjan縮點,因爲每個強連通分量裏的每個牛肯定是互相喜歡的,所以我們要找強連通分量出度爲0的那一團。
1.只找到一團出度爲0的強連通分量,答案就是那一團的大小。
2.找到0或多團強連通分量,答案是0,因爲他們不互相喜歡,也就是不是所有的牛都喜歡他。

#include <bits/stdc++.h>

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, a, b) for (int i = (int)(a); i >= (int)b; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, r, l) for (int i = (r); i >= (l); i--)
#define ms(x, y) memset(x, y, sizeof(x))
#define SZ(x) ((int)(x).size())

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

const int maxn = (int)1e4+1000; //點數
int scc, top, tot;
vector<int> G[maxn];
int low[maxn], dfn[maxn], belong[maxn];
int stk[maxn], vis[maxn];
int sz[maxn], out[maxn];
void init(int n) {
    for (int i = 1; i <= n; ++i) {
        G[i].clear();
        low[i] = 0;
        dfn[i] = 0;
        stk[i] = 0;
        vis[i] = 0;
    }
    scc = top = tot = 0;
}
void tarjan(int x) {
    stk[top++] = x;
    vis[x] = 1;
    low[x] = dfn[x] = ++tot;
    for (int to : G[x]) {
        if (!dfn[to]) {
            tarjan(to);
            low[x] = min(low[x], low[to]);
        } else if (vis[to]) {
            low[x] = min(low[x], dfn[to]);
        }
    }
    if (low[x] == dfn[x]) {
        ++scc;
        int temp;
        do {
            temp = stk[--top];
            vis[temp] = 0;
            belong[temp] = scc;
            ++sz[scc];
        } while (temp != x);
    }
}
int n, m;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.precision(10);
    cout << fixed;
#ifdef LOCAL_DEFINE
    freopen("input.txt", "r", stdin);
#endif

    ms(sz, 0);
    ms(out, 0);
    cin >> n >> m;
    init(n);
    forn(i, m) {
        int u, v;
        cin >> u >> v;
        G[u].emplace_back(v);
    }
    for1(i, n) if (!dfn[i]) tarjan(i);
    for1(i, n) {
        for (int to : G[i]) {
            if (belong[i] != belong[to]) ++out[belong[i]];
        }
    }
    int cnt = 0, ans = 0;
    for1(i, scc) {
        if (!out[i]) {
            ++cnt;
            ans += sz[i];
        }
    }
    if (cnt == 1) cout << ans << '\n';
    else cout << 0 << '\n';


#ifdef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章