点连通分量

电力

\(\href{https://www.acwing.com/solution/content/20702/}{点连通分量}\)

#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define x first
#define y second
#define inf 0x3f3f3f3f
const int N = 10010, M = 30010;
int n, m;
int h[N], e[M], ne[M], idx;
int dfn[N], low[N], timestamp;
int root, ans;

void add(int a, int b) {
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

void tarjan(int u) {
    dfn[u] = low[u] = ++timestamp;
    int cnt = 0;
    for (int i = h[u]; ~i; i = ne[i]) {
        int j = e[i];
        if (!dfn[j]) {
            tarjan(j);
            low[u] = min(low[u], low[j]);
            if (low[j] >= dfn[u]) ++cnt;
        }
        else low[u] = min(low[u], dfn[j]);
    }
    if (u != root) cnt++;
    ans = max(ans, cnt);
}

int main() {
    IO;
    while (cin >> n >> m, n || m) {
        memset(dfn, 0, sizeof dfn);
        memset(h, -1, sizeof h);
        idx = timestamp = 0;
        while (m--) {
            int a, b;
            cin >> a >> b;
            add(a, b), add(b, a);
        }
        ans = 0;
        int cnt = 0;
        for (root = 0; root < n; ++root)
            if (!dfn[root]) {
                ++cnt;
                tarjan(root);
            }
        cout << ans + cnt - 1 << '\n';
    }
    return 0;
}

上一篇:<dfn>


下一篇:【考试总结2020-02-20】完整