add: 220103

This commit is contained in:
Lam Haoyin 2022-01-03 13:37:59 +08:00
parent d0bd5ce0e7
commit fe12451a4b
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
2 changed files with 34 additions and 1 deletions

33
2201/220103.cpp Normal file
View File

@ -0,0 +1,33 @@
#include <vector>
#include <cstdio>
#include <cstring>
class Solution {
private:
// Reduce cache miss!
// inline static int inDegree[1005];
// inline static int outDegree[1005];
inline static unsigned degrees[1005][2];
public:
static int findJudge(int n, const std::vector<std::vector<int>>& trust) {
int len = trust.size();
if (len < n - 1) return -1;
memset(degrees, 0, sizeof degrees);
// for (const auto& i : trust) {
for (int i = 0; i < len; ++i) {
++degrees[trust[i][0]][0];
++degrees[trust[i][1]][1];
}
for (int i = 1; i <= n; ++i) {
// if (!degrees[i][0] && degrees[i][1] == n - 1)
if (!(degrees[i][0] | (degrees[i][1] ^ n - 1)))
return i;
}
return -1;
}
};
int main() {
std::printf("%d\n", Solution::findJudge(4, {{1, 3}, {1, 4}, {2, 3}, {2, 4}, {4, 3}}));
return 0;
}

View File

@ -3,4 +3,4 @@ PROJECT(2201)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2201 220103-CN.cpp) ADD_EXECUTABLE(2201 220103.cpp)