电力

LibreOJ-10103-电力open in new window

实现

#include <iostream>
#include <cstring>
using namespace std;

const int N = 10010, M = 15010;
struct edge {
    int to, next;
};
edge e[2 * M];
int idx, head[N];
int n, m, root;
int cnt, dfn[N], low[N];
int res;

void add_edge (int u, int v) {
    e[idx].to = v;
    e[idx].next = head[u];
    head[u] = idx ++;
}
void tarjan (int cur) {
    low[cur] = dfn[cur] = ++ cnt;
    int tot = 0;
    for (int i = head[cur]; i != -1; i = e[i].next) {
        int to = e[i].to;
        if (dfn[to] == 0) {
            tarjan(to);
            low[cur] = min(low[cur], low[to]);

            if (low[to] >= dfn[cur]) ++ tot;
        } else {
            low[cur] = min(low[cur], dfn[to]);
        }
    }
    if (cur != root) ++ tot;
    res = max(res, tot);
}

int main () {
    while (scanf("%d%d", &n, &m), n || m) {
        idx = cnt = res = 0;
        memset(head, -1, sizeof(head));
        memset(dfn, 0, sizeof(dfn));

        for (int i = 1, u, v; i <= m; ++ i) {
            scanf("%d%d", &u, &v);
            ++ u, ++ v;
            add_edge(u, v);
            add_edge(v, u);
        }

        int cnt = 0;
        for (int i = 1; i <= n; ++ i) {
            if (dfn[i] == 0) {
                root = i;
                ++ cnt;
                tarjan(i);
            }
        }
        printf("%d\n", res + cnt - 1);
    }
    return 0;
}
最后修改于: