Ultimate Template
View code
# Include <stdio. h>
# Include < String . H>
# Include <algorithm>
Using Namespace STD;
Const Int N = 10010 ;
Const Int M = 100010 ;
Int Min ( Int A, Int B ){
Return A> B? B:;
}
Struct Edge {
Int To, next, ID;
} Edge [ 2 * M];
Int N, m, TOT, tdfn, nbridge;
Int Head [N], vis [N], low [N], dfn [N], bridge [m];
Void Add (Int A, Int B, Int Edgeid ){
// This method is slow in determining the duplicate edge.
/* For (j = head [a]; J! =-1; j = edge [J]. Next ){
If (B = edge [J].)
Break;
}
If (J! =-1)
{
Edge [J]. Tag = 1;
Edge [J ^ 1]. Tag = 1;
Return;
} */
Edge [tot]. To = B;
Edge [tot]. ID = edgeid;
Edge [tot]. Next = head [a];
Head [a] = tot ++;
Edge [tot]. To =;
Edge [tot]. ID = edgeid;
Edge [tot]. Next = head [B];
Head [B] = tot ++;
}
Void DFS ( Int U, Int FA ){
Int I, V; low [u] = dfn [u] = ++ tdfn; vis [u] = 1 ;
For (I = head [u]; I! =- 1 ; I = edge [I]. Next ){
V = edge [I].;
If (Vis [v] = 1 & Edge [I]. ID! = FA) low [u] = min (low [u], dfn [v]);
Else If (! Vis [v])
{
DFS (v, edge [I]. ID );
Low [u] = min (low [u], low [v]);
If (Low [v]> dfn [u]) {
Bridge [edge [I]. ID] = ++ nbridge;
}
}
}
Vis [u] = 2 ;
}
Void Init (){
Int I;
Tdfn = 1 ; Tot = 0 ; Nbridge = 0 ;
For (I = 0 ; I <= N; I ++) head [I] =- 1 ;
For (I = 1 ; I <= m; I ++) Bridge [I] = 0 ;
For (I = 1 ; I <= N; I ++) vis [I] = 0 ;
}
Int Main ()
{
Int I, K, T;
Int U, V;
Scanf ( " % D " , & T );
While (T --){
Scanf ( " % D " , & N, & M );
Init ();
For (K = 1 ; K <= m; k ++)
{
Scanf ( " % D " , & U, & V );
Add (u, v, k );
}
DFS ( 1 , 0 );
Printf ( " % D \ n " , Nbridge );
For (I = 1 , K = nbridge; I <= m; I ++)
{
If (Bridge [I])
{
Printf ( " % D " , I );
If (-- K) printf ( " " );
}
}
If (Nbridge) printf ( " \ N " );
If (T) printf ( " \ N " );
}
Return 0 ;
}