From 0390e11347056fbc1d5e16bc1ddcd288febf4726 Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Fri, 13 Jun 2025 22:44:41 +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=EC=9D=B4=EA=B0=95?= =?UTF-8?q?=ED=98=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" | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 "lkhyun/202506/13 BOJ G3 \355\205\200 \355\224\204\353\241\234\354\240\235\355\212\270.md" diff --git "a/lkhyun/202506/13 BOJ G3 \355\205\200 \355\224\204\353\241\234\354\240\235\355\212\270.md" "b/lkhyun/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..c98beb8b --- /dev/null +++ "b/lkhyun/202506/13 BOJ G3 \355\205\200 \355\224\204\353\241\234\354\240\235\355\212\270.md" @@ -0,0 +1,49 @@ +```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[] want; + static boolean[] visited; + static int noTeamCnt; + + public static void main(String[] args) throws IOException { + int T = Integer.parseInt(br.readLine()); + for (int t = 0; t < T; t++) { + int n = Integer.parseInt(br.readLine()); + want = new int[n+1]; + st = new StringTokenizer(br.readLine()); + + for (int i = 1; i <= n; i++) { + want[i] = Integer.parseInt(st.nextToken()); + } + + visited = new boolean[n+1]; + noTeamCnt = 0; + for (int i = 1; i <= n; i++) { // 매 인원마다 체크 + if(visited[i]) continue; // 한번이라도 거쳐갔다면 넘어감 + int cur = want[i]; + List temp = new ArrayList<>(); + temp.add(i); //현재 인원을 리스트에 넣고 + visited[i] = true; //다녀감 체크 + while(!visited[cur]) { //순회하지 않은 경우만 돌음 + temp.add(cur); + visited[cur] = true; + cur = want[cur]; + } + int curPos = temp.indexOf(cur); //현재 접근하는 위치 인덱스 추출 + if(curPos == -1){ // 만약 접근하는 위치가 리스트에 존재하지 않는다면 이미 팀을 형성한 것임 + noTeamCnt += temp.size(); + }else{ // 접근하는 위치가 리스트에 존재한다면 그 위치의 앞부분은 모두 팀을 만들지 못한 것 + noTeamCnt += curPos; + } + } + bw.write(noTeamCnt + "\n"); + } + bw.close(); + } +} +```