- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongest_Path.java
More file actions
Latest commit
55 lines (46 loc) · 1.19 KB
/
Copy pathLongest_Path.java
File metadata and controls
55 lines (46 loc) · 1.19 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
52
53
54
55
importjava.util.*;
importjava.io.*;
publicclassLongest_Path {
publicstaticintn, m;
publicstaticMap<Integer, LinkedList<Integer>> graph = newHashMap<>();
publicstaticint[] dp;
publicstaticintTopologicalSort() {
Queue<Integer> qu = newLinkedList<>();
for (inti = 1; i <= n; i++)
if (dp[i] == 0)
qu.add(i);
intcount = 0;
while (!qu.isEmpty()) {
intsize = qu.size();
for (intk = 0; k < size; k++) {
LinkedList<Integer> temp = graph.get(qu.poll());
if (temp == null)
continue;
for (intt : temp) {
dp[t]--;
if (dp[t] == 0)
qu.add(t);
}
}
count++;
}
returncount - 1;
}
publicstaticvoidmain(String[] args) throwsIOException {
BufferedReaderbr = newBufferedReader(newInputStreamReader(System.in));
String[] s = br.readLine().split(" ");
n = Integer.parseInt(s[0]);
m = Integer.parseInt(s[1]);
dp = newint[n + 1];
for (inti = 0; i < m; i++) {
s = br.readLine().split(" ");
intx = Integer.parseInt(s[0]);
inty = Integer.parseInt(s[1]);
if (!graph.containsKey(x))
graph.put(x, newLinkedList<Integer>());
graph.get(x).add(y);
dp[y]++;
}
System.out.println(TopologicalSort());
}
}