알고리즘

[알고리즘] 백준 2623 - 음악 프로그램 (C++)

blueberrysoda 2024. 10. 10. 23:58

[문제 링크] : https://www.acmicpc.net/problem/2623

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int N, M;
int Arr[1001];
vector<int> V[1001];

void solve(){
    queue<int> Q;
    for(int i=1; i<=N; i++){
        if(Arr[i] == 0){
            Q.push(i);
        }
    }

    vector<int> Ans;
    while(Q.empty() == false){
        int num = Q.front();
        Q.pop();
        Ans.push_back(num);
        for(int i=0; i<V[num].size(); i++){
            int tmp = V[num][i];
            Arr[tmp]--;
            if(Arr[tmp] == 0){
                Q.push(tmp);
            }
        }    
    }

    if(Ans.size() != N){
        cout << 0 << "\n";
    }
    else{
        for(int i=0; i<Ans.size(); i++){
            cout << Ans[i] << "\n";
        }
    }
    return;
}

int main(){
    cin >> N >> M;

    int cnt, inp;
    for(int i=0; i<M; i++){
        vector<int> tmp;
        cin >> cnt;
        for(int j=0; j<cnt; j++){
            cin >> inp;
            tmp.push_back(inp);
        }
        for(int j=0; j<tmp.size(); j++){
            for(int k=j+1; k<tmp.size(); k++){
                V[tmp[j]].push_back(tmp[k]);
                Arr[tmp[k]]++;
            }
        }
    }

    solve();
    
    return 0;
}