Using Namespace: #Include #Include #Include #Include #Include #Include
Using Namespace: #Include #Include #Include #Include #Include #Include
#include
#include
#include
#include
#include
<iostream>
<fstream>
<vector>
<queue>
<stack>
<algorithm>
j+1
endl;
graph[j][k]+1
endl;
be done!"<<endl;
j+1
endl;
graph[j][k]+1
endl;
be done!"<<endl;
int main () {
ifstream cin("data.in");
int t,n,m,u,v;
//"t" is the number of test cases,
//"n" is the number of nodes and "m" is the number of edges
cin>>t;
while(t--){
cin>>n>>m;
graph.clear();
graph.resize(n);
vis.clear();
vis.resize(n);
fill_n(vis.begin(),n,false);
while (m--){
cin>>u>>v;
u--;
v--;
graph[u].push_back(v);
//for non oriented graphs add the reverse edge
graph[v].push_back(u);
}
//For connected non-oriented graphs or highly connected oriented graphs
cout<<"### New Test Case !!!"<<endl;
cout<<"Starting BFS ..."<<endl;
BFS(0);
cout<<"BFS terminated !"<<endl;
fill_n(vis.begin(),n,false); // clean vis for DFS
cout<<"Starting DFS ..."<<endl;
DFS(0);
cout<<"DFS terminated !"<<endl;
//For other graphs, given the number of node in the graph,
//we make a loop and invoke BFS or DFS for a node if it is not visited.
/*
for(int i=0;i<=n;i++){
if(!vis[i]){
BFS(i);
}
}
*/
}
return 0;
}