ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

Algorithm/Baekjoon

[Java] ๋ฐฑ์ค€ 2056๋ฒˆ -์ž‘์—…

๊ฐœ๋ฐœ๊ฐœ๊ตด๐Ÿธ 2022. 6. 5. 20:24

https://www.acmicpc.net/problem/2056

 

2056๋ฒˆ: ์ž‘์—…

์ˆ˜ํ–‰ํ•ด์•ผ ํ•  ์ž‘์—… N๊ฐœ (3 ≤ N ≤ 10000)๊ฐ€ ์žˆ๋‹ค. ๊ฐ๊ฐ์˜ ์ž‘์—…๋งˆ๋‹ค ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„(1 ≤ ์‹œ๊ฐ„ ≤ 100)์ด ์ •์ˆ˜๋กœ ์ฃผ์–ด์ง„๋‹ค. ๋ช‡๋ช‡ ์ž‘์—…๋“ค ์‚ฌ์ด์—๋Š” ์„ ํ–‰ ๊ด€๊ณ„๋ผ๋Š” ๊ฒŒ ์žˆ์–ด์„œ, ์–ด๋–ค ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด

www.acmicpc.net

[๋ฌธ์ œ ํ’€์ด]

์ด์ „์— ํ’€์—ˆ๋˜ ์œ„์ƒ์ •๋ ฌ์„ ์‚ฌ์šฉํ•œ ๋ฐฑ์ค€ ๊ฒŒ์ž„๊ฐœ๋ฐœ ๋ฌธ์ œ๋ž‘ ๊ฑฐ์˜ ๋น„์Šทํ•œ ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.

ํ’€์ด๋Š” ๊ฒŒ์ž„๊ฐœ๋ฐœ ํฌ์ŠคํŒ…์„ ์ฒจ๋ถ€ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

https://j-su2.tistory.com/4?category=1016202

 

[Java] ๋ฐฑ์ค€ 1516๋ฒˆ - ๊ฒŒ์ž„ ๊ฐœ๋ฐœ

https://www.acmicpc.net/problem/1516 1516๋ฒˆ: ๊ฒŒ์ž„ ๊ฐœ๋ฐœ ์ฒซ์งธ ์ค„์— ๊ฑด๋ฌผ์˜ ์ข…๋ฅ˜ ์ˆ˜ N(1 ≤ N ≤ 500)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ N๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ ๊ฑด๋ฌผ์„ ์ง“๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„๊ณผ ๊ทธ ๊ฑด๋ฌผ์„ ์ง“๊ธฐ ์œ„ํ•ด ๋จผ์ € ์ง€์–ด์ ธ์•ผ ํ•˜๋Š”..

j-su2.tistory.com

[์ •๋‹ต ์ฝ”๋“œ]

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    public static int N;
    public static int[] degree;
    public static int[] time;
    public static int[] maxTime;
    public static ArrayList<ArrayList<Integer>> graph = new ArrayList<ArrayList<Integer>>();
    public static PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        N = Integer.parseInt(br.readLine());
        degree = new int[N];
        time = new int[N];
        maxTime = new int[N];
        for(int i=0;i<N;i++) {
            graph.add(new ArrayList<Integer>());
        }
        for(int i=0;i<N;i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            time[i] = Integer.parseInt(st.nextToken());
            // ์˜์กด ๊ด€๊ณ„์ˆ˜ ๋ฐ›์Œ
            degree[i] = Integer.parseInt(st.nextToken());
            if (degree[i] == 0) {
                pq.add(i);
            }
            for (int j = 0; j < degree[i]; j++) {
                // 0๋ฒˆ~N-1๋ฒˆ์œผ๋กœ ๋„ฃ๊ธฐ ์œ„ํ•ด์„œ -1 ํ•ด์คŒ
                int num = Integer.parseInt(st.nextToken());
                graph.get(num - 1).add(i);
            }
        }
        findTime();
        Arrays.sort(time);
        System.out.println(time[N-1]);
    }
    public static void findTime() {
        while(!pq.isEmpty()) {
            int p = pq.poll();
            for(Integer i:graph.get(p)) {
                maxTime[i] = Math.max(maxTime[i], time[p]);
                degree[i] -= 1;
                if(degree[i] == 0) {
                    time[i] += maxTime[i];
                    pq.add(i);
                }
            }
        }
    }
}