-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path241112-2644.java
51 lines (46 loc) · 1.49 KB
/
241112-2644.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
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 Main {
static List<Integer>[] map;
static boolean[] visited;
static int answer;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
map = new ArrayList[n + 1];
visited = new boolean[n + 1];
for (int i = 1; i <= n; i++) {
map[i] = new ArrayList<>();
}
StringTokenizer st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(br.readLine());
for (int i = 0; i < m; i++) {
st = new StringTokenizer(br.readLine());
int p = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
map[p].add(c);
map[c].add(p);
}
answer = -1;
dfs(x, y, 0);
System.out.println(answer);
}
static void dfs(int start, int end, int cnt) {
if (start == end) {
answer = cnt;
return;
}
visited[start] = true;
for (final int next : map[start]) {
if (!visited[next]) {
dfs(next, end, cnt + 1);
}
}
}
}