1차 - 메모리: 311304 KB, 시간: 1500 ms
2차 - 메모리: 319264 KB, 시간: 1804 ms
깊이 우선 탐색, 그래프 이론, 그래프 탐색
2025년 1월 17일 16:42:34
이번 가을학기에 '문제 해결' 강의를 신청한 학생들은 텀 프로젝트를 수행해야 한다. 프로젝트 팀원 수에는 제한이 없다. 심지어 모든 학생들이 동일한 팀의 팀원인 경우와 같이 한 팀만 있을 수도 있다. 프로젝트 팀을 구성하기 위해, 모든 학생들은 프로젝트를 함께하고 싶은 학생을 선택해야 한다. (단, 단 한 명만 선택할 수 있다.) 혼자 하고 싶어하는 학생은 자기 자신을 선택하는 것도 가능하다.
학생들이(s1, s2, ..., sr)이라 할 때, r=1이고 s1이 s1을 선택하는 경우나, s1이 s2를 선택하고, s2가 s3를 선택하고,..., sr-1이 sr을 선택하고, sr이 s1을 선택하는 경우에만 한 팀이 될 수 있다.
예를 들어, 한 반에 7명의 학생이 있다고 하자. 학생들을 1번부터 7번으로 표현할 때, 선택의 결과는 다음과 같다.
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|
| 3 | 1 | 3 | 7 | 3 | 4 | 6 |
위의 결과를 통해 (3)과 (4, 7, 6)이 팀을 이룰 수 있다. 1, 2, 5는 어느 팀에도 속하지 않는다.
주어진 선택의 결과를 보고 어느 프로젝트 팀에도 속하지 않는 학생들의 수를 계산하는 프로그램을 작성하라.
첫째 줄에 테스트 케이스의 개수 T가 주어진다. 각 테스트 케이스의 첫 줄에는 학생의 수가 정수 n (2 ≤ n ≤ 100,000)으로 주어진다. 각 테스트 케이스의 둘째 줄에는 선택된 학생들의 번호가 주어진다. (모든 학생들은 1부터 n까지 번호가 부여된다.)
각 테스트 케이스마다 한 줄에 출력하고, 각 줄에는 프로젝트 팀에 속하지 못한 학생들의 수를 나타내면 된다.
/**
* Author: yngbao97, Yuk Yejin
* Problem: 텀 프로젝트_9466
* Date: 2025.01.17
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main {
static BufferedReader br;
static BufferedWriter bw;
static StringTokenizer st;
public static void main(String[] args) throws Exception {
br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.parseInt(br.readLine());
for (int tc = 0; tc < T; tc++) {
int n = Integer.parseInt(br.readLine());
int[] next = new int[n+1];
st = new StringTokenizer(br.readLine(), " ");
for (int i = 1; i <= n; i++) next[i] = Integer.parseInt(st.nextToken());
boolean[] visited = new boolean[n+1];
Queue<Integer> queue = new ArrayDeque<>();
int notTeam = n;
for (int i = 1; i <= n; i++) {
if (visited[i]) continue;
int curr = i;
while (true) {
visited[curr] = true;
queue.add(curr);
if (next[curr] == curr) {
notTeam--;
queue.clear();
break;
} else if (next[curr] == i){
notTeam -= queue.size();
queue.clear();
break;
} else if (visited[next[curr]]) {
while(!queue.isEmpty() && queue.peek() != next[curr]) queue.poll();
notTeam -= queue.size();
queue.clear();
break;
}
curr = next[curr];
}
}
bw.write(String.valueOf(notTeam)+ "\n");
}
bw.flush();
bw.close();
br.close();
}
}
/**
* Author: yngbao97, Yuk Yejin
* Problem: 텀 프로젝트_9466
* Date: 2025.01.17
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main {
static BufferedReader br;
static BufferedWriter bw;
static StringTokenizer st;
static int[] next;
static boolean[] visited;
static int notTeam;
static Set<Integer> passed;
public static void main(String[] args) throws Exception {
br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.parseInt(br.readLine());
for (int tc = 0; tc < T; tc++) {
int n = Integer.parseInt(br.readLine());
next = new int[n+1];
st = new StringTokenizer(br.readLine(), " ");
for (int i = 1; i <= n; i++) next[i] = Integer.parseInt(st.nextToken());
visited = new boolean[n+1];
notTeam = n;
for (int i = 1; i <= n; i++) {
if (visited[i]) continue;
visited[i] = true;
passed = new HashSet<>();
passed.add(i);
if(checkNext(next[i]) == i) notTeam--;
passed.clear();
}
bw.write(String.valueOf(notTeam)+ "\n");
}
bw.flush();
bw.close();
br.close();
}
private static int checkNext(int curr) {
if (visited[curr]) return curr;
passed.add(curr);
visited[curr] = true;
int last = checkNext(next[curr]);
if (passed.contains(last)) {
notTeam--;
if (last == curr) passed.remove(last);
}
return last;
}
}