난 정말 최고야 멋있어
BOJ 9466 본문
#include <bits/stdc++.h>
using namespace std;
int T;
int students[100000];
int indegree[100000];
int solve(int n)
{
queue<int> Q;
int count = 0;
for (int i = 0 ; i < n ; i++)
if (indegree[i] == 0)
Q.push(i);
int cur;
while (!Q.empty()){
cur = Q.front();
Q.pop();
count++;
cur = students[cur];
indegree[cur]--;
if (indegree[cur] == 0)
Q.push(cur);
}
return count;
}
int main(){
cin >> T;
for (int i=0; i<T; i++){
int n;
cin >> n;
memset(students, 0, sizeof(students[0]) * n);
memset(indegree, 0, sizeof(indegree[0]) * n);
for (int j= 0; j < n ;j++){
cin >> students[j];
--students[j];
++indegree[students[j]];
}
cout << solve(n) << endl;
}
}
ㅠㅠㅠㅠㅠㅠㅠㅠㅠ
위상정렬 덕분에 쎄게 배웠다