package graph; import java.util.HashSet; import java.util.Iterator; public class Ranking { public static void main(String[] args) { SolutionRanking su = new SolutionRanking(); int n = 5; int[][] results = {{4, 3}, {4, 2}, {3, 2}, {1, 2}, {2, 5}}; // 2 System.out.println(su.solution(n, results)); } } class SolutionRanking { public int solution(int n, int[][] results) { int answer = 0; // 그래프 생성을 위한 공간 할당 Graph[] graph = new Graph[n+1]; for(int i=1; i i = graph[curr].win.iterator(); i.hasNext();) { int temp = Integer.parseInt(i.next().toString()); check = findWinner(graph, temp, check); } } return check; } public boolean[] findLoser(Graph[] graph, int curr, boolean[] check) { if(!check[curr]) { check[curr] = true; for(Iterator i = graph[curr].lose.iterator(); i.hasNext();) { int temp = Integer.parseInt(i.next().toString()); check = findLoser(graph, temp, check); } } return check; } } class Graph { HashSet win; HashSet lose; Graph() { win = new HashSet(); lose = new HashSet(); } @Override public String toString() { return "Graph [win=" + win + ", lose=" + lose + "]"; } }