-
Notifications
You must be signed in to change notification settings - Fork 2
/
Main.java
52 lines (41 loc) Β· 1.35 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package DFS.P13023;
import java.io.*;
import java.util.*;
public class Main {
static int N, M;
static LinkedList<Integer>[] list;
static boolean[] visited;
static int ans = 0;
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/DFS/P13023/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
list = new LinkedList[N];
for (int i = 0; i < N; i++) list[i] = new LinkedList<>();
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
list[a].add(b);
list[b].add(a);
}
visited = new boolean[N];
for (int i = 0; i < N; i++) {
if (ans == 0) dfs(i, 1);
}
System.out.println(ans);
}
static void dfs(int cur, int count) {
if (count == 5) {
ans = 1;
return;
}
visited[cur] = true;
for (int to : list[cur]) {
if (!visited[to]) dfs(to, count + 1);
}
visited[cur] = false;
}
}