P1231 危险道路

P1231 危险道路
2017年7月29日
DFS+割边


#include<algorithm>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<cmath>
using namespace std;

int N, M;
struct Edges{
    int x, y, next;
}E[10100];    int ans = 0;
int V[200];
int reE[10100];
//
int dfn[200], low[200], ind = 0;
//
int Que[200], tails = 0;
struct CutEdges{
    int x, y;
}CE[10100];


bool cmp(CutEdges a, CutEdges b)
{
    return ((a.x < b.x) || (a.x == b.x && a.y < b.y));
}

void AssigenEdges(int xi, int yi)
{
   E[++ans].next = V[xi];   E[ans].y = yi;    E[ans].x = xi;    V[xi] = ans;
   E[++ans].next = V[yi];   E[ans].y = xi;    E[ans].x = yi;    V[yi] = ans;
   reE[ans] = ans - 1;    reE[ans - 1] = ans;
}

void Putin()
{
    cin >> N >> M;
    int ai, bi;
    for(int i = 1; i <= M; i++){
        cin >> ai >> bi;
        AssigenEdges(ai, bi);
    }
}

void DFS_CutEdge(int x, int e)
{
    dfn[x] = low[x] = ++ind;
    for(int i = V[x]; i > 0; i = E[i].next){
        int y = E[i].y;
        if(i != e){
            if(!dfn[y]){
                DFS_CutEdge(y, reE[i]);
                if(low[y] < low[x])    low[x] = low[y];
            }
            else
                if(dfn[y] <= low[x])    low[x] = dfn[y];
        }
    }
    if(low[x] >= dfn[x] && e)    Que[++tails] = e;
}

void Printout()
{
    for(int i = 1; i <= tails; i++){
        int o = Que[i];
        CE[i].x = min(E[o].x, E[o].y);
        CE[i].y = max(E[o].x, E[o].y);
    }
    sort(CE + 1, CE + 1 + tails, cmp);
    for(int i = 1; i <= tails; i++)
        cout << CE[i].x << ' ' << CE[i].y << endl;
}

int main()
{
    Putin();
    DFS_CutEdge(1, 0);
    Printout();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值