개발일지

[백준] 게임 개발(1516) 본문

알고리즘

[백준] 게임 개발(1516)

devbh 2020. 3. 19. 06:35


import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
    public static void main(String args[]) throws Exception {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        ArrayList<Integer>[] list = new ArrayList[N + 1];
        int[] indegree = new int[N + 1];
        int[] value = new int[N + 1];
        int[] result = new int[N + 1];
        int temp = 0;
        for (int i = 1; i <= N; i++) {
            list[i] = new ArrayList<Integer>();
        }
        for (int i = 1; i <= N; i++) {
            value[i] = sc.nextInt();
            temp = sc.nextInt();
            while (temp != -1) {
                indegree[i]++;
                list[temp].add(i);
                temp = sc.nextInt();
            }
        }

        Queue<Integer> queue = new LinkedList<Integer>();
        for (int i = 1; i <= N; i++) {
            if (indegree[i] == 0) {
                queue.add(i);
                result[i] = value[i];

            }
        }
        while (!queue.isEmpty()) {
            int current = queue.poll();
            for (int i = 0; i < list[current].size(); i++) {
                int next = list[current].get(i);
                indegree[next]--;
                result[next] = Math.max(result[next], result[current] + value[next]);

                if (indegree[next] == 0) {
                    queue.add(next);
                }
            }

        }
        for (int i = 1; i <= N; i++) {
            System.out.println(result[i]);
        }
    }
}

'알고리즘' 카테고리의 다른 글

[백준]단절점(11266)  (0) 2020.03.25
[백준] 교수님은 기다리지 않는다(3830)  (0) 2020.03.19
[백준] 키 순서(2458)  (0) 2020.03.19
[백준] LCA2(11438)  (0) 2020.03.03
[백준] 네트워크 연결 (1922)  (0) 2020.03.03
Comments