-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
65 lines (54 loc) · 1.65 KB
/
Solution.java
File metadata and controls
65 lines (54 loc) · 1.65 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
56
57
58
59
60
61
62
63
64
65
import java.util.List;
import java.util.ArrayList;
import java.util.Queue;
import java.util.LinkedList;
import java.util.Arrays;
public class Solution {
static List<Integer>[] adj;
static boolean[] visited;
static int[] level;
public static void main(String[] args){
int n = 6;
int[][] vertex = {
{3,6}
,{4,3}
,{3,2}
,{1,3}
,{1,2}
,{2,4}
,{5,2}
};
int answer = solution(n, vertex);
System.out.println(answer);
}
public static int solution(int n, int[][] edge) {
adj = new ArrayList[n+1];
for(int i=0; i<n+1; i++) adj[i] = new ArrayList<Integer>();
for(int i=0; i<edge.length; i++){
adj[edge[i][0]].add(edge[i][1]);
adj[edge[i][1]].add(edge[i][0]);
}
visited = new boolean[n+1];
level = new int[n+1]; // depth를 저장할 것임
bfs();
int max = Arrays.stream(level).max().getAsInt();
int answer = (int)Arrays.stream(level).filter(i -> i == max).count();
return answer;
}
public static void bfs(){
Queue<int[]> q = new LinkedList<>();
q.add(new int[] {1,0}); // [0] idx, [1] depth
visited[1] = true;
level[1] = 0;
while(!q.isEmpty()){
int[] i = q.poll();
for(int next : adj[i[0]]){
if(!visited[next]){
visited[next] = true;
level[next] = i[1]+1;
q.add(new int[] {next, i[1]+1});
}
}
}
}
}