File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 44
55### 성능 요약
66
7- 메모리: 9436 KB, 시간: 92 ms
7+ 메모리: 9448 KB, 시간: 84 ms
88
99### 분류
1010
1111그래프 이론, 그래프 탐색, 너비 우선 탐색, 깊이 우선 탐색
1212
1313### 제출 일자
1414
15- 2026년 2월 11일 21:21:31
15+ 2026년 2월 17일 16:47:53
1616
1717### 문제 설명
1818
Original file line number Diff line number Diff line change @@ -7,32 +7,28 @@ let input = fs
77 . map ( ( x ) => x . replace ( "\r" , "" ) ) ;
88
99const N = Number ( input [ 0 ] ) ;
10- const M = Number ( input [ 1 ] ) ;
11-
12- const visited = Array . from ( { length : N + 1 } ) . fill ( false ) ;
13- const graph = Array . from ( { length : N + 1 } , ( ) => [ ] ) ;
14-
15- let cnt = 0 ;
16-
17- for ( let i = 2 ; i < M + 2 ; i ++ ) {
10+ const T = Number ( input [ 1 ] ) ;
11+ let result = 0 ;
12+ const graph = Array . from ( { length : N + 1 } , ( ) => Array ( ) ) ;
13+ for ( let i = 2 ; i <= T + 1 ; i ++ ) {
1814 const [ x , y ] = input [ i ] . split ( " " ) . map ( Number ) ;
1915 graph [ x ] . push ( y ) ;
2016 graph [ y ] . push ( x ) ;
2117}
2218
23- const queue = [ ] ;
24- queue . push ( 1 ) ;
25- visited [ 1 ] = true ;
19+ const visited = Array ( N + 1 ) . fill ( false ) ;
20+
21+ dfs ( 1 ) ;
22+
23+ function dfs ( V ) {
24+ visited [ V ] = true ;
2625
27- while ( queue . length ) {
28- const current = queue . shift ( ) ;
29- for ( const next of graph [ current ] ) {
26+ for ( const next of graph [ V ] ) {
3027 if ( ! visited [ next ] ) {
31- visited [ next ] = true ;
32- queue . push ( next ) ;
33- cnt ++ ;
28+ result ++ ;
29+ dfs ( next ) ;
3430 }
3531 }
3632}
3733
38- console . log ( cnt ) ;
34+ console . log ( result ) ;
You can’t perform that action at this time.
0 commit comments