File tree Expand file tree Collapse file tree 3 files changed +50
-0
lines changed
Expand file tree Collapse file tree 3 files changed +50
-0
lines changed Original file line number Diff line number Diff line change 285285| 07 | | [ JUNGOL-1863 종교] ( ./src/Graph/jo1863 ) | union-find : union-by-rank |
286286| 08 | | [ Baekjoon-21276 계보 복원가 호석] ( ./src/Graph/P21276 ) | [ 위상정렬] ( ./notes/Graph/TopologicalSort.md ) |
287287| 09 | ⭐️ | [ Baekjoon-2458 키 순서] ( ./src/Graph/P2458 ) | |
288+ | 10 | | [ Programmers 가장 먼 노드] ( ./src/Graph/prg49189 ) | |
288289
289290### Minimum Spanning Tree (MST)
290291
Original file line number Diff line number Diff line change 1+ ## [ programmers - 그래프] 가장 먼 노드
2+
3+ ![ image] ( https://user-images.githubusercontent.com/22045163/116100368-ce13ea80-a6e7-11eb-9309-04ad089d34ca.png )
Original file line number Diff line number Diff line change 1+ package Graph .prg49189 ;
2+
3+ import java .util .*;
4+
5+ class Solution {
6+
7+ LinkedList <Integer >[] graph ;
8+
9+ public int solution (int n , int [][] edge ) {
10+
11+ graph = new LinkedList [n +1 ];
12+ for (int i = 1 ; i <= n ; i ++) graph [i ] = new LinkedList <>();
13+
14+ for (int [] e : edge ) {
15+ graph [e [0 ]].add (e [1 ]);
16+ graph [e [1 ]].add (e [0 ]);
17+ }
18+
19+ return bfs (n );
20+ }
21+
22+ int bfs (int n ) {
23+
24+ Queue <Integer > q = new LinkedList <>();
25+ boolean [] visited = new boolean [n +1 ];
26+ q .offer (1 );
27+ visited [1 ] = true ;
28+
29+ int curSize = 0 ;
30+
31+ while (!q .isEmpty ()) {
32+ curSize = q .size ();
33+ for (int i = 0 ; i < curSize ; i ++) {
34+ int cur = q .poll ();
35+ for (int next : graph [cur ]) {
36+ if (!visited [next ]) {
37+ visited [next ] = true ;
38+ q .offer (next );
39+ }
40+ }
41+ }
42+ }
43+
44+ return curSize ;
45+ }
46+ }
You can’t perform that action at this time.
0 commit comments