B11 宽搜(BFS)

视频链接:111 宽搜(BFS)_哔哩哔哩_bilibili

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

const int N=100010;
int n, m, a, b;
vector<int> e[N];
int vis[N];
queue<int> q;

void bfs(int s){
  vis[s]=1; q.push(s); 
  while(q.size()){
    int x=q.front(); q.pop();
    // printf("%d出队\n",x);
    for(auto y : e[x]){
      if(vis[y]) continue;
      vis[y]=1; q.push(y);
      // printf("  %d入队\n",y);
    }
  }
}
int main(){
  cin >> n >> m;
  for(int i=0; i<m; i++){
    cin >> a >> b;
    e[a].push_back(b);
    e[b].push_back(a);
  }
  bfs(1);
  return 0;
}

 

posted @ 2022-05-28 13:05  董晓  阅读(566)  评论(0编辑  收藏  举报