ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 백준 1766 C++ 풀이
    카테고리 없음 2018. 11. 12. 00:03
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <math.h>
    #include <climits>
    #include <map>
    using namespace std;
    int degree[32001];
    vector<int> buffer[32001];

    int main(){
    cin.tie(NULL);
    ios::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    for (auto i =0; i<m; i++){
    int from, to;
    cin >> from >> to;
    buffer[from-1].push_back(to-1);
    degree[to-1]++;
    }
    priority_queue<int, vector<int>, greater<int>> q;

    for (auto i =0; i<n; i++){
    if (degree[i] == 0){
    // cout << i << 'a' << '\n';
    q.push(i);
    }
    }

    while (!q.empty()){
    int top = q.top();
    cout << top+1 << ' ';
    q.pop();
    for (int to : buffer[top]){
    degree[to]--;
    if (degree[to] == 0){
    q.push(to);
    }
    }
    }

    return 0;
    }


Designed by Tistory.