-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSolution.java
53 lines (41 loc) ยท 1.16 KB
/
Solution.java
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
44
45
46
47
48
49
50
51
52
53
package Graph.prg49191;
class Solution {
int[][] graph;
public int solution(int n, int[][] results) {
graph = new int[n+1][n+1];
for (int[] res : results) {
graph[res[0]][res[1]] = 1;
} // graph[A][B] == 1 : A๋ B๋ฅผ ์ด๊ฒผ๋ค.
for (int i = 1; i <= n; i++) {
graph[i][0] = -1;
}
for (int i = 1; i <= n; i++) {
dfs(i, n);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
graph[0][j] += graph[i][j];
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (graph[0][i] + graph[i][0] == n-1) ans ++;
}
return ans;
}
void dfs(int cur, int n) {
for (int i = 1; i <= n; i++) {
if (graph[cur][i] == 1) {
if (graph[i][0] == -1) dfs(i, n);
for (int j = 1; j <= n; j++) {
if (graph[i][j] == 1) graph[cur][j] = 1;
}
}
}
int cnt = 0;
for (int i = 1; i <= n; i++) {
cnt += graph[cur][i];
}
graph[cur][0] = cnt;
}
}