[백준] 단절점과 단절선 #14675

welchs·2022년 2월 27일
0

알고리즘

목록 보기
40/44
post-thumbnail

후기

개념 자체를 알고 있으면 쉬운 문제...
싸이클이 없는 노드의 경우 무조건 해당 간선이 단절선이 된다.
그리고 해당 정점에서 연결된 간선이 2개 이상일 경우 해당 정점이 단절점이 된다.

Node.js 풀이

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

const N = Number(input[0]);
const data = input.slice(1, N);
const q = input.slice(N + 1);

const solution = (N, data, q) => {
  const nodes = Array.from(Array(N + 1), () => new Array());
  data.forEach((v) => {
    const [n1, n2] = v.split(' ').map(Number);
    nodes[n1].push(n2);
    nodes[n2].push(n1);
  });
  let answer = '';
  q.forEach((qq) => {
    const [t, k] = qq.split(' ').map(Number);
    if (t === 1) {
      answer += nodes[k].length > 1 ? 'yes\n' : 'no\n';
    } else if (t === 2) {
      answer += 'yes\n';
    }
  });
  return answer;
};

console.log(solution(N, data, q));
profile
고수가 되고 싶은 조빱

0개의 댓글