• poj1144


    求无向图的割顶和桥的裸题。

    #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;
    }

    恶心的地方在于读入,以及千万不要忘记初始化!!!

  • 相关阅读:
    POJ 1185 状压DP
    POJ 1321
    hdu 1384 查分约束
    hdu 2196 树形dp
    hdu 4612 双联通缩点+树形dp
    poj 3469 最小割模板sap+gap+弧优化
    hdu 4858 容器的简单模拟
    hdu 4857 逆向拓扑排序+反向输出
    isap算法模板poj 1273gap+弧优化 最大流
    ISAP 算法的学习
  • 原文地址:https://www.cnblogs.com/yohanlong/p/7768603.html
Copyright © 2020-2023  润新知