PS/Problems
[Java] 백준 9466번 텀 프로젝트 - DP, case work
CalicoCat22
2022. 2. 11. 18:24
https://www.acmicpc.net/problem/9466
9466번: 텀 프로젝트
이번 가을학기에 '문제 해결' 강의를 신청한 학생들은 텀 프로젝트를 수행해야 한다. 프로젝트 팀원 수에는 제한이 없다. 심지어 모든 학생들이 동일한 팀의 팀원인 경우와 같이 한 팀만 있을
www.acmicpc.net
처음에는 사이클에 구성되지 않는 노드의 개수를 찾는 문제라고 생각했으나
시간복잡도가 n^2가 되어 불가능했기에,
어느 노드에서 출발하더라도 마지막에는 항상 사이클로 연결된다는 점을 이용했다.
import java.util.*;
import java.io.*;
public class g3_9466 {
static FastScanner fs = new FastScanner();
static PrintWriter pw = new PrintWriter(System.out);
static int T, n, cnt, flag;
static int[] visit, next;
static Queue<Integer> route;
public static void main(String[] args) {
T = fs.nextInt();
for (int t=0;t<T;t++){
n = fs.nextInt();
visit = new int[n + 1];
next = new int[n + 1];
cnt = 0;
for (int i=1;i<=n;i++) next[i] = fs.nextInt();
for (int i=1;i<=n;i++){
route = new LinkedList<>();
flag = i;
find(i);
}
pw.println(cnt);
}
pw.close();
}
static void find(int now){
route.add(now);
// this node is never visited
if (visit[now] == 0){
visit[now] = flag;
find(next[now]);
}
// this node was once visited during this searching
else if (visit[now] == flag){
while (!route.isEmpty()){
if (route.poll() == now) return;
cnt++;
}
}
// this node was visited during past serching
else cnt += route.size() - 1;
}
// ----------input function----------
static void sort(int[] a) {
ArrayList<Integer> L = new ArrayList<>();
for (int i : a)
L.add(i);
Collections.sort(L);
for (int i = 0; i < a.length; i++)
a[i] = L.get(i);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}