forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindCelebrity.cpp
More file actions
36 lines (31 loc) · 934 Bytes
/
FindCelebrity.cpp
File metadata and controls
36 lines (31 loc) · 934 Bytes
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
#include <iostream>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define MAXN 50
// If no such celebrity exists, return -1.
int FindCelebrity(int mat[][MAXN], int n) {
int candidate = 0;
FOR(i,1,n-1) {
if (candidate == -1) {
candidate = i;
continue;
}
bool f1 = mat[candidate][i] > 0;
bool f2 = mat[i][candidate] > 0;
if (f1 == f2) candidate = -1;
else if (f1 && !f2) candidate = i;
}
if (candidate == -1) return -1;
REP(i,n) {
if (candidate == i) continue;
if (mat[candidate][i] > 0 || mat[i][candidate] == 0) return -1;
}
return candidate;
}
int main() {
int mat[4][MAXN] = {{0, 0, 1, 0}, {1, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 1, 0}};
int res = FindCelebrity(mat, 4);
cout << res << endl;
return 0;
}