๐ ์ถ๋ฐ์ ๋ ํ์ํ๋ค๊ณ checkํด์ฃผ๊ธฐ!!
์ฝ๋
const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
const [N, M, K, X] = input.shift().split(" ").map(Number);
const graph = Array.from(new Array(N + 1), () => []);
for (let i = 0; i < M; i++) {
const [a, b] = input[i].split(" ").map(Number);
graph[a].push(b);
}
const result = [];
const queue = [[X, 0]];
const checked = new Array(N + 1).fill(0);
checked[X] = 1;
while (queue.length) {
const [check, dis] = queue.shift();
for (let i = 0; i < graph[check].length; i++) {
const next = graph[check][i];
if (checked[next] === 0) {
if (dis + 1 === K) {
result.push(next);
} else {
queue.push([next, dis + 1]);
}
checked[next] = 1;
}
}
}
if (result.length === 0) {
console.log(-1);
} else {
result.sort((a, b) => a - b);
for (num of result) {
console.log(num);
}
}
์ ๋ณด๊ฐ ๋ง์ ๋ณด์ผ ์ ์์๋๋ฐ, ๊ธ์ ๋ช ํํ๊ฒ ์์ ํด์ฃผ์ ์ ์ดํดํ๊ธฐ ์ฌ์ ์ต๋๋ค. '์ถ๋ฐ์ ๋ ํ์ํ๋ค๊ณ checkํด์ฃผ๊ธฐ!!'๋ผ๋ ๋ถ๋ถ์ด ํฌ๊ฒ ์๋ฟ์์ด์. ์ข์ ํ ๊ฐ์ฌํฉ๋๋ค!