-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFarthestNode.java
55 lines (43 loc) · 1.27 KB
/
FarthestNode.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
import java.util.*;
class Solution {
private ArrayList<ArrayList<Integer>> list = new ArrayList<>();
private boolean visited[];
private int depth[];
public int solution(int n, int[][] edge) {
init(n,edge);
bfs(1);
Arrays.sort(depth);
int answer = 0;
int max = depth[n];
for(int i=n; i>=0; i--){
if(max != depth[i]) break;
answer++;
}
return answer;
}
public void bfs(int start){
Queue<Integer> queue = new LinkedList<>();
queue.add(start);
depth[start] = 1;
visited[start] = true;
while(!queue.isEmpty()){
int cur = queue.poll();
for(int vertex : list.get(cur)){
if(!visited[vertex]){
queue.add(vertex);
depth[vertex] = depth[cur]+1;
visited[vertex] = true;
}
}
}
}
public void init(int n, int [][] edge){
for(int i=0; i<=n; i++) list.add(new ArrayList<>());
for(int i=0; i<edge.length; i++){
list.get(edge[i][0]).add(edge[i][1]);
list.get(edge[i][1]).add(edge[i][0]);
}
visited = new boolean[n+1];
depth = new int[n+1];
}
}