Skip to content

Commit 5295a10

Browse files
authored
[20250805] BOJ / G5 / ABCDE / 이준희
1 parent a654fbd commit 5295a10

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

JHLEE325/202508/05 BOJ G5 ABCDE.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
7+
static int n, m;
8+
static List<Integer>[] friends;
9+
static boolean[] visited;
10+
static boolean found = false;
11+
12+
public static void main(String[] args) throws Exception {
13+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
14+
StringTokenizer st = new StringTokenizer(br.readLine());
15+
16+
n = Integer.parseInt(st.nextToken());
17+
m = Integer.parseInt(st.nextToken());
18+
19+
friends = new ArrayList[n];
20+
for (int i = 0; i < n; i++) friends[i] = new ArrayList<>();
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+
friends[a].add(b);
27+
friends[b].add(a);
28+
}
29+
30+
visited = new boolean[n];
31+
for (int i = 0; i < n && !found; i++) {
32+
visited[i] = true;
33+
dfs(i, 1);
34+
visited[i] = false;
35+
}
36+
37+
System.out.println(found ? 1 : 0);
38+
}
39+
40+
static void dfs(int cur, int depth) {
41+
if (found) return;
42+
if (depth == 5) {
43+
found = true;
44+
return;
45+
}
46+
47+
for (int next : friends[cur]) {
48+
if (visited[next]) continue;
49+
visited[next] = true;
50+
dfs(next, depth + 1);
51+
visited[next] = false;
52+
if (found) return;
53+
}
54+
}
55+
}
56+
57+
```

0 commit comments

Comments
 (0)