bfs遍历有向图,用的vector结果老是出错
#include <iostream>
#include <string.h>
#include <vector>
#include <queue>
using namespace std;
const int maxn = 1000001;
int n, m;
bool vis[maxn];
vector<int> map[maxn];
queue<int> q;
int main (){
memset (vis, 0, sizeof (vis));
cin >> n >> m;
for (int i = 0; i < m; i++){
int a, b;
cin >> a >> b;
map[a].push_back (b);
}
q.push (1);
while (!q.empty ()){
cout << q.front () << " ";
vis[q.front ()] = 1;
for (int i = 1; i <= map[q.front ()].size (); i++){
if (!vis[map[q.front ()][i]]){
vis[map[q.front ()][i]] = 1;
q.push (map[q.front ()][i]);
}
}
q.pop ();
}
return 0;
}