原题目:https://leetcode-cn.com/problems/minimum-number-of-vertices-to-reach-all-nodes/
思路:
对示例的结果分析,我们只需找到入度为0的节点即可
代码:
class Solution {
public:
vector<int> findSmallestSetOfVertices(int n, vector<vector<int>>& edges) {
vector<int> g(n,0);
vector<int> ans;
for(int i=0;i<edges.size();i++){
g[edges[i][1]] ++;
}
for(int i=0;i<n;i++){
if(g[i]==0) ans.push_back(i);
}
return ans;
}
};