This question is to ask the number of bridges.
Then there are the corresponding theorem of bridge judgment:
And u in adjacent nodes, there is a node is the smallest timestamp than the
current u access to a large order, that this point can only be reached by u fruit, then the
adjacent edges between them is the bridge
#include<iostream> #include<string.h> #include<algorithm> #include<stdio.h> using namespace std; const int SIZE = 100010; struct node{ int u,v; }brige[SIZE]; int head[SIZE],ver[SIZE*2],Next[SIZE*2]; int dfn[SIZE],low[SIZE],n,m,tot,num,cnt; void add(int x,int y){ ver[++tot]=y,Next[tot]=head[x],head[x]=tot; } voidTarjan ( int U, int pre) { DFN [U] = Low [U] ++ = NUM; for ( int I = head [U]; I; I = the Next [I]) { int V = Ver [I] ; IF (V == pre) Continue ; IF (! DFN [V]) { Tarjan (V, U); Low [U] = min (Low [U], Low [V]); IF (Low [V] > DFN [u]) { / * in adjacent nodes and u, there is one node is the smallest timestamp than the current u order to access large, that is to say the point u can be reached by the fruit, then between their adjacent side is the bridge * / cnt ++; care [cnt] .u = u; care [cnt] .v = v; if (care [cnt] .u> care [cnt] .v) { swap (care [cnt] .u, care [cnt] .v); } } } Else if (low [u]> dfn [v]) medium [u] = min (low [in], DFN [v]); } } Void init () { MEMS (care, 0 , sizeof (care)); MEMS (low, 0 , sizeof (low)); MEMS (ver, 0 , sizeof(ver)); memset(dfn,0,sizeof(dfn)); memset(Next,0,sizeof(Next)); memset(head,0,sizeof(head)); cnt=0; num=0; tot=1; } bool cmp(node a,node b){ if (a.u==b.u)return a.v<b.v; return a.u<b.u; } int main(){ int id,tmp,nx; while(~scanf("%d",&n)){ if (n==0){ printf("0 critical links\n\n"); continue; } init(); for(int i=1;i<=n;i++){ scanf("%d",&id); id++; getchar(); getchar(); scanf("%d",&tmp); getchar(); for (int j=1;j<=tmp;j++){ scanf("%d",&nx); nx++; add(id,nx); add(nx,id); } } for (int i=1;i<=n;i++){ if(!dfn[i])tarjan(i,i); } printf("%d critical links\n",cnt); sort(brige+1,brige+1+cnt,cmp); for (int i=1;i<=cnt;i++){ printf("%d - %d\n",brige[i].u-1,brige[i].v-1); } printf("\n"); } return 0; } /* 3 critical links 0 - 1 3 - 4 6 - 7 */