hdu 2767 Proving Equivalences(求最少加幾條邊使圖變爲強連通圖)

題目鏈接

思路:
先用tarjan縮點
然後說結論:
設in0爲圖中入度爲0點的個數,out0爲圖中出度爲0點的個數,最少需要加max(in0, out0)才能使圖變爲強連通圖。當然,如果已經是強連通圖的情況要特判一下。

個人的理解是這樣的,因爲強連通圖每個點的入度和出度肯定是>=1的,所以目標就是讓圖中的每個點入度和出度都>=1就能變成強連通圖, 而每次加邊最多隻能同時消滅一個入度爲0和一個出度爲0,所以最少加max(in0, out0)條邊,圖就能變成強連通圖(如果已經是強連通圖的情況要特判,因爲此時不符合這個結論)。

#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 = 2*(int)1e4+100; //點數
int scc, top, tot;
vector<int> G[maxn];
int low[maxn], dfn[maxn], belong[maxn];
int stk[maxn], vis[maxn], in[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;
        in[i] = 0;
        out[i] = 0;
    }
    scc = top = tot = 0;
}
void tarjan(int x) {
    stk[top++] = x;
    low[x] = dfn[x] = ++tot;
    vis[x] = 1;
    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];
            belong[temp] = scc;
            vis[temp] = 0;
        } while (temp != x);
    }
}
int tc, 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

    cin >> tc;
    while (tc--) {
        cin >> n >> m;
        init(n);
        forn(i, m) {
            int u, v;
            cin >> u >> v;
            G[u].eb(v);
        }
        for1(i, n) if (!dfn[i]) tarjan(i);
        if (scc == 1) cout << 0 << '\n';
        else {
            for (int i = 1; i <= n; ++i) {
                for (int j : G[i]) {
                    if (belong[i] == belong[j]) continue;
                    ++out[belong[i]];
                    ++in[belong[j]];
                }
            }
            int in0 = 0, out0 = 0;
            for1(i, scc) {
                if (!in[i]) ++in0;
                if (!out[i]) ++out0;
            }
            cout << max(in0, out0) << '\n';
        }
    }

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

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