From 61e9361cf1bedec5a78b9f2937c804b26195c597 Mon Sep 17 00:00:00 2001 From: zinnnn37 Date: Wed, 8 Oct 2025 23:29:39 +0900 Subject: [PATCH] =?UTF-8?q?[20251008]=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=EB=AF=BC?= =?UTF-8?q?=EC=A7=84?= 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" | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 "zinnnn37/202510/8 BOJ G3 \355\205\200 \355\224\204\353\241\234\354\240\235\355\212\270.md" diff --git "a/zinnnn37/202510/8 BOJ G3 \355\205\200 \355\224\204\353\241\234\354\240\235\355\212\270.md" "b/zinnnn37/202510/8 BOJ G3 \355\205\200 \355\224\204\353\241\234\354\240\235\355\212\270.md" new file mode 100644 index 00000000..8801da48 --- /dev/null +++ "b/zinnnn37/202510/8 BOJ G3 \355\205\200 \355\224\204\353\241\234\354\240\235\355\212\270.md" @@ -0,0 +1,68 @@ +```java +import java.io.*; +import java.util.StringTokenizer; + +public class BJ_9466_텀_프로젝트 { + + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + private static final StringBuilder sb = new StringBuilder(); + private static StringTokenizer st; + + private static int N, cnt; + private static int[] group; + private static boolean[] visited, finished; + + public static void main(String[] args) throws IOException { + int T = Integer.parseInt(br.readLine()); + + while (T-- > 0) { + init(); + sol(); + } + bw.write(sb.toString()); + bw.flush(); + bw.close(); + br.close(); + } + + private static void init() throws IOException { + N = Integer.parseInt(br.readLine()); + + cnt = 0; + group = new int[N + 1]; + visited = new boolean[N + 1]; + finished = new boolean[N + 1]; + + st = new StringTokenizer(br.readLine()); + for (int i = 1; i <= N; i++) { + group[i] = Integer.parseInt(st.nextToken()); + } + } + + private static void sol() throws IOException { + for (int i = 1; i <= N; i++) { + dfs(i); + } + sb.append(N - cnt).append("\n"); + } + + private static void dfs(int node) { + visited[node] = true; + + int next = group[node]; + + if (!visited[next]) { + dfs(next); + } else if (!finished[next]) { + while (next != node) { + cnt++; + next = group[next]; + } + cnt++; + } + finished[node] = true; + } + +} +``` \ No newline at end of file