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; + } +} +```