求无向图的割顶和桥的裸题。
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 105, maxm = maxn * maxn; int n, m, tot, dfs_clock; int h[maxn], dfn[maxn], low[maxn], iscut[maxn]; struct edge { int v, next; }a[maxm]; void add(int x, int y) { a[tot].v = y; a[tot].next = h[x]; h[x] = tot++; } int dfs(int u, int fa) { int lowu = dfn[u] = ++dfs_clock; int child = 0; for (int i = h[u]; ~i; i = a[i].next) { int v = a[i].v; if (!dfn[v]) { child++; int lowv = dfs(v, u); lowu = min(lowu, lowv); if (lowv >= dfn[u]) { iscut[u] = 1; } // if (lowv > dfn[u]) // { // printf("%d - %d ", u, v); // } }else if (dfn[v] < dfn[u] && v != fa) { lowu = min(lowu, dfn[v]); } } if (fa == 0 && child == 1) { iscut[u] = 0; } low[u] = lowu; return lowu; } int main() { // freopen("poj1144.in","r",stdin); while (scanf("%d", &n) && n) { memset(h, -1, sizeof h); tot = dfs_clock = 0; memset(low, 0, sizeof low); memset(dfn, 0, sizeof dfn); memset(iscut, 0, sizeof iscut); int x; while (scanf("%d", &x) && x) { while (getchar() != ' ') { int y; scanf("%d", &y); add(x, y); add(y, x); } } dfs(1, 0); int ans = 0; for (int i = 1; i <= n; i++) if (iscut[i]) ans++; printf("%d ", ans); } return 0; }
恶心的地方在于读入,以及千万不要忘记初始化!!!