1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
// 997. Find the Town Judge
#include "leetcode.h"
/*
* in a town, there are 'n' people labeled from 1 to 'n'. there is a rumor that
* one of these people is secretly the town judge. if the town judge exists,
* then: the town judge trusts nobody. everybody (except the judge) trusts the
* judge. given: array 'trust' where 'trust[i]' = [ai, bi] representing the the
* person labeled ai trusts the person labeled bi. return label of judge, if
* they exist.
*/
class Solution {
public:
int findJudge(int n, vvd(int) trust) {
vector<unordered_set<int>> graph(n + 1, unordered_set<int>());
for (auto &edge : trust) {
int a = edge[0], b = edge[1];
graph[a].insert(b); // a trusts b
}
int townJudge = -1;
for (int i = 1; i <= n; ++i)
if (graph[i].size() == 0) {
townJudge = i;
break;
}
if (townJudge == -1)
return -1;
for (int i = 1; i <= n; ++i) {
if (i == townJudge)
continue;
if (!graph[i].count(townJudge))
return -1;
}
return townJudge;
}
};
int main() {
Solution obj;
vvd(int) trust1 = {{1, 2}}, trust2 = {{1, 3}, {2, 3}},
trust3 = {{1, 3}, {2, 3}, {3, 1}};
cout << obj.findJudge(2, trust1) << endl; // expect: 2
cout << obj.findJudge(3, trust2) << endl; // expect: 3
cout << obj.findJudge(3, trust3) << endl; // expect: -1
}
|