bfs

queue q;
st[1] = true; // 表示1号点已经被遍历过
q.push(1);

while (q.size())
{
int t = q.front();
q.pop();

for (int i = h[t]; i != -1; i = ne[i])
{
    int j = e[i];
    if (!st[j])
    {
        st[j] = true; // 表示点j已经被遍历过
        q.push(j);
    }
}

}

posted on 2025-03-12 19:37  下头小美  阅读(5)  评论(0)    收藏  举报