Test instructions
An undirected graph that directs as many undirected edges as possible to a directional edge makes the graph strongly connected undirected graphs guaranteed to be connected and without heavy edges
Ideas:
The bridge must be bidirectional, so the first side of the two connected components and the bridge is saved in ans
Each side of the two-connected component must be able to turn into a directional edge (after all, the circle is composed of a graph) side of the direction of the two ways:
1, for the branch edge u->v hypothesis Low[v]>dfn[u] explain v back to u above go so ans should be v->u side otherwise is u->v
2, for the reverse side should be all in the ANS due to the Dfs tree such edge is conducive to low reduction
Code:
#include <cstdio> #include <iostream> #include <string> #include <cstring> #include < algorithm> #include <cmath> #include <map> #include <set> #include <vector>using namespace Std;typedef long long LL; #define N 1010#define M 2000010#define inf 2147483647int n,m,t=1,tot,idx;int head[n],dfn[n],low[ n];struct edge{int u,v,next; BOOL Vis,cut,left;} ed[m];void Add (int u,int v) {ed[tot].u=u; Ed[tot].v=v; Ed[tot].next=head[u]; Ed[tot].vis=ed[tot].cut=ed[tot].left=false; head[u]=tot++;} void Tarjan (int u) {int i,v; Dfn[u]=low[u]=++idx; for (I=head[u];~i;i=ed[i].next) {v=ed[i].v; if (Ed[i].vis) continue;ed[i].vis=ed[i^1].vis=true; if (dfn[v]==-1) {Tarjan (v); Low[u]=min (Low[u],low[v]); if (Dfn[u]<low[v]) {ed[i].cut=ed[i^1].cut=true; Ed[i].left=ed[i^1].left=true; }} else Low[u]=min (Low[u],dfn[v]); }}void dfs (int u) {int i,v; Dfn[u]=low[u]=++idx; for (I=head[u];~i;i=ed[i].next) {if (ed[i].cut) continue; V=ED[I].V; if (dfn[v]==-1) {ed[i].vis=ed[i^1].vis=true; DFS (v); Low[u]=min (Low[u],low[v]); if (Low[v]>dfn[u]) ed[i^1].left=true; else ed[i].left=true; } else {low[u]=min (low[u],dfn[v]); if (!ed[i].vis) ed[i].left=true; Ed[i].vis=ed[i^1].vis=true; }}}void Solve () {int I;memset (dfn,-1,sizeof (DFN)); Idx=0;tarjan (1); Memset (Dfn,-1,sizeof (DFN)); Idx=0;for (i=0;i< tot;i++) ed[i].vis=false;for (i=1;i<=n;i++) {if (dfn[i]==-1) DFS (i); }}int Main () {int i,u,v; while (~SCANF ("%d%d", &n,&m)) {if (!n&&!m) break; tot=0; memset (head,-1,sizeof (head)); for (i=1;i<=m;i++) {scanf ("%d%d", &u,&v); Add (U,V); Add (V,u); }Solve (); printf ("%d\n\n", t++); for (i=0;i<tot;i++) {if (ed[i].left) printf ("%d%d\n", ED[I].U,ED[I].V); } printf ("#\n"); }return 0;}
POJ 1515 Street Directions