BFS
BFS
一.代码实现
原理
通过创建临时队列,实现从近到远扩散式的遍历
vector<int>e[N];
int vis[N];
queue<int>q;void bfs()
{vis[1]=1; q.push(1);//起点while(!q.empty())//如果队列不为空{int cur=q.front();//将队首元素设为curq.pop();//弹出队首for(auto y:e[cur])//遍历队首元素的孩子{if(!vis[y])//如果没访问过的话,{q.push(y);//就加入队列vis[y]=1;//这个现在被访问过了}}}
}