From 7f02cc3a067c8f2a0299cbd5122615964499c633 Mon Sep 17 00:00:00 2001 From: oncsr Date: Fri, 4 Jul 2025 10:07:21 +0900 Subject: [PATCH] =?UTF-8?q?[20250706]=20BOJ=20/=20P4=20/=20=EA=B0=80?= =?UTF-8?q?=ED=86=A8=EB=A6=AD=EB=8C=80=ED=95=99=EA=B5=90=EC=97=90=20?= =?UTF-8?q?=EC=9B=8C=ED=84=B0=20=EC=8A=AC=EB=9D=BC=EC=9D=B4=EB=93=9C?= =?UTF-8?q?=EB=A5=BC=3F=3F=20/=20=EA=B6=8C=ED=98=81=EC=A4=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\354\235\264\353\223\234\353\245\274??.md" | 140 ++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 "khj20006/202507/06 BOJ P4 \352\260\200\355\206\250\353\246\255\353\214\200\355\225\231\352\265\220\354\227\220 \354\233\214\355\204\260 \354\212\254\353\235\274\354\235\264\353\223\234\353\245\274??.md" diff --git "a/khj20006/202507/06 BOJ P4 \352\260\200\355\206\250\353\246\255\353\214\200\355\225\231\352\265\220\354\227\220 \354\233\214\355\204\260 \354\212\254\353\235\274\354\235\264\353\223\234\353\245\274??.md" "b/khj20006/202507/06 BOJ P4 \352\260\200\355\206\250\353\246\255\353\214\200\355\225\231\352\265\220\354\227\220 \354\233\214\355\204\260 \354\212\254\353\235\274\354\235\264\353\223\234\353\245\274??.md" new file mode 100644 index 00000000..d4698f1a --- /dev/null +++ "b/khj20006/202507/06 BOJ P4 \352\260\200\355\206\250\353\246\255\353\214\200\355\225\231\352\265\220\354\227\220 \354\233\214\355\204\260 \354\212\254\353\235\274\354\235\264\353\223\234\353\245\274??.md" @@ -0,0 +1,140 @@ +```java +import java.util.*; +import java.io.*; + +class IOController { + BufferedReader br; + BufferedWriter bw; + StringTokenizer st; + + public IOController() { + br = new BufferedReader(new InputStreamReader(System.in)); + bw = new BufferedWriter(new OutputStreamWriter(System.out)); + st = new StringTokenizer(""); + } + + String nextLine() throws Exception { + String line = br.readLine(); + st = new StringTokenizer(line); + return line; + } + + String nextToken() throws Exception { + while (!st.hasMoreTokens()) nextLine(); + return st.nextToken(); + } + + int nextInt() throws Exception { + return Integer.parseInt(nextToken()); + } + + long nextLong() throws Exception { + return Long.parseLong(nextToken()); + } + + double nextDouble() throws Exception { + return Double.parseDouble(nextToken()); + } + + void close() throws Exception { + bw.flush(); + bw.close(); + } + + void write(String content) throws Exception { + bw.write(content); + } + +} + +public class Main { + + static IOController io; + + // + + static int N, M; + static List[] G, R; + static Stack S; + static int[] C, D; + static boolean[] vis; + + public static void main(String[] args) throws Exception { + + io = new IOController(); + + init(); + solve(); + + io.close(); + + } + + public static void init() throws Exception { + + N = io.nextInt(); + M = io.nextInt(); + G = new List[N+1]; + R = new List[N+1]; + for(int i=1;i<=N;i++) { + G[i] = new ArrayList<>(); + R[i] = new ArrayList<>(); + } + + while(M-- > 0) { + int a = io.nextInt(), b = io.nextInt(); + G[a].add(b); + R[b].add(a); + } + + S = new Stack<>(); + C = new int[N+1]; + + } + + static void solve() throws Exception { + + vis = new boolean[N+1]; + for(int i=1;i<=N;i++) if(!vis[i]) { + vis[i] = true; + dfs1(i); + } + + vis = new boolean[N+1]; + int cnt = 0; + while(!S.isEmpty()) { + int n = S.pop(); + if(vis[n]) continue; + vis[n] = true; + dfs2(n,++cnt); + } + + D = new int[cnt+1]; + for(int i=1;i<=N;i++) for(int j:G[i]) { + if(C[i] != C[j]) D[C[j]]++; + } + + int ans = 0; + for(int i=1;i<=cnt;i++) if(D[i] == 0) ans++; + io.write(ans + "\n"); + + } + + static void dfs1(int n) { + for(int i:G[n]) if(!vis[i]) { + vis[i] = true; + dfs1(i); + } + S.add(n); + } + + static void dfs2(int n, int c) { + C[n] = c; + for(int i:R[n]) if(!vis[i]) { + vis[i] = true; + dfs2(i,c); + } + } + +} +```