-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ2644.java
More file actions
57 lines (48 loc) · 1.6 KB
/
BOJ2644.java
File metadata and controls
57 lines (48 loc) · 1.6 KB
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
53
54
55
56
57
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
public class BOJ2644 {
static ArrayList<Integer> arr[];
static int a, b;
static boolean[] visited;
static int count = 0;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
a = Integer.parseInt(st.nextToken());
b = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(br.readLine());
visited = new boolean[n+1];
arr = new ArrayList[n+1];
for(int i = 1; i <= n; i++){
arr[i] = new ArrayList<Integer>();
}
for(int i = 0; i < m; i++){
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
arr[x].add(y);
arr[y].add(x);
}
int cnt = 0;
dfs(a, cnt);
if(count == 0) System.out.print(-1);
else System.out.print(count);
}
static void dfs(int n, int cnt){
visited[n] = true;
for(int i : arr[n]){
if(!visited[i]){
if(i == b){
count = cnt + 1;
return;
}
dfs(i, cnt+1);
}
}
}
}