File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import java .io .*;
2+ import java .util .*;
3+
4+ public class Beakjoon_13023 {
5+ static List <Integer >[] graph ;
6+ static boolean [] visited ;
7+ static boolean found = false ;
8+
9+ public static void main (String [] args ) throws IOException {
10+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
11+ StringTokenizer st = new StringTokenizer (br .readLine ());
12+
13+ int n = Integer .parseInt (st .nextToken ());
14+ int m = Integer .parseInt (st .nextToken ());
15+
16+ graph = new ArrayList [n ];
17+ for (int i = 0 ; i < n ; i ++) {
18+ graph [i ] = new ArrayList <>();
19+ }
20+
21+ // 친구 관계 입력 받기
22+ for (int i = 0 ; i < m ; i ++) {
23+ st = new StringTokenizer (br .readLine ());
24+ int a = Integer .parseInt (st .nextToken ());
25+ int b = Integer .parseInt (st .nextToken ());
26+ graph [a ].add (b );
27+ graph [b ].add (a );
28+ }
29+
30+ visited = new boolean [n ];
31+
32+ for (int i = 0 ; i < n ; i ++) {
33+ Arrays .fill (visited , false );
34+ dfs (i , 1 );
35+
36+ if (found ) break ;
37+ }
38+
39+ System .out .println (found ? 1 : 0 );
40+ }
41+
42+ static void dfs (int current , int depth ) {
43+ if (depth == 5 ) {
44+ found = true ;
45+ return ;
46+ }
47+
48+ visited [current ] = true ;
49+
50+ for (int next : graph [current ]) {
51+ if (!visited [next ]) {
52+ dfs (next , depth + 1 );
53+ if (found ) return ;
54+ }
55+ }
56+
57+ visited [current ] = false ;
58+ }
59+ }
You can’t perform that action at this time.
0 commit comments