Skip to content

Commit 31ce437

Browse files
beberichetony9402
andauthored
[ADD] baekjoon 2637 java solution (#91)
* [ADD] baekjoon 2637 java solution * Refactoring --------- Co-authored-by: Minsang Kim <[email protected]>
1 parent f58b175 commit 31ce437

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

solutions/baekjoon/2637/Main.java

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Authored by: beberiche
2+
// Co-authored by:
3+
// Link: http://boj.kr/7c016fb410f24cd3995d7c6b25b945f4
4+
5+
import java.util.*;
6+
import java.io.*;
7+
8+
public class Main {
9+
public static void main(String[] args) {
10+
FastReader rd = new FastReader();
11+
int N = rd.nextInt();
12+
int M = rd.nextInt();
13+
int[] outDegree = new int[N + 1]; // 진출차수
14+
int[] inDegree = new int[N + 1]; // 진입차수
15+
List<int[]> list[] = new ArrayList[N + 1];
16+
for (int i = 1; i <= N; i++) {
17+
list[i] = new ArrayList<>();
18+
}
19+
20+
for (int i = 0; i < M; i++) {
21+
int n1 = rd.nextInt();
22+
int n2 = rd.nextInt();
23+
int cnt = rd.nextInt();
24+
25+
list[n1].add(new int[]{n2, cnt}); // 방향 그래프
26+
outDegree[n1]++;
27+
inDegree[n2]++;
28+
}
29+
30+
Queue<Integer> q = new LinkedList<>();
31+
32+
int[] dp = new int[N + 1];
33+
q.add(N);
34+
dp[N] = 1;
35+
while (!q.isEmpty()) {
36+
int curr = q.poll();
37+
38+
for (int[] next : list[curr]) {
39+
dp[next[0]] += dp[curr] * next[1];
40+
inDegree[next[0]]--;
41+
if (inDegree[next[0]] == 0) {
42+
q.add(next[0]);
43+
}
44+
}
45+
}
46+
StringBuilder sb = new StringBuilder();
47+
for (int i = 1; i <= N; i++) {
48+
if (outDegree[i] == 0) {
49+
sb.append(i).append(" ").append(dp[i]).append("\n");
50+
}
51+
}
52+
System.out.print(sb.toString());
53+
}
54+
55+
56+
static class FastReader {
57+
BufferedReader br;
58+
StringTokenizer st;
59+
60+
public FastReader() {
61+
br = new BufferedReader(new InputStreamReader(System.in));
62+
}
63+
64+
String next() {
65+
while (st == null || !st.hasMoreElements()) {
66+
try {
67+
st = new StringTokenizer(br.readLine());
68+
} catch (IOException e) {
69+
e.printStackTrace();
70+
}
71+
}
72+
return st.nextToken();
73+
}
74+
75+
int nextInt() {
76+
return Integer.parseInt(next());
77+
}
78+
79+
long nextLong() {
80+
return Long.parseLong(next());
81+
}
82+
83+
double nextDouble() {
84+
return Double.parseDouble(next());
85+
}
86+
87+
String nextLine() {
88+
String str = "";
89+
try {
90+
str = br.readLine();
91+
} catch (IOException e) {
92+
e.printStackTrace();
93+
}
94+
return str;
95+
}
96+
}
97+
}
98+
99+
/* Solution Description
100+
101+
1. 위상 정렬 응용 문제. 주어진 입력값에 대해 기본 부품, 중간 부품을 구분 하는 방법을 알면 쉽게 풀 수 있다.
102+
103+
2. 완제품으로 부터, 필요한 부품들을 찾아 그래프를 형성하는 경우,
104+
기본 부품은 리프 노드에 해당하며, 더이상 탐색이 진행 되지 않는,
105+
즉 `outDegree`(진출차수) 가 `0` 인 노드를 의미한다.
106+
반대로 `outDegree` 가 `0` 보다 크다면, 해당 노드는 완제품 혹은 중간 부품에 해당한다.
107+
108+
3. 해당 문제의 경우, `N` 이 완제품이므로,
109+
`N` 을 제외한 `outDegree` 가 `0` 보다 큰 노드는 모두 중간 부품이 된다.
110+
111+
4. 입력 값 마다 `inDegree` 와 `outDegree` 를 구한 후,
112+
완제품 `N` 을 시작으로 위상 정렬 탐색을 진행한다.
113+
만약 부모 노드가 `n` 개 필요한 경우,
114+
필요한 부품은 $n*cnt_{idx}$ 의 총합이 된다. 이를 `dp` 점화식으로 표현하자면 다음과 같다.
115+
$dp[p] = dp[p] + (dp[c] * cnt)$
116+
117+
5. 누적된 `dp[]` 내에서, 기본 제품만 골라 해당 노드의 인덱스와 누적값을 출력하면 정답이 된다.
118+
119+
*/

0 commit comments

Comments
 (0)