Notice
Recent Posts
Recent Comments
Today
Total
01-10 12:27
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[๋ฐฑ์ค€ 2252] ์ค„ ์„ธ์šฐ๊ธฐ(JAVA) ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm || ๋ฌธ์ œํ’€์ด/PS

[๋ฐฑ์ค€ 2252] ์ค„ ์„ธ์šฐ๊ธฐ(JAVA)

WonderJay 2023. 1. 17. 12:15
728x90
๋ฐ˜์‘ํ˜•
SMALL

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๋ช…์˜ ํ•™์ƒ๋“ค์„ ํ‚ค ์ˆœ์„œ๋Œ€๋กœ ์ค„์„ ์„ธ์šฐ๋ ค๊ณ  ํ•œ๋‹ค. ๊ฐ ํ•™์ƒ์˜ ํ‚ค๋ฅผ ์ง์ ‘ ์žฌ์„œ ์ •๋ ฌํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๊ฒ ์ง€๋งŒ, ๋งˆ๋•…ํ•œ ๋ฐฉ๋ฒ•์ด ์—†์–ด์„œ ๋‘ ํ•™์ƒ์˜ ํ‚ค๋ฅผ ๋น„๊ตํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ํ•˜์˜€๋‹ค. ๊ทธ๋‚˜๋งˆ๋„ ๋ชจ๋“  ํ•™์ƒ๋“ค์„ ๋‹ค ๋น„๊ตํ•ด ๋ณธ ๊ฒƒ์ด ์•„๋‹ˆ๊ณ , ์ผ๋ถ€ ํ•™์ƒ๋“ค์˜ ํ‚ค๋งŒ์„ ๋น„๊ตํ•ด ๋ณด์•˜๋‹ค.

์ผ๋ถ€ ํ•™์ƒ๋“ค์˜ ํ‚ค๋ฅผ ๋น„๊ตํ•œ ๊ฒฐ๊ณผ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ค„์„ ์„ธ์šฐ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

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

ํ•™์ƒ๋“ค์˜ ๋ฒˆํ˜ธ๋Š” 1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ์ด๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ํ•™์ƒ๋“ค์„ ์•ž์—์„œ๋ถ€ํ„ฐ ์ค„์„ ์„ธ์šด ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๋‹ต์ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€์ธ ๊ฒฝ์šฐ์—๋Š” ์•„๋ฌด๊ฑฐ๋‚˜ ์ถœ๋ ฅํ•œ๋‹ค.

์˜ˆ์ œ ์ž…๋ ฅ 1 ๋ณต์‚ฌ

3 2
1 3
2 3

์˜ˆ์ œ ์ถœ๋ ฅ 1 ๋ณต์‚ฌ

1 2 3

์˜ˆ์ œ 1์˜ ๊ฒฝ์šฐ, 1 > 3, 2 > 3 ์ด๋ผ๋Š” ์ˆœ์„œ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ ์šฐ์„ ์ˆœ์œ„๋ฅผ ์ง€ํ‚ค๋ฉด์„œ ํ•™์ƒ๋“ค์„ ์ค„ ์„ธ์šธ ์ˆ˜ ์žˆ๋Š” ๊ฒฐ๊ณผ๋Š” 1 2 3 ์ด๋‹ค. 

๋ฌธ์ œ์˜ ์กฐ๊ฑด์„ ์ฝ์–ด๋ณด๋ฉด, 1>3, 2>3 ์ด๋ผ๋Š” ์šฐ์„ ์ˆœ์œ„๋Š” 1→3, 2→3 ๊ณผ ๊ฐ™์€ ์—ฐ๊ฒฐ๊ด€๊ณ„๋กœ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๊ณ 

๋ฌธ์ œ ์ƒํ™ฉ์—์„œ๋Š” ๋‹น์—ฐํžˆ ์‚ฌ์ดํด์ด ์กด์žฌํ•˜์ง€ ์•Š์„ ๊ฒƒ์ด๋ฏ€๋กœ ์•ž์—์„œ ๋ถ€ํ„ฐ ์ค„์„ ์„ธ์šด ๊ฒฐ๊ณผ๋Š” ์œ„์ƒ ์ •๋ ฌ ๊ฒฐ๊ณผ์™€ ๊ฐ™๋‹ค.

 

๋‹ต์ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ธ ๊ฒฝ์šฐ์—๋Š” ์•„๋ฌด๊ฑฐ๋‚˜ ์ถœ๋ ฅํ•˜๋ผ๋Š” ์กฐ๊ฑด์€

์œ„์ƒ ์ •๋ ฌ ๊ตฌํ˜„ ๊ณผ์ •์—์„œ Queue ์— ๋„ฃ๋Š” ์ˆœ์„œ๋Š” ์–ด๋–ป๊ฒŒ ํ•ด๋„ ์ƒ๊ด€ ์—†๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.

 

๊ตฌํ˜„ ๊ณผ์ •

1. ArrayList<ArrayList<Integer>> graph ์ž๋ฃŒ๊ตฌ์กฐ์— ๊ฐ ํ•™์ƒ๋“ค์˜ ํ‚ค ๋Œ€์†Œ ๊ด€๊ณ„๋ฅผ ์ €์žฅ

2. indegree ๋ฅผ ๊ณ„์‚ฐ

3. indegree ๊ฐ€ 0 ์ธ ๋…ธ๋“œ๋ฅผ queue ์— offer

4. queue ๊ฐ€ ๋นŒ ๋•Œ ๊นŒ์ง€ ์•„๋ž˜ ๊ณผ์ •์„ ๋ฐ˜๋ณต

4-1. cur = queue.poll()  , cur ์ถœ๋ ฅ

4-2. cur ์— ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค. ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ์˜ indegree ๋ฅผ ํ•˜๋‚˜ ์”ฉ ๊ฐ์†Œ์‹œํ‚จ๋‹ค.

4-3 ๊ทธ๋Ÿฐ๋ฐ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ์˜ indegree ๊ฐ€ 0 ์ด ๋˜๋ฉด, queue ์— offer ํ•œ๋‹ค.

 


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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer stk = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(stk.nextToken()); // ํ•™์ƒ ์ˆ˜
        int m = Integer.parseInt(stk.nextToken()); // ์—์ง€ ์ˆ˜
        ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
        for (int i = 0; i <= n; i++)
            graph.add(new ArrayList<>());
        int[] indegree = new int[n + 1];
        for (int i = 0; i < m; i++) {
            stk = new StringTokenizer(br.readLine());
            int a = Integer.parseInt(stk.nextToken());
            int b = Integer.parseInt(stk.nextToken());
            graph.get(a).add(b);
            indegree[b]++;
        }

        Queue<Integer> q = new LinkedList<>();
        for (int i = 1; i <= n; i++) {
            if (indegree[i] == 0)
                q.offer(i);
        }
        while (!q.isEmpty()) {
            int now = q.poll();
            System.out.print(now + " ");
            for (int cur : graph.get(now)) {
                indegree[cur]--;
                if (indegree[cur] == 0)
                    q.offer(cur);
            }
        }
    }

}

728x90
๋ฐ˜์‘ํ˜•
LIST
Comments