割点

原理

见《进阶指南》第398页。

模板题

洛谷-P3388-【模板】割点(割顶)open in new window

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

const int N = 20010, M = 100010;
struct edge {
    int to, next;
};
edge e[2 * M];
int idx, head[N];
int n, m, root;
int cnt, dfn[N], low[N];
bool cut[N];

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;
                if (cur != root || tot >= 2)
                    cut[cur] = true;
            }
        } else {
            low[cur] = min(low[cur], dfn[to]);
        }
    }
}

int main () {
    memset(head, -1, sizeof(head));
    scanf("%d%d", &n, &m);
    for (int i = 1, u, v; i <= m; ++ i) {
        scanf("%d%d", &u, &v);
        add_edge(u, v);
        add_edge(v, u);
    }

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