๐ŸŽฒ ๋ฐฑ์ค€ 18352๋ฒˆ ํŠน์ • ๊ฑฐ๋ฆฌ์˜ ๋„์‹œ ์ฐพ๊ธฐ

Jeongeunยท2023๋…„ 7์›” 21์ผ
0

๋ฐฑ์ค€

๋ชฉ๋ก ๋ณด๊ธฐ
98/187

๋ฐฑ์ค€ 18352๋ฒˆ

๐Ÿ’Š ์ถœ๋ฐœ์ ๋„ ํƒ์ƒ‰ํ–ˆ๋‹ค๊ณ  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);
  }
}

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2023๋…„ 7์›” 21์ผ

์ •๋ณด๊ฐ€ ๋งŽ์•„ ๋ณด์ผ ์ˆ˜ ์žˆ์—ˆ๋Š”๋ฐ, ๊ธ€์„ ๋ช…ํ™•ํ•˜๊ฒŒ ์„œ์ˆ ํ•ด์ฃผ์…”์„œ ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์› ์Šต๋‹ˆ๋‹ค. '์ถœ๋ฐœ์ ๋„ ํƒ์ƒ‰ํ–ˆ๋‹ค๊ณ  checkํ•ด์ฃผ๊ธฐ!!'๋ผ๋Š” ๋ถ€๋ถ„์ด ํฌ๊ฒŒ ์™€๋‹ฟ์•˜์–ด์š”. ์ข‹์€ ํŒ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ