티스토리 뷰

https://www.acmicpc.net/problem/11403

 

11403번: 경로 찾기

가중치 없는 방향 그래프 G가 주어졌을 때, 모든 정점 (i, j)에 대해서, i에서 j로 가는 경로가 있는지 없는지 구하는 프로그램을 작성하시오.

www.acmicpc.net

const fs = require('fs');
const input = fs.readFileSync("./dev/stdin").toString().trim().split("\n").map(v=>v.split(' ').map(x=>+x)); 
const answer = [];
const N = input.shift()[0];

for(let i = 0; i<N; i++){
  const temp = new Array(N).fill(0)
  let q  = [];
  q.push(i);

  while(q.length>0){
    
    const now = q.shift();
    input[now].forEach((v,i)=>{
      if(v==1&&temp[i]==0){
        q.push(i);
        temp[i] = 1;
      }
    })
  }
  answer.push(temp)
}

console.log(answer.map(v=>v.join(' ')).join('\n'))

 

728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함