Algorithm/Baekjoon

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

๊ฐœ๋ฐœ๊ฐœ๊ตด๐Ÿธ 2022. 5. 29. 19:01

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

 

1516๋ฒˆ: ๊ฒŒ์ž„ ๊ฐœ๋ฐœ

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

www.acmicpc.net

----------------------------------------------------------------------------------------------------------------------

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

์ด ๋ฌธ์ œ๋Š” ํŠน์ˆ˜์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ ์œ„์ƒ์ •๋ ฌ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.

( ์œ„์ƒ์ •๋ ฌ์„ ๋ชฐ๋ผ์„œ ํ•œ์ฐธ ํ—ค๋งค๋‹ค๊ฐ€ ๊ฒฐ๊ตญ ํ’€์ด๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ํ’€์—ˆ๋„ค์š”ใ… ใ…  )

์•„๋ž˜์˜ ๋ธ”๋กœ๊ทธ๊ฐ€ ์œ„์ƒ์ •๋ ฌ์„ ์ดํ•ดํ•˜๋Š”๋ฐ ๋งŽ์€ ๋„์›€์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

https://codingnojam.tistory.com/66

 

[Algorithm] ์œ„์ƒ์ •๋ ฌ(Topological Sort)์„ Java๋กœ ๊ตฌํ˜„ํ•ด๋ณด์ž!!

์•ˆ๋…•ํ•˜์„ธ์š” ์ฝ”๋”ฉ๋…ธ์žผ์ž…๋‹ˆ๋‹ค. ์˜ค๋Š˜์€ ๊ทธ๋ž˜ํ”„ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ ์œ„์ƒ ์ •๋ ฌ์„ Java๋กœ ๊ตฌํ˜„ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ์œ„์ƒ ์ •๋ ฌ(Topological Sort)์ด๋ž€? 1. 1 ๊ฐœ๋… ์šฐ๋ฆฌ๊ฐ€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ณต๋ถ€ํ•  ๋•Œ ๊ทธ๋ž˜ํ”„์— ๊ด€๋ จ๋œ ๋ฌธ์ œ๋ฅผ ๋งŽ

codingnojam.tistory.com

์˜ˆ์ œ 1๋ฒˆ์„ ์‚ดํŽด๋ณด๋ฉด ์ปดํ“จํ„ฐ์˜ ์ง„์ž…์ฐจ์ˆ˜์ธ degree[]๋ฐฐ์—ด์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ์ง„์ž…์ฐจ์ˆ˜๊ฐ€ 0์ธ 1์„ ์šฐ์„ ์ˆœ์œ„ํ์— ๋„ฃ์–ด์ฃผ๊ณ  ๋…ธ๋“œ๋“ค์˜ ์˜์กด๊ด€๊ณ„๋ฅผ child ArrayList์— ์ €์žฅํ•˜์—ฌ ์œ„์ƒ์ •๋ ฌ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

์ด๋•Œ, ๋‹ค๋ฅธ ์ผ๋ฐ˜์ ์ธ ์œ„์ƒ์ •๋ ฌ๊ณผ ๋‹ค๋ฅด๊ฒŒ ๊ฒŒ์ž„ ๊ฐœ๋ฐœ ๋ฌธ์ œ์—์„œ๋Š” ๋ชจ๋“  ๊ฑด๋ฌผ์„ ์ง“๋Š”๋ฐ ์ตœ์†Œ์‹œ๊ฐ„์„ ๊ตฌํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฑด๋ฌผ์„ ์ง“๋Š”๋ฐ ๋“œ๋Š” ์‹œ๊ฐ„์„ ์ €์žฅํ•œ ๋ฐฐ์—ด time[]๊ณผ ํ•ด๋‹น ๋…ธ๋“œ๊นŒ์ง€ ์˜ค๊ธฐ์œ„ํ•ด ๋ฐฉ๋ฌธํ–ˆ๋˜ ๋…ธ๋“œ๋“ค์˜ ์ตœ๋Œ€๊ฐ’์„ ์ €์žฅํ•ด๋‘๋Š” maxTime[]์„ ์ถ”๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Main {
    public static int N;
    public static int time[];
    public static ArrayList<ArrayList<Integer>> child = new ArrayList<ArrayList<Integer>>();
    public static int maxTime[];
    public static int degree[];
    public static PriorityQueue<Integer> pq = new PriorityQueue<>();
    public static ArrayList<Integer> start = new ArrayList<>();
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        N = Integer.parseInt(br.readLine());
        time = new int[N + 1];
        maxTime = new int[N + 1];
        degree = new int[N + 1];
        for(int i=0;i<=N;i++) {
            child.add(new ArrayList<Integer>());
        }
        for(int i=1;i<=N;i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            time[i] = Integer.parseInt(st.nextToken());
            int num = Integer.parseInt(st.nextToken());
            int cnt = 0;
            if(num == -1) {
                pq.offer(i);
            } else {
                while(num != -1) {
                    cnt++;
                    child.get(num).add(i);
                    num = Integer.parseInt(st.nextToken());
                }
                degree[i] = cnt;
            }
        }
        findChild();
        for(int i=1;i<=N;i++) {
            System.out.println(time[i]);
        }
    }
    public static void findChild() {
        while(!pq.isEmpty()) {
            int p = pq.poll();
            for(Integer i:child.get(p)) {
                degree[i]--;
                maxTime[i] = Math.max(maxTime[i], time[p]);
                if(degree[i] == 0) {
                    time[i] += Math.max(maxTime[i], time[p]);
                    pq.offer(i);
                }
            }
        }
    }
}