Skip to content

Commit a7b261d

Browse files
authored
Merge pull request #349 from AlgorithmWithGod/lkhyun
[20250613] BOJ / G3 / 텀 프로젝트 / 이강현
2 parents cee200c + 0390e11 commit a7b261d

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
static StringTokenizer st;
9+
static int[] want;
10+
static boolean[] visited;
11+
static int noTeamCnt;
12+
13+
public static void main(String[] args) throws IOException {
14+
int T = Integer.parseInt(br.readLine());
15+
for (int t = 0; t < T; t++) {
16+
int n = Integer.parseInt(br.readLine());
17+
want = new int[n+1];
18+
st = new StringTokenizer(br.readLine());
19+
20+
for (int i = 1; i <= n; i++) {
21+
want[i] = Integer.parseInt(st.nextToken());
22+
}
23+
24+
visited = new boolean[n+1];
25+
noTeamCnt = 0;
26+
for (int i = 1; i <= n; i++) { // 매 인원마다 체크
27+
if(visited[i]) continue; // 한번이라도 거쳐갔다면 넘어감
28+
int cur = want[i];
29+
List<Integer> temp = new ArrayList<>();
30+
temp.add(i); //현재 인원을 리스트에 넣고
31+
visited[i] = true; //다녀감 체크
32+
while(!visited[cur]) { //순회하지 않은 경우만 돌음
33+
temp.add(cur);
34+
visited[cur] = true;
35+
cur = want[cur];
36+
}
37+
int curPos = temp.indexOf(cur); //현재 접근하는 위치 인덱스 추출
38+
if(curPos == -1){ // 만약 접근하는 위치가 리스트에 존재하지 않는다면 이미 팀을 형성한 것임
39+
noTeamCnt += temp.size();
40+
}else{ // 접근하는 위치가 리스트에 존재한다면 그 위치의 앞부분은 모두 팀을 만들지 못한 것
41+
noTeamCnt += curPos;
42+
}
43+
}
44+
bw.write(noTeamCnt + "\n");
45+
}
46+
bw.close();
47+
}
48+
}
49+
```

0 commit comments

Comments
 (0)