-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCelebrity Problem
More file actions
51 lines (42 loc) · 1.23 KB
/
Celebrity Problem
File metadata and controls
51 lines (42 loc) · 1.23 KB
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
47
48
49
50
51
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
// write your code here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[][] arr = new int[n][n];
for (int j = 0; j < n; j++) {
String line = br.readLine();
for (int k = 0; k < n; k++) {
arr[j][k] = line.charAt(k) - '0';
}
}
findCelebrity(arr);
}
public static void findCelebrity(int[][] arr) {
Stack<Integer> st = new Stack<>();
for(int i=0;i<arr.length;i++){
st.push(i);
}
while(st.size()>=2){
int i = st.pop();
int j = st.pop();
if(arr[i][j]==1){
st.push(j);
}else{
st.push(i);
}
}
int pot = st.pop();
for(int i=0;i<arr.length;i++){
if(i!=pot){
if(arr[i][pot]==0 || arr[pot][i]==1){
System.out.println("none");
return;
}
}
}
System.out.println(pot);
}
}