1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
#include<iostream> #include<cstdio> #include<vector> #include<cstring> using namespace std; long long read() { long long x=0,f=1; char c=getchar(); while(!isdigit(c)){if(c=='-') f=-1;c=getchar();} while(isdigit(c)){x=x*10+c-'0';c=getchar();} return x*f; } const int N=1000+10; vector <int> e[N],e2[N]; int dfn[N],dfn_to,low[N],mstack[N],top,belong[N],cnt; bool vis[N],InStack[N]; void Tarjan(int now,int father) { vis[now]=InStack[now]=true; mstack[++top]=now; dfn[now]=low[now]=++dfn_to; for(int i=0;i<int(e[now].size());i++) if(vis[e[now][i]]==false) { Tarjan(e[now][i],now); low[now]=min(low[now],low[e[now][i]]); } else if(e[now][i]!=father and InStack[e[now][i]]==true) low[now]=min(low[now],dfn[e[now][i]]); if(low[now]==dfn[now]) { cnt++; while(mstack[top+1]!=now) InStack[mstack[top]]=false, belong[mstack[top--]]=cnt; } } int Find(int now,int x) { for(int i=0;i<int(e[now].size());i++) if(e[now][i]==x) return i; return -1; } void dfs2(int now) { if(vis[now]==true) return; vis[now]=true; for(int i=0;i<int(e[now].size());i++) if(belong[e[now][i]]==belong[now]) { printf("%d %d\n",now,e[now][i]); e[e[now][i]][Find(e[now][i],now)]=0; dfs2(e[now][i]); } } int n,m; int main() { for(int o=1;;o++) { n=read(),m=read(); if(n==0 and m==0) break;
for(int i=0;i<=n;i++) e[i].clear(),e2[i].clear(); for(int i=1;i<=n;i++) e[i].reserve(4),e2[i].reserve(4); for(int i=1;i<=m;i++) { int s=read(),t=read(); e[s].push_back(t); e[t].push_back(s); }
memset(vis,0,sizeof vis); memset(mstack,0,sizeof mstack); dfn_to=cnt=0; Tarjan(1,0);
printf("%d\n\n",o); memset(vis,0,sizeof vis); for(int i=1;i<=n;i++) dfs2(i); for(int i=1;i<=n;i++) for(int j=0;j<int(e[i].size());j++) if(belong[i]!=belong[e[i][j]] and e[i][j]!=0) printf("%d %d\n",i,e[i][j]); printf("#\n"); } return 0; }
|