๐ŸŽฒ ๋ฐฑ์ค€ 1389๋ฒˆ ์ผ€๋นˆ ๋ฒ ์ด์ปจ์˜ 6๋‹จ๊ณ„ ๋ฒ•์น™

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

๋ฐฑ์ค€

๋ชฉ๋ก ๋ณด๊ธฐ
86/186

๋ฐฑ์ค€ 1389๋ฒˆ

๐Ÿ’Š ์ฒ˜์Œ์— ์‹œ์ž‘์ ๊ณผ ๋์ ์„ ์„ค์ •ํ•ด์•ผํ•œ๋‹ค ์ƒ๊ฐํ–ˆ๋”๋‹ˆ ๋ณต์žกํ•ด์กŒ๋Š”๋ฐ ์‹œ์ž‘์ ๋งŒ ์ƒ๊ฐํ•˜๊ณ  ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•ด๋ฉด ๋” ๊ฐ„๋‹จํžˆ ํ’€ ์ˆ˜ ์žˆ๋‹ค.
๐Ÿ’Š ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•  ๋•Œ๋Š” BFS๋ฅผ ์‚ฌ์šฉํ•˜์ž.

const fs = require('fs'); 
const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');

const [N, M] = input.shift().split(" ").map(Number);

const graph = Array.from(new Array(N + 1), () => []);

let result = [0, Infinity];

for (let i = 0; i < M; i++) {
  const [a, b] = input[i].split(" ").map(Number);
  graph[a].push(b);
  graph[b].push(a);
}

const BFS = (start) => {
  let willCheck = [[start, 0]];
  let checked = new Array(N + 1).fill(0);
  let bakon = new Array(N + 1).fill(0);
  checked[start] = 1;
  while (willCheck.length) {
    const [node, count] = willCheck.shift();
    bakon[node] = count;
    for (let i = 0; i < graph[node].length; i++) {
      const next = graph[node][i];
      if (checked[next] === 0) {
        checked[next] = 1;
        let nextCount = count + 1;
        willCheck.push([next, nextCount]);
      }
    }
  }

  return bakon.reduce((a, b) => a + b, 0);
};

for (let i = 1; i <= N; i++) {
  const sum = BFS(i);
  if (result[1] > sum) {
    result = [i, sum];
  }
}

console.log(result[0]);

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