๐ŸŽฒ ๋ฐฑ์ค€ 1005๋ฒˆ ACM Craft

Jeongeunยท2023๋…„ 11์›” 14์ผ
0

๋ฐฑ์ค€

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

๋ฐฑ์ค€ 1005๋ฒˆ

๐Ÿ’ก ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ์ตœ์ข…์ ์œผ๋กœ ๊ตฌํ•˜๋ ค๋Š” ๊ฑด๋ฌผ๋ถ€ํ„ฐ ์‹œ์ž‘ํ–ˆ๋‹ค.
๐Ÿ’ก node ๋ฐฐ์—ด์˜ index๋Š” ๊ฑด๋ฌผ ๋ฒˆํ˜ธ์ด๊ณ  ๊ฐ’์€ ์ด ๊ฑด๋ฌผ์„ ๊ฑด์„คํ•˜๊ธฐ ์œ„ํ•ด ์™„์„ฑ๋˜์–ด์•ผํ•˜๋Š” ๊ฑด๋ฌผ ๋ฒˆํ˜ธ๋ฅผ ์ €์žฅํ•œ๋‹ค.

์ฝ”๋“œ

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

for (let c = 0; c < input.length; c++) {
  const [N, K] = input[c].split(" ").map(Number);
  const sec = input[c + 1].split(" ").map(Number);
  sec.unshift(0);
  const node = Array.from(new Array(N + 1), () => []);
  for (let k = 0; k < K; k++) {
    const [X, Y] = input[c + 2 + k].split(" ").map(Number);
    node[Y].push(X);
  }
  const dp = new Array(N + 1).fill(-1);

  const dpValue = (index) => {
    if (dp[index] !== -1) return dp[index];
    if (node[index].length === 0) {
      dp[index] = sec[index];
      return sec[index];
    } else {
      let max = -1;

      for (let i = 0; i < node[index].length; i++) {
        if (max < dpValue(node[index][i])) max = dpValue(node[index][i]);
      }
      dp[index] = max + sec[index];
      return max + sec[index];
    }
  };

  console.log(dpValue(input[c + 2 + K]));

  c = c + 2 + K;
}

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

comment-user-thumbnail
2023๋…„ 11์›” 14์ผ

์ข‹์€ ๊ธ€ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์ž์ฃผ ๋ฐฉ๋ฌธํ• ๊ฒŒ์š” :)

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