0997.cpp (449B)
1 class Solution { 2 public: 3 int findJudge(int n, vector<vector<int>> &trust) { 4 if (n == 1 && trust.empty()) return 1; 5 6 vector<int> trusted(n + 1, 0); 7 unordered_set<int> trusting; 8 9 for (auto &p : trust) { 10 trusting.insert(p[0]); 11 trusted[p[1]]++; 12 } 13 14 for (int i = 1; i <= n; i++) 15 if (trusted[i] == n - 1 && !trusting.count(i)) return i; 16 17 return -1; 18 } 19 };