|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.util.*; |
| 6 | + |
| 7 | +class Main { |
| 8 | + |
| 9 | + static int nodeCnt,ans; |
| 10 | + static Node[] nodes; |
| 11 | + |
| 12 | + static class Node{ |
| 13 | + int startTime = 0; |
| 14 | + int cost; |
| 15 | + int parentLeft; |
| 16 | + |
| 17 | + HashSet<Node> children = new HashSet<>(); |
| 18 | + |
| 19 | + public Node() { |
| 20 | + |
| 21 | + } |
| 22 | + |
| 23 | + } |
| 24 | + |
| 25 | + public static void main(String[] args) throws NumberFormatException, IOException { |
| 26 | + init(); |
| 27 | + process(); |
| 28 | + print(); |
| 29 | + |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | + public static void init() throws NumberFormatException, IOException { |
| 34 | + BufferedReader br= new BufferedReader(new InputStreamReader(System.in));; |
| 35 | + nodeCnt = Integer.parseInt(br.readLine()); |
| 36 | + nodes = new Node[nodeCnt+1]; |
| 37 | + ans = 0; |
| 38 | + |
| 39 | + for (int i = 1; i <=nodeCnt; i++) { |
| 40 | + nodes[i] = new Node(); |
| 41 | + } |
| 42 | + |
| 43 | + for (int i = 1; i <= nodeCnt; i++) { |
| 44 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 45 | + int cost = Integer.parseInt(st.nextToken()); |
| 46 | + |
| 47 | + nodes[i].cost = cost; |
| 48 | + |
| 49 | + int parentCnt = Integer.parseInt(st.nextToken()); |
| 50 | + nodes[i].parentLeft = parentCnt; |
| 51 | + for (int j = 0; j < parentCnt; j++) { |
| 52 | + int nodeNum = Integer.parseInt(st.nextToken()); |
| 53 | + nodes[nodeNum].children.add(nodes[i]); |
| 54 | + } |
| 55 | + |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public static void process() throws IOException { |
| 60 | + Queue<Node> q = new LinkedList<>(); |
| 61 | + |
| 62 | + for (int i = 1; i<= nodeCnt; i++) { |
| 63 | + if (nodes[i].parentLeft == 0) q.add(nodes[i]); |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + while(!q.isEmpty()) { |
| 68 | + Node node = q.poll(); |
| 69 | + |
| 70 | + int newNum = node.cost + node.startTime; |
| 71 | + ans = Math.max(newNum, ans); |
| 72 | + |
| 73 | + for (Node child: node.children) { |
| 74 | + child.parentLeft--; |
| 75 | + child.startTime = Math.max(child.startTime, newNum); |
| 76 | + if (child.parentLeft == 0) q.add(child); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + public static void print() { |
| 83 | + System.out.println(ans); |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
0 commit comments