-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDuelingPhilosophersProblem.java
105 lines (97 loc) · 3.03 KB
/
DuelingPhilosophersProblem.java
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* https://icpcarchive.ecs.baylor.edu/index.php?option=onlinejudge&page=show_problem&problem=4206
#topological-sort
*/
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
class DuelingPhilosophersProblem {
public static boolean topoSort(
ArrayList<ArrayList<Integer>> graph, int[] inDegree, ArrayList<Integer> result) {
int sz = graph.size();
boolean[] visitedArr = new boolean[sz];
Deque<Integer> zeroInDegree = new LinkedList<>();
for (int i = 1; i < inDegree.length; i++) {
if (inDegree[i] == 0) {
zeroInDegree.addLast(i);
}
}
while (!zeroInDegree.isEmpty()) {
int node = zeroInDegree.pollFirst();
visitedArr[node] = true;
result.add(node);
for (int neighbour : graph.get(node)) {
if (!visitedArr[neighbour]) {
inDegree[neighbour] -= 1;
if (inDegree[neighbour] == 0) {
zeroInDegree.addLast(neighbour);
}
}
}
}
boolean ans = true;
for (int degree : inDegree) {
if (degree > 0) {
ans = false;
}
}
return ans;
/**
* other way: - no need parent graph ans = 1; while (!zeroInDegree.isEmpty()) { // int sz =
* zeroInDegree.size(); // if sz > 1: ans = 2 int node = zeroInDegree.pollFirst();
* visitedArr[node] = true; result.add(node); for (int neighbour : graph.get(node)) {
* if(!visitedArr[neighbour]) { inDegree[neighbour] -= 1; if(inDegree[neighbour] == 0) {
* zeroInDegree.addLast(neighbour); } } } }
*
* <p>boolean flg = true; for (int degree : inDegree) { if (degree > 0) { flg = false; } } if
* (flg == false) return 0; return ans;
*/
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
int n = sc.nextInt();
int m = sc.nextInt();
if (n == 0 && m == 0) break;
int sz = n + 1;
ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
ArrayList<HashSet<Integer>> parentGraph = new ArrayList<>();
for (int i = 0; i < sz; i++) {
graph.add(new ArrayList<Integer>());
parentGraph.add(new HashSet<>());
}
ArrayList<Integer> result = new ArrayList<>();
int[] inDegree = new int[sz];
int u, v;
for (int i = 0; i < m; i++) {
u = sc.nextInt();
v = sc.nextInt();
graph.get(u).add(v);
inDegree[v] += 1;
parentGraph.get(v).add(u);
}
boolean ans = topoSort(graph, inDegree, result);
if (ans) {
boolean multipleOrderFlg = false;
u = result.get(0);
for (int i = 1; i < result.size(); i++) {
v = result.get(i);
if (!parentGraph.get(v).contains(u)) {
multipleOrderFlg = true;
break;
}
u = v;
}
if (multipleOrderFlg) {
System.out.println(2);
} else {
System.out.println(1);
}
} else {
System.out.println(0);
}
}
return;
}
}