Tyvj(无向图的桥)

题目链接

分析:
无向图的桥

tip

这么裸的题我为什么没有一A呢
因为我又把n和m搞混啦!!!

这里写代码片
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;

const int N=210;
int n,m;
struct node{
    int x,y,nxt;
    bool p;
};
node way[1010];
int pre[N],back[N],tt=0,tot=0,st[N],an=0;
struct po{
    int x,y;
};
po ans[1010];

void add(int u,int w)
{
    tot++;
    way[tot].x=u;way[tot].y=w;way[tot].nxt=st[u];st[u]=tot;
}

int cmp(const po &a,const po &b)
{
    if (a.x!=b.x) return a.x<b.x;
    else return a.y<b.y;
}

int dfs(int now,int fa)
{
    int ch=0;
    int low=pre[now]=++tt;
    for (int i=st[now];i;i=way[i].nxt)
    {
        int v=way[i].y;
        if (!pre[v])
        {
            int lowv=dfs(v,now);
            low=min(low,lowv);
            ch++;
            if (lowv>pre[now])  //存在即合理 
            {
                an++;
                ans[an].x=min(now,v);
                ans[an].y=max(now,v); 
            }  
        }
        else if (pre[v]<pre[now]&&v!=fa)
        {
            low=min(low,pre[v]);
        }
    }
    back[now]=low;
    return low;
}

int main()
{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=m;i++)
    {
        int u,w;
        scanf("%d%d",&u,&w);
        add(u,w);
        add(w,u);
    }
    dfs(1,-1);
    sort(ans+1,ans+1+an,cmp);
    int xx=-1;
    int yy=-1;
    for (int i=1;i<=an;i++)
        if (xx!=ans[i].x|yy!=ans[i].y)
        {
            printf("%d %d
",ans[i].x,ans[i].y);
            xx=ans[i].x;
            yy=ans[i].y;
        }
    return 0;
}
原文地址:https://www.cnblogs.com/wutongtong3117/p/7673119.html