/
순위.cpp
44 lines (35 loc) · 993 Bytes
/
순위.cpp
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* problem : https://programmers.co.kr/learn/courses/30/lessons/49191
* algorithm : Floyd
* time complexity : O(N^3)
*/
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int solution(int n, vector<vector<int>> results) {
vector<vector<bool>> matches(n, vector<bool>(n, false));
for(auto result : results) {
int winner = result[0] - 1;
int loser = result[1] - 1;
matches[winner][loser] = true;
}
for(int k=0; k<n; k++){
for(int u = 0; u < n; u++){
for(int v = 0; v < n; v++){
if(u == v) continue;
matches[u][v] = matches[u][v] || (matches[u][k] && matches[k][v]);
}
}
}
int answer = 0;
for(int u = 0; u < n; u++){
int sum = 0;
for(int v = 0; v < n; v++){
sum += matches[u][v] + matches[v][u];
}
answer += (sum == n-1);
}
return answer;
}