Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
749 B
1
Indexable
Never
class Solution {
public:
    vector<int> findOrder(int n, vector<vector<int>>& pre) {
        vector<int> res;
        vector<vector<int>> adj(n);
        vector<int>deg(n,0);
        for(int i=0;i<pre.size();i++){
            adj[pre[i][1]].push_back(pre[i][0]);
            deg[pre[i][0]]++;
        }
        queue<int>s;
        for(int i=0;i<n;i++){
            if(deg[i]==0) s.push(i);
        }
        while(!s.empty()){
            int f=s.front();
            res.push_back(f);
            s.pop();
            n--;
            for(auto nbr:adj[f]){
                deg[nbr]--;
                if(deg[nbr]==0) s.push(nbr);
            }
        }
        vector<int> empty;
        if(n==0) return res;
        else return empty;

    }
};