From 4935d86e9c5db01d9106d3409b6e9fa15530505b Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Wed, 11 Jun 2025 23:16:42 +0900 Subject: [PATCH] =?UTF-8?q?[20250611]=20BOJ=20/=20G3=20/=20=EC=9D=8C?= =?UTF-8?q?=EC=95=85=ED=94=84=EB=A1=9C=EA=B7=B8=EB=9E=A8=20/=20=EC=9D=B4?= =?UTF-8?q?=EA=B0=95=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\353\241\234\352\267\270\353\236\250.md" | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 "lkhyun/202506/12 BOJ G3 \354\235\214\354\225\205\355\224\204\353\241\234\352\267\270\353\236\250.md" diff --git "a/lkhyun/202506/12 BOJ G3 \354\235\214\354\225\205\355\224\204\353\241\234\352\267\270\353\236\250.md" "b/lkhyun/202506/12 BOJ G3 \354\235\214\354\225\205\355\224\204\353\241\234\352\267\270\353\236\250.md" new file mode 100644 index 00000000..ba41de6d --- /dev/null +++ "b/lkhyun/202506/12 BOJ G3 \354\235\214\354\225\205\355\224\204\353\241\234\352\267\270\353\236\250.md" @@ -0,0 +1,73 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + static StringTokenizer st; + static int N,M; + static List[] adjList; + static int[] degree; + static int[] result; + + public static void main(String[] args) throws IOException { + st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + adjList = new List[N+1]; + degree = new int[N+1]; + result = new int[N]; + + for (int i = 1; i <= N; i++) { + adjList[i] = new ArrayList<>(); + } + + for (int i = 0; i < M; i++) { + st = new StringTokenizer(br.readLine()); + int cnt = Integer.parseInt(st.nextToken()); + int[] order = new int[cnt]; + for (int j = 0; j < cnt; j++) { + order[j] = Integer.parseInt(st.nextToken()); + } + for (int j = 0; j < cnt-1; j++) { + for (int k = j+1; k < cnt; k++) { + if(!adjList[order[j]].contains(order[k])) { + adjList[order[j]].add(order[k]); + degree[order[k]]++; + } + } + } + } + + if(go() == N){ + for (int i = 0; i < N; i++) { + bw.write(result[i]+"\n"); + } + }else{ + bw.write("0"); + } + bw.close(); + } + public static int go(){ + int cnt = 0; + ArrayDeque q = new ArrayDeque<>(); + for (int i = 1; i <= N; i++) { + if(degree[i] == 0){ + q.add(i); + } + } + + while(!q.isEmpty()){ + int cur = q.poll(); + result[cnt++] = cur; + for(int next : adjList[cur]){ + if(--degree[next] == 0){ + q.add(next); + } + } + } + return cnt; + } +} +```