From 15dbc8385c71be91577b975de40850881b7d1c3c Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Fri, 13 Jun 2025 23:36:53 +0900 Subject: [PATCH] =?UTF-8?q?[20250613]=20BOJ=20/=20G3=20/=20=ED=85=80=20?= =?UTF-8?q?=ED=94=84=EB=A1=9C=EC=A0=9D=ED=8A=B8=20/=20=EA=B9=80=EC=88=98?= =?UTF-8?q?=EC=97=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\353\241\234\354\240\235\355\212\270.md" | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 "suyeun84/202506/13 BOJ G3 \355\205\200 \355\224\204\353\241\234\354\240\235\355\212\270.md" diff --git "a/suyeun84/202506/13 BOJ G3 \355\205\200 \355\224\204\353\241\234\354\240\235\355\212\270.md" "b/suyeun84/202506/13 BOJ G3 \355\205\200 \355\224\204\353\241\234\354\240\235\355\212\270.md" new file mode 100644 index 00000000..0401db48 --- /dev/null +++ "b/suyeun84/202506/13 BOJ G3 \355\205\200 \355\224\204\353\241\234\354\240\235\355\212\270.md" @@ -0,0 +1,48 @@ +```java +import java.io.*; +import java.util.*; + +public class boj9466 { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static StringTokenizer st; + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} + static int nextInt() {return Integer.parseInt(st.nextToken());} + + static int N, cycle; + static int[] point; + static boolean[] check, visited; + public static void main(String[] args) throws Exception { + nextLine(); + int T = nextInt(); + + for (int tc = 0; tc < T; tc++) { + nextLine(); + N = nextInt(); + nextLine(); + point = new int[N+1]; + for (int i = 1; i <= N; i++) point[i] = nextInt(); + cycle = 0; + check = new boolean[N+1]; + visited = new boolean[N+1]; + + for (int i = 1; i <= N; i++) { + if (check[i]) continue; + dfs(i); + } + System.out.println(N - cycle); + } + + } + static void dfs(int idx) { + if (check[idx]) return; + if (visited[idx]) { + check[idx] = true; + cycle++; + } + visited[idx] = true; + dfs(point[idx]); + check[idx] = true; + visited[idx] = false; + } +} +```