【割边 && 桥】UVA - 796 Critical Links

本文介绍了一种求解图中桥的数量及具体位置的方法,通过使用深度优先搜索(DFS)来识别图中的割边,并给出了完整的C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description

给你n个点的图,让你求出有几个桥,按字典序大小输出

思路:会了割点,割边就是改一下 low[v] > dfn[u] 代表,u-v是割边, 裸的模板题,就不弄注释了,心累,看不懂代码,先去看割点

#include<bits/stdc++.h>
using namespace std;
#define mm 10004
struct node
{
    int to, next;
    bool operator < (const node &b) const {
        if(to == b.to) return next < b.next;
        else return to < b.to;
    }
};
node Map[mm * 10];
int dfn[mm], low[mm], head[mm];
node ans[mm];
int top;
int n, sig, root;
void tardfs(int u, int father)
{
    dfn[u] = low[u] = sig++;
    for(int i = head[u]; ~i; i = Map[i].next)
    {
        int to = Map[i].to;
        if(!dfn[to])
        {
            tardfs(to, u);
            low[u] = min(low[u], low[to]);
            if(low[to] > dfn[u])
            {
                ans[top].to = min(u, to);
                ans[top++].next = max(u, to);
            }
        }
        else if(to != father)
        {
            low[u] = min(low[u], dfn[to]);
        }
    }
    return;
}
void add(int u, int v, int &cnt)
{
    Map[cnt].to = v;
    Map[cnt].next = head[u];
    head[u] = cnt++;
}
int main()
{
    int u, m, v, cnt;
    while(~scanf("%d", &n))
    {
        sig = 1;
        cnt = 0; top = 0;
        memset(head, -1, sizeof(head));
        memset(dfn, 0, sizeof(dfn));
        memset(low, 0, sizeof(low));
        for(int i = 0; i < n; i++)
        {
            scanf("%d", &u);
            u = u + 1;
            scanf("%*c%*c%d%*c", &m);
            while(m--)//存图
            {
                scanf("%d", &v);
                v = v + 1;
                add(u, v, cnt);
                add(v, u, cnt);
            }
        }
        for(int i = 0; i < n; i++)
        {
            root = i;
            if(!dfn[i])
            tardfs(i, root);
        }
        sort(ans, ans + top);
        printf("%d critical links\n", top);
        for(int i = 0; i < top; i++)
        {
            printf("%d - %d\n", ans[i].to - 1, ans[i].next - 1);
        }
        printf("\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值