Skip to content

Commit

Permalink
Merge pull request #26 from AlgoLeadMe/7-mong3
Browse files Browse the repository at this point in the history
7-mong3
  • Loading branch information
mong3125 authored Mar 18, 2024
2 parents aae81a5 + dc134e9 commit dd10e7b
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions mong3125/DFS/BOJ13023.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class BOJ13023 {

static int N, M; // ์‚ฌ๋žŒ์˜ ์ˆ˜, ์นœ๊ตฌ ๊ด€๊ณ„ ์ˆ˜
static List<Integer>[] friends;
static boolean[] visited; // ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ์ธ์ง€
static boolean isExist = false;

public static void main(String[] args) throws IOException {
// ์ž…๋ ฅ
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
friends = new ArrayList[N];
for (int i = 0; i < N; i++) {
friends[i] = new ArrayList<>();
}
visited = new boolean[N+1];

// ์–‘๋ฐฉํ–ฅ ์นœ๊ตฌ๊ด€๊ณ„ ๋งบ์–ด์ฃผ๊ธฐ
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());

friends[a].add(b);
friends[b].add(a);
}

for (int i = 0; i < N; i++) {
dfs(i, 1);
if (isExist) break;
}

int answer = isExist ? 1 : 0;
System.out.println(answer);
}

public static void dfs(int personId, int depth) {
if (depth >= 5 || isExist) {
isExist = true;
return;
}

visited[personId] = true;
for (int i : friends[personId]) {
if (!visited[i]) {
dfs(i, depth + 1);
}
}
visited[personId] = false;
}
}

0 comments on commit dd10e7b

Please sign in to comment.