플로이드-와샬
https://school.programmers.co.kr/learn/courses/30/lessons/49191
class Solution {
public int solution(int n, int[][] results) {
int[][] rank = new int[n + 1][n + 1];
for(int[] result : results) {
rank[result[0]][result[1]] = 1;
rank[result[1]][result[0]] = -1;
}
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) {
for(int k=1; k<=n; k++) {
if(rank[i][k] == 1 && rank[k][j] == 1) {
rank[i][j] = 1;
rank[j][i] = -1;
}
if(rank[i][k] == -1 && rank[k][j] == -1) {
rank[i][j] = -1;
rank[j][i] = 1;
}
}
}
}
int answer = 0;
for(int i=1; i<=n; i++) {
int cnt = 0;
for(int j=1; j<=n; j++) {
if(rank[i][j] != 0) {
cnt++;
}
}
if(cnt == n-1) {
answer++;
}
}
return answer;
}
}
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) {
for(int k=1; k<=n; k++) {
if(rank[i][k] == 1 && rank[k][j] == 1) {
rank[i][j] = 1;
rank[j][i] = -1;
}
if(rank[i][k] == -1 && rank[k][j] == -1) {
rank[i][j] = -1;
rank[j][i] = 1;
}
}
}
}
for(int i=1; i<=n; i++) {
int cnt = 0;
for(int j=1; j<=n; j++) {
if(rank[i][j] != 0) {
cnt++;
}
}
if(cnt == n-1) {
answer++;
}
}
1시간 30분