-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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]; | ||
} | ||
} |