-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBOJ2056.java
More file actions
87 lines (70 loc) · 2.23 KB
/
BOJ2056.java
File metadata and controls
87 lines (70 loc) · 2.23 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class BOJ2056 {
static int n;
static int[] now, timeSum, inDegree;
static List<Integer>[] graph;
private static void input() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
//
n = Integer.parseInt(br.readLine());
//
now = new int[n + 1];
timeSum = new int[n + 1];
inDegree = new int[n + 1];
// set graph
graph = new List[n + 1];
for (int i = 1; i <= n; i++) {
graph[i] = new ArrayList<>();
}
for (int i = 1; i <= n; i++) {
st = new StringTokenizer(br.readLine());
// set now array
int time = Integer.parseInt(st.nextToken());
now[i] = time;
int count = Integer.parseInt(st.nextToken());
for (int j = 0; j < count; j++) {
int parent = Integer.parseInt(st.nextToken());
graph[parent].add(i);
inDegree[i]++;
}
}
}
public static void main(String[] args) throws IOException {
input();
process();
}
private static void process() {
topologicalSorting();
// print answer
int max = Integer.MIN_VALUE;
for (int i = 1; i <= n; i++) {
if (max < timeSum[i])
max = timeSum[i];
}
System.out.println(max);
}
private static void topologicalSorting() {
Queue<Integer> queue = new LinkedList<>();
// set init
for (int i = 1; i <= n; i++) {
if (inDegree[i] == 0) {
queue.add(i);
timeSum[i] = now[i];
}
}
while (!queue.isEmpty()) {
int thisVertex = queue.poll();
for (int adjacency : graph[thisVertex]) {
inDegree[adjacency]--;
timeSum[adjacency] = Math.max(timeSum[adjacency], timeSum[thisVertex] + now[adjacency]);
if (inDegree[adjacency] == 0) {
queue.add(adjacency);
}
}
}
}
}