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