class Solution {
    public int solution(int n, int[][] results) {
        int answer = 0;
        int[][] graph = new int[n+1][n+1];
        
        for(int[] i : results){
            graph[i[0]][i[1]]=1; //이긴 경우
        }
        //간접적 승패 업데이트 (A->B, B->C => A->C)
        for(int i=1; i<=n; i++){
            for(int j=1; j<=n; j++){
                for(int z=1; z<=n; z++){
                    if(graph[j][i]==1 && graph[i][z]==1){
                        graph[j][z]=1;
                    }
                }
            }
        }
        //경기 횟수 카운트
        for(int i=1; i<=n; i++){
            int count = 0;
            for(int j=1; j<=n; j++){
                if(graph[i][j]==1 || graph[j][i]==1) count++;
            }
            if(count == n-1) answer++;
        }
        
        return answer;
    }
}

플로이드- 워셜 이용해야 하는 문제

코린이는 모르는게 넘 많습니다 ... 공부하자 아자🌱

*플로이드-워셜 : 모든 정점에서 모든 정점으로 가는 최단거리를 구할 수 있음

'NOTE > 프로그래머스' 카테고리의 다른 글

도둑질  (0) 2022.09.30
정수 삼각형  (0) 2022.09.30
사칙연산  (0) 2022.09.29
N으로 표현  (0) 2022.09.29
신고 결과 받기  (0) 2022.05.10

+ Recent posts