voidbfs(int u){ while (!Q.empty()) Q.pop(); //清空队列,以作下用 Q.push(u); //放入结点u vis[u] = 1; //标记u d[u] = 0; //用以记录最短距离,视情况用 p[u] = -1; //记录是从哪个节点走到当前节点的 while (!Q.empty()) { u = Q.front(); //得到队首u Q.pop(); for (int i = head[u]; i; i = e[i].nxt) { //访问连接u的边 if (!vis[e[i].to]) { Q.push(e[i].to); //放入队列 vis[e[i].to] = 1; d[e[i].to] = d[u] + 1; //显然多一条边,距离就+1 p[e[i].to] = u; //途径此边 } } } }
voidrestore(int x){ vector<int> res; for (int v = x; v != -1; v = p[v]) { res.push_back(v); } std::reverse(res.begin(), res.end()); for (int i = 0; i < res.size(); ++i) printf("%d", res[i]); puts(""); }