Input:
8 9
1 2
1 3
2 4
2 5
3 6
3 7
4 8
5 8
6 7
// Figure BFs, using the C ++ queue
# Include <stdio. h>
# Include <string. h>
# Include <queue>
Using namespace STD;
# Define N 10
Int G [N] [N], BZ [N], n, m;
Queue <int> q;
Void BFS (INT cur)
{Int J;
BZ [cur] = 1; q. Push (cur );
While (! Q. Empty ())
{Cur = Q. Front (); printf ("V % d", cur); q. Pop ();
For (j = 1; j <= N; j ++)
If (BZ [J] = 0 & G [cur] [J] = 1) {q. Push (j); BZ [J] = 1 ;}
}
}
Void input ()
{Int I, j, F, T;
Scanf ("% d", & N, & M );
For (INT I = 1; I <= m; I ++)
{Scanf ("% d", & F, & T); G [f] [T] = G [T] [f] = 1 ;}
}
Int main ()
{Memset (G, 0, sizeof (g); memset (BZ, 0, sizeof (BZ ));
Input (); BFS (1 );
}
**************************************** **************************************** *****
// Figure BFS
# Include <stdio. h>
# Include <string. h>
# Define N 10
Int G [N] [N], BZ [N], n, m, Q [N], QE, QS;
Void BFS (INT cur)
{Int J, QS, QE;
Qs = Qe = 0; BZ [cur] = 1; Q [QE ++] = cur;
While (QS <QE)
{Cur = Q [QS ++]; printf ("V % d", cur );
For (j = 1; j <= N; j ++)
{If (BZ [J] = 0 & G [cur] [J] = 1)
{Q [QE ++] = J; BZ [J] = 1 ;}
}
}
}
Void input ()
{Int I, j, F, T;
Scanf ("% d", & N, & M );
For (INT I = 1; I <= m; I ++)
{Scanf ("% d", & F, & T); G [f] [T] = G [T] [f] = 1 ;}
}
Int main ()
{Int I, J;
Memset (G, 0, sizeof (g); memset (BZ, 0, sizeof (BZ ));
Input (); BFS (1 );
}