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

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

 

2252๋ฒˆ: ์ค„ ์„ธ์šฐ๊ธฐ

์ฒซ์งธ ์ค„์— N(1 ≤ N ≤ 32,000), M(1 ≤ M ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. M์€ ํ‚ค๋ฅผ ๋น„๊ตํ•œ ํšŒ์ˆ˜์ด๋‹ค. ๋‹ค์Œ M๊ฐœ์˜ ์ค„์—๋Š” ํ‚ค๋ฅผ ๋น„๊ตํ•œ ๋‘ ํ•™์ƒ์˜ ๋ฒˆํ˜ธ A, B๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด๋Š” ํ•™์ƒ A๊ฐ€ ํ•™์ƒ B์˜ ์•ž์— ์„œ์•ผ ํ•œ๋‹ค๋Š” ์˜

www.acmicpc.net

 

ํ’€์ด

  • "์—ฐ๊ฒฐ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„ N๋ช…์˜ ํ•™์ƒ ์ค„ ์„ธ์šฐ๊ธฐ"๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•ด์„ํ•  ์ˆ˜ ์žˆ๋‹ค.
    => ์—ฐ๊ฒฐ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„ ์ˆœํ™˜ํ•˜์ง€ ์•Š๋Š” ๋ฐฉํ–ฅ์„ฑ ๊ทธ๋ž˜ํ”„(DAG=๋ฐฉํ–ฅ์„ฑ ๋น„์ˆœํ™˜ ๊ทธ๋ž˜ํ”„)์—์„œ ๊ฐ ๋…ธ๋“œ๋ฅผ ์ค„ ์„ธ์šฐ์‹œ์˜ค.
  • ์ด๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์œ„์ƒ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ์ผ์น˜ํ•˜๋ฏ€๋กœ ์œ„์ƒ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•ด ํ’€์ดํ•œ๋‹ค.
  • ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ ๋…ธ๋“œ์˜ ์—ฐ๊ฒฐ ๋‹นํ•œ ์ •๋ณด๋ฅผ linked ๋ฐฐ์—ด์— ๋ฏธ๋ฆฌ ์ž…๋ ฅ ํ›„ linked ๋ฐฐ์—ด์ด 0์ธ ๊ฒฝ์šฐ ํ•ด๋‹น ๋…ธ๋“œ๋ฅผ queue์— ์ถ”๊ฐ€ํ•˜์—ฌ BFS๋ฅผ ์ด์šฉํ•ด ์œ„์ƒ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ตฌํ˜„ํ–ˆ๋‹ค.
  1. ๊ฐ ๋…ธ๋“œ์˜ ์—ฐ๊ฒฐ ๋‹นํ•œ ์ •๋ณด๋ฅผ linked ๋ฐฐ์—ด์— ์ €์žฅํ•œ๋‹ค.
  2. linked ๋ฐฐ์—ด์—์„œ ๊ฐ’์ด 0์ธ ๋…ธ๋“œ๋ฅผ queue์— ์ถ”๊ฐ€ํ•œ๋‹ค.
  3. ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ๋Š” BFS๋ฅผ ์ด์šฉํ•ด ํƒ์ƒ‰ํ•˜๋Š”๋ฐ, queue์— ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ์กฐ๊ฑด์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
    1. queue์—์„œ ๋…ธ๋“œ ํ•˜๋‚˜๋ฅผ ๊บผ๋‚ด๊ณ  ์ •๋‹ต ์ถœ๋ ฅ์— ์ด์šฉํ•  StringBuffer์— ์ถ”๊ฐ€ํ•œ๋‹ค. 
    2. queue์—์„œ ๊บผ๋‚ธ ํ˜„์žฌ ๋…ธ๋“œ(current)์™€ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ(node)๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ, linked[node]๋ฅผ 1์”ฉ ๊ฐ์†Œ ์‹œํ‚จ๋‹ค.
    3. linked[node]๊ฐ€ 0์ด ๋˜์—ˆ๋‹ค๋ฉด, queue์— ์ถ”๊ฐ€ํ•˜๊ณ  ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌํ•œ๋‹ค.
    4. ์—ฌ๊ธฐ์„œ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ๋Š” linked[node] == 0 ์ธ ๊ฒฝ์šฐ๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค.
  4. StringBuffer์— ๋‹ด์•„๋‘” ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

์ฝ”๋“œ

import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class ์ค„_์„ธ์šฐ๊ธฐ_2252 {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] input = br.readLine().split(" ");
        int N = Integer.parseInt(input[0]);
        int M = Integer.parseInt(input[1]);
        StringBuffer sb = new StringBuffer();

        List<List<Integer>> graph = new ArrayList<>();
        int[] linked = new int[N + 1];

        for (int i = 0; i <= N; i++) {
            graph.add(new ArrayList<>());
        }

        for (int i = 0; i < M; i++) {
            String[] input2 = br.readLine().split(" ");
            int start = Integer.parseInt(input2[0]);
            int target = Integer.parseInt(input2[1]);
            linked[target] += 1;
            graph.get(start).add(target);
        }


        Queue<Integer> queue = new LinkedList<>();

        for (int i = 1; i < linked.length; i++) {
            if (linked[i] == 0) {
                queue.add(i);
            }
        }

        while (!queue.isEmpty()) {
            Integer current = queue.poll();
            sb.append(current + " ");

            List<Integer> nodeList = graph.get(current);
            for (Integer node : nodeList) {
                if (--linked[node] == 0) {
                    queue.add(node);
                }
            }

        }

        bw.write(sb.toString());

        bw.close();
        br.close();
    }
}
๋ฐ˜์‘ํ˜•