997 找到小镇法官
程序员文章站
2022-05-21 15:06:52
...
目标点入度 == N - 1,出度 = 0
class Solution {
public:
int findJudge(int N, vector<vector<int>>& trust) {
int indegree[N + 1], outdegree[N + 1];
memset(indegree, 0, sizeof(indegree));
memset(outdegree, 0, sizeof(outdegree));
for(auto i : trust)
++outdegree[i[0]], ++indegree[i[1]];
for(int i = 1; i <= N; ++i)
if(indegree[i] == N - 1 && !outdegree[i])
return i;
return -1;
}
};
上一篇: 蓝桥杯 BFS 迷宫寻宝