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; }