-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSolution.java
46 lines (34 loc) Β· 1000 Bytes
/
Solution.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
package Graph.prg49189;
import java.util.*;
class Solution {
LinkedList<Integer>[] graph;
public int solution(int n, int[][] edge) {
graph = new LinkedList[n+1];
for (int i = 1; i <= n; i++) graph[i] = new LinkedList<>();
for (int[] e : edge) {
graph[e[0]].add(e[1]);
graph[e[1]].add(e[0]);
}
return bfs(n);
}
int bfs(int n) {
Queue<Integer> q = new LinkedList<>();
boolean[] visited = new boolean[n+1];
q.offer(1);
visited[1] = true;
int curSize = 0;
while (!q.isEmpty()) {
curSize = q.size();
for (int i = 0; i < curSize; i++) {
int cur = q.poll();
for (int next : graph[cur]) {
if (!visited[next]) {
visited[next] = true;
q.offer(next);
}
}
}
}
return curSize;
}
}