class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param numProject int整型 
     * @param groups int整型vector<vector<>> 
     * @return int整型vector
     */

    vector<int> findOrder(int numProject, vector<vector<int> >& groups) {
       vector<vector<int>> edges(numProject+1); 
        vector<int> ru(numProject); 
        
        for(auto pair: groups)
        {
            edges[pair[1]].push_back(pair[0]);
            ru[pair[0]]++;
        }


        queue<int> q;
        for(int i = 0; i < numProject; i++)
        {
            if(ru[i] == 0) 
            { 
                q.push(i);
            }
        }

        vector<int> ret;
        while(!q.empty())
        {
            auto a = q.front();
            q.pop();

            ret.push_back(a);

            for(auto b: edges[a])
            {
                --ru[b];
                if(ru[b] == 0)  
                {
                    q.push(b);
                }
            }
        }

   
        if(ret.size() == numProject) 
        {
             return ret;
        }
        else
            return {};

    }
};