图的割点与割边

/* 图的割点— */

#include<cstdio>/* 用的邻接矩阵来存图, 实际应用用邻接表 */
#include<algorithm>
using namespace std;
int n, m, e[9][9], root;
int num[9], low[9], flag[9], index;//idex用来进行时间戳的递增
//割点算法的核心
void dfs(int cur , int father)//需要传入当前顶点编号 和 父顶点的编号
{
    int child=0, i, j;//child用来记录在生成树中当前顶点cur的儿子个数
    index++;//时间戳加1
    num[cur] = index;//当前顶点cur的时间戳
    low[cur] = index;//当前顶点cur能够访问到最早顶点的时间戳, 刚开始当然是自己
    for(i=1; i<=n;i++)//枚举与当前顶点cur有边相连的顶点i
    {
        if(e[cur][i] == 1)
        {
            if(num[i] == 0)//如果顶点i的时间戳不为0, 说明顶点i还没有访问过
            {
                child++;
                dfs(i, cur);//继续往下深度优先遍历
                //更新当前顶点cur能否访问到最早顶点的时间戳
                low[cur] = min(low[cur], low[i]);
                //如果当前顶点不是根结点并且满足low[i]>=num[cur],则当前顶点为割点
                if(cur != root && low[i] >= num[cur])
                    flag[cur] = 1;
                //如果当前顶点的根结点, 在生成树中根结点必须要有两个儿子,这个根结点才是割点
                if(cur == root && child == 2)
                    flag[cur] = 1;
            }
            else if(i != father)
            //否则如果顶点i曾经被访问过, 并且这个顶点不是当前顶点粗人的父亲,则需要更新当前结点粗人能否到最早顶点的时间戳
            low[cur] = min(low[cur], num[i]);
        }
    }
}
int main()
{
    int i, j, x, y;
    scanf("%d%d", &n,&m);
    for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
            e[i][j] = 0;

    for(i=1;i<=m;i++)
    {
        scanf("%d%d", &x, &y);
        e[x][y] = 1;
        e[y][x] = 1;
    }
    root =1;
    dfs(1, root); //从1号开始进行深度优先遍历
    for(i=1;i<=n;i++)
    {
        if(flag[i] == 1)
            printf("%d ", i);
    }
    return 0;
}
/* 
6 7
1 4
1 3
4 2
3 2
2 5
2 6
5 6
2  */

图的割边—桥*

#include<cstdio>/* 用的邻接矩阵来存图, 实际应用用邻接表 */
#include<algorithm>
using namespace std;
int n, m, e[9][9], root;
int num[9], low[9], flag[9], index;//idex用来进行时间戳的递增
//割点算法的核心
void dfs(int cur , int father)//需要传入当前顶点编号 和 父顶点的编号
{
    int  i, j;//child用来记录在生成树中当前顶点cur的儿子个数
    index++;//时间戳加1
    num[cur] = index;//当前顶点cur的时间戳
    low[cur] = index;//当前顶点cur能够访问到最早顶点的时间戳, 刚开始当然是自己
    for(i=1; i<=n;i++)//枚举与当前顶点cur有边相连的顶点i
    {
        if(e[cur][i] == 1)
        {
            if(num[i] == 0)//如果顶点i的时间戳不为0, 说明顶点i还没有访问过
            {
                dfs(i, cur);//继续往下深度优先遍历
                //更新当前顶点cur能否访问到最早顶点的时间戳
                low[cur] = min(low[cur], low[i]);
                //如果当前顶点不是根结点并且满足low[i]>=num[cur],则当前顶点为割点
                if(low[i] > num[cur])//change
                    printf("%d-%d\n", cur,i);
            }
            else if(i != father)
            //否则如果顶点i曾经被访问过, 并且这个顶点不是当前顶点粗人的父亲,则需要更新当前结点粗人能否到最早顶点的时间戳
            low[cur] = min(low[cur], num[i]);
        }
    }
}
int main()
{
    int i, j, x, y;
    scanf("%d%d", &n,&m);
    for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
            e[i][j] = 0;

    for(i=1;i<=m;i++)
    {
        scanf("%d%d", &x, &y);
        e[x][y] = 1;
        e[y][x] = 1;
    }
    root =1;
    dfs(1, root); //从1号开始进行深度优先遍历
   
    return 0;
}
/* 
6 6
1 4
1 3
4 2
3 2
2 5
5 6
5-6
2-5
2 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值