• toc {:toc}

๐Ÿ“ฆ1766: ๋ฌธ์ œ์ง‘


[์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด] ๊ฐ•๋™๊ทœ Reviewed by Kade Kang (devkade12@gmail.com) Reviewed:: 2024-02-18 ๋ฌธ์ œ ํ™•์ธํ•˜๊ธฐ

ํ’€์ด

  • 1, 2, 3 ๋ฒˆ์˜ ์กฐ๊ฑด์„ ๋ชจ๋‘ ๋งŒ์กฑํ•˜๋„๋ก ๋งŒ๋“ ๋‹ค.
  • ๊ฐ€๋Šฅํ•œ ์‰ฌ์šด ๋ฌธ์ œ๋ถ€ํ„ฐ ํ’€์–ด์•ผ ํ•˜๊ณ  ์ž‘์€ ์ˆ˜๊ฐ€ ๋” ์‰ฌ์šด ๋ฌธ์ œ์ด๊ธฐ ๋•Œ๋ฌธ์— ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž‘์€ ์ˆœ์„œ๋ถ€ํ„ฐ ์ˆœ์ฐจ์ ์œผ๋กœ ์ถœ๋ ฅ๋˜๋„๋ก ํ•œ๋‹ค.
  • ๋จผ์ € ํ‘ธ๋Š” ๊ฒƒ์ด ์ข‹์€ ๋ฌธ์ œ๋ฅผ ๋ฐ˜๋“œ์‹œ ๋จผ์ € ํ’€๊ธฐ ์œ„ํ•ด ๋‚˜์ค‘์— ํ’€์–ด์•ผ ํ•  ๋ฌธ์ œ์— +1 ์„ ๋”ํ•ด์ฃผ๊ณ  ๋จผ์ € ํ’€์–ด์•ผ ํ•  ๋ฌธ์ œ๊ฐ€ ํ’€๋ฆฐ ๊ฒฝ์šฐ -1 ์„ ํ•˜์—ฌ ์šฐ์„ ์ˆœ์œ„ ํ์— ๋„ฃ์–ด์คŒ์œผ๋กœ์จ ์ˆœ์„œ๋ฅผ ์ง€ํ‚จ๋‹ค.
#include <iostream>
#include <vector>
#include <queue>
#define endl '\n'
using namespace std;
 
int N, M, A, B;
int degree[32001];
vector<int> connect[32001];
 
void Input(){
    cin >> N >> M;
    for(int i=0; i<M; i++){
        cin >> A >> B;
        connect[A].push_back(B);
        degree[B]++;
    }
}
 
void Solution(){
    priority_queue<int, vector<int>, greater<int>> pq;
    
    for(int i=1; i<=N; i++){
        if(degree[i]==0){
            pq.push(i);
        }
    }
 
    for(int i=1; i<=N; i++){
        int cur = pq.top(); pq.pop();
 
        cout << cur << ' ';
 
        for(auto nxt : connect[cur]){
            degree[nxt]--;
            if(degree[nxt]==0){
                pq.push(nxt);
            }
        }
    }
}
 
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
 
    Input();
    Solution();
 
    return 0;
}