forked from wotjd4305/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproGraph1.java
More file actions
67 lines (54 loc) · 1.64 KB
/
proGraph1.java
File metadata and controls
67 lines (54 loc) · 1.64 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.*;
public class proGraph1{
public static void main(String[] args) {
int edge[][] = {{3,6},{4,3},{3,2},{1,3},{1,2},{2,4},{5,2}};
int n = 6;
System.out.println(solution(n,edge));
}
public static int solution(int n, int[][] edge) {
ArrayList<ArrayList<Integer>> list = new <ArrayList<Integer>> ArrayList();
Queue<Integer> q = new LinkedList<Integer>();
//n이 6이면 n개의 배열이고 {1,2}, {1,3}
for(int i = 0; i < edge.length; i++) {
list.add(new ArrayList<Integer>());
}
for(int i = 0; i < edge.length; i++) {
int m = edge[i][0];
int h = edge[i][1];
//앞뒤로 채움, 인접행렬같이.. ArrayList는 1부터 시작. 즉 1,2,3,4,5,6
list.get(m).add(h);
list.get(h).add(m);
}
//최소 거리
int[] d = new int[n+1];
Arrays.fill(d, -1);
//첫번째니까 0
d[1] = 0;
q.add(1);
int u = 0;
//전형적인 BFS,
while(q.size() > 0) {
//처음에는 1부터 시작이니 u=1
u = q.poll();
//처음회전에는 {1,3} {1,2}니까 e = 3과 2가나옴
for(int e : list.get(u)) {
if(d[e] == -1) {
d[e] = d[u]+1;
q.add(e);
}
}
}
int count = 0;
int max = 0;
for(int i : d)
{
max = Math.max(max, i);
}
for(int i : d)
{
if(max == i)
count++;
}
return count;
}
}