Notice
Recent Posts
Recent Comments
Link
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Tags more
Archives
Today
Total
관리 메뉴

난 정말 최고야 멋있어

BOJ 9466 본문

카테고리 없음

BOJ 9466

n00bh4cker 2020. 8. 6. 17:30
#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;
    }
}

ㅠㅠㅠㅠㅠㅠㅠㅠㅠ

위상정렬 덕분에 쎄게 배웠다