dfs 깊이
dfs 돌리기
const input = require("fs").readFileSync("dev/stdin").toString().trim().split("\n");
const [n, m, r] = input[0].split(" ").map(Number);
const graph = Array.from({ length: n + 1 }, () => []);
for (let i = 1; i <= m; i++) {
const [a, b] = input[i].split(" ").map(Number);
graph[a].push(b);
graph[b].push(a);
}
graph.forEach((v) => v.sort((a, b) => a - b));
// console.log(graph);
const visited = new Array(n + 1).fill(-1);
function dfs(cur, dep) {
visited[cur] = dep;
for (const next of graph[cur]) {
if (visited[next] === -1) {
dfs(next, dep + 1);
}
}
}
dfs(r, 0);
console.log(visited.slice(1).join("\n"));