-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLastShot.java
52 lines (48 loc) · 1.39 KB
/
LastShot.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
/** https://www.spoj.com/problems/LASTSHOT/ #dfs */
/** 1 3 1 4 4 5 2 1 expected output: 5 */
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Scanner;
class LastShot {
static int getImpact(int vertex, ArrayList<ArrayList<Integer>> graph, int totalVertex) {
boolean[] visitedArr = new boolean[totalVertex];
int count = 0;
Deque<Integer> queue = new LinkedList<>();
visitedArr[vertex] = true;
queue.addLast(vertex);
while (!queue.isEmpty()) {
count++;
int node = queue.pollLast();
for (int childNode : graph.get(node)) {
if (!visitedArr[childNode]) {
visitedArr[childNode] = true;
queue.addLast(childNode);
}
}
}
return count;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
ArrayList<ArrayList<Integer>> graph = new ArrayList<>(n + 1);
for (int i = 0; i < n + 1; i++) {
graph.add(new ArrayList<Integer>());
}
for (int i = 0; i < m; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
graph.get(a).add(b);
}
int maxImpact = 1;
for (int node = 1; node < graph.size(); node++) {
int count = getImpact(node, graph, n + 1);
if (count > maxImpact) {
maxImpact = count;
}
}
System.out.println(maxImpact);
}
}