菜鸡求助
  • 板块学术版
  • 楼主Gunpowder_OI
  • 当前回复2
  • 已保存回复2
  • 发布时间2020/8/19 10:57
  • 上次更新2023/11/6 19:57:30
查看原帖
菜鸡求助
316855
Gunpowder_OI楼主2020/8/19 10:57

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;
}
2020/8/19 10:57
加载中...