๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿง  ์•Œ๊ณ ๋ฆฌ์ฆ˜/์œ ํ˜•๋ณ„ ๋Œ€ํ‘œ๋ฌธ์ œ

[์œ„์ƒ์ •๋ ฌ][๋ฐฑ์ค€] 2252 JS ํ’€์ด

by megan07 2023. 12. 28.

 

์œ„์ƒ์ •๋ ฌ

: ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์—์„œ ๋…ธ๋“œ๋“ค๊ฐ„ ์„ ํ–‰ ์ˆœ์„œ๋ฅผ ์œ„๋ฐฐํ•˜์ง€ ์•Š๊ณ  ์ •๋ ฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜

 

๊ตฌํ˜„๋ฒ•

1. ๋งจ ์ฒ˜์Œ ๋ชจ๋“  ๊ฐ„์„ ์„ ์ฝ์œผ๋ฉฐ indegree ํ…Œ์ด๋ธ”์„ ์ฑ„์šด๋‹ค
2. indegree๊ฐ€ 0์ธ ์ •์ ๋“ค์„ ๋ชจ๋‘ ํ์— ๋„ฃ๋Š”๋‹ค
3. ํ์—์„œ ์ •์ ์„ ๊บผ๋‚ด์–ด ์œ„์ƒ ์ •๋ ฌ ๊ฒฐ๊ณผ์— ์ถ”๊ฐ€ํ•œ๋‹ค
4. ํ•ด๋‹น ์ •์ ์œผ๋กœ๋ถ€ํ„ฐ ์—ฐ๊ฒฐ๋œ ๋ชจ๋“  ์ •์ ์˜ indegree ๊ฐ’์„ 1 ๊ฐ์†Œ์‹œํ‚จ๋‹ค. ์ด ๋•Œ indegree๊ฐ€ 0์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ทธ ์ •์ ์„ ํ์— ์ถ”๊ฐ€ํ•œ๋‹ค
5. ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ 3,4๋ฒˆ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค
์ข€ ๋” ์ž์„ธํ•œ ์„ค๋ช…์ด ํ•„์š”ํ•˜๋‹ค๋ฉด
์ถœ์ฒ˜: ๋ฐ”ํ‚น๋… ๋ธ”๋กœ๊ทธ
https://blog.encrypted.gg/1020

 

์œ„์ƒ ์ •๋ ฌ ๋ฌธ์ œ์— ์ ์šฉํ•˜๊ธฐ

 

๋ฌธ์ œ 

N๋ช…์˜ ํ•™์ƒ๋“ค์„ ํ‚ค ์ˆœ์„œ๋Œ€๋กœ ์ค„์„ ์„ธ์šฐ๋ ค๊ณ  ํ•œ๋‹ค. ๊ฐ ํ•™์ƒ์˜ ํ‚ค๋ฅผ ์ง์ ‘ ์žฌ์„œ ์ •๋ ฌํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๊ฒ ์ง€๋งŒ, ๋งˆ๋•…ํ•œ ๋ฐฉ๋ฒ•์ด ์—†์–ด์„œ ๋‘ ํ•™์ƒ์˜ ํ‚ค๋ฅผ ๋น„๊ตํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ํ•˜์˜€๋‹ค. ๊ทธ๋‚˜๋งˆ๋„ ๋ชจ๋“  ํ•™์ƒ๋“ค์„ ๋‹ค ๋น„๊ตํ•ด ๋ณธ ๊ฒƒ์ด ์•„๋‹ˆ๊ณ , ์ผ๋ถ€ ํ•™์ƒ๋“ค์˜ ํ‚ค๋งŒ์„ ๋น„๊ตํ•ด ๋ณด์•˜๋‹ค.

์ผ๋ถ€ ํ•™์ƒ๋“ค์˜ ํ‚ค๋ฅผ ๋น„๊ตํ•œ ๊ฒฐ๊ณผ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ค„์„ ์„ธ์šฐ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— N(1 ≤ N ≤ 32,000), M(1 ≤ M ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. M์€ ํ‚ค๋ฅผ ๋น„๊ตํ•œ ํšŒ์ˆ˜์ด๋‹ค. ๋‹ค์Œ M๊ฐœ์˜ ์ค„์—๋Š” ํ‚ค๋ฅผ ๋น„๊ตํ•œ ๋‘ ํ•™์ƒ์˜ ๋ฒˆํ˜ธ A, B๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด๋Š” ํ•™์ƒ A๊ฐ€ ํ•™์ƒ B์˜ ์•ž์— ์„œ์•ผ ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.

ํ•™์ƒ๋“ค์˜ ๋ฒˆํ˜ธ๋Š” 1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ์ด๋‹ค.

 

์ ‘๊ทผ๋ฒ•

1. ๊ทธ๋ž˜ํ”„ ์ธ์ ‘๋ฆฌ์ŠคํŠธ ๋งŒ๋“ค๊ธฐ
2. ๊ทธ๋ž˜ํ”„ indegree ๋ฐฐ์—ด ๋งŒ๋“ค๊ธฐ
3. indegree๊ฐ€ 0์ธ ์ •์ ๋“ค ํ์— ๋„ฃ๊ธฐ
4. indegree๊ฐ€ 0์ธ ์ •์ ๋“ค์˜ ๋ชจ๋“  ์ธ์ ‘ ์ •์ ๋“ค์˜ indegree๊ฐ’ -1
5. ๋งŒ์•ฝ ์ธ์ ‘ ์ •์ ์˜ indegree๊ฐ€ 0์ด ๋˜๋ฉด ํ์— ๋„ฃ์–ด์คŒ
6. ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต

 

 

class Node {
  constructor(val) {
    this.val = val;
    this.next = null;
  }
}

class Queue {
  constructor() {
    this.front = null;
    this.rear = null;
    this.length = 0;
  }

  enqueue(val) {
    const newNode = new Node(val);
    if (!this.front) {
      this.front = newNode;
      this.rear = newNode;
    } else {
      this.rear.next = newNode;
      this.rear = newNode;
    }

    return ++this.length;
  }

  dequeue() {
    if (!this.front) return null;

    const tmp = this.front;
    if (this.front === this.rear) {
      this.rear = null;
    }
    this.front = this.front.next;
    this.length--;

    return tmp.val;
  }
}

const filePath = process.platform === 'linux' ? '/dev/stdin' : './input.txt';


// ์ž…๋ ฅ๊ฐ’์ด ์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ์ž…๋ ฅ ๊ฐ’์˜ ๊ธธ์ด(n), n๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ์„œ ํ•œ ์ค„์— ํ•˜๋‚˜์˜ ์ž…๋ ฅ๊ฐ’์ด ์ฃผ์–ด์งˆ ๋•Œ
const [n, ...input] = require('fs')
  .readFileSync(filePath)
  .toString()
  .trim()
  .split('\n');

function solution(N, M, list) {
  const adj = {};
  const indegree = new Array(N + 1).fill(0);
  const queue = new Queue();
  const result = [];

  list.forEach(([v1, v2]) => {
    if (!adj[v2]) adj[v2] = [];

    //๊ทธ๋ž˜ํ”„ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ ๋งŒ๋“ค๊ณ 
    //indegree ๋ฐฐ์—ด ์ฑ„์šฐ๊ธฐ
    adj[v2].push(v1);
    indegree[v1]++;
  });

  //indegree 0์ธ ์ •์ ๋“ค ํ์— ๋„ฃ๊ธฐ
  indegree.forEach((v, idx) => {
    if (idx !== 0 && v === 0) {
      queue.enqueue(idx);
      result.push(idx);
    }
  });

  while (queue.length) {
    const v = queue.dequeue();

    //๋ชจ๋“  ์ •์ ๋“ค์˜ indegree ๊ฐ’ -1
    adj[v]?.forEach((v2) => {
      if (indegree[v2] !== 0) {
        indegree[v2]--;

        //indegree๊ฐ’์ด 0์ด๋ผ๋ฉด ํ์— ๋„ฃ์–ด์คŒ
        if (indegree[v2] === 0) {
          result.push(v2);
          queue.enqueue(v2);
        }
      }
    });
  }

  return result.reverse().join(' ');
}

const arr = n.split(' ').map(Number);
const answer = solution(
  arr[0],
  arr[1],
  input.map((r) => r.split(' ').map(Number)),
);

console.log(answer);