-
Notifications
You must be signed in to change notification settings - Fork 3
/
CCC07S3.java
60 lines (60 loc) · 1.89 KB
/
CCC07S3.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
53
54
55
56
57
58
59
60
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
/**
* CCC '07 S3 - Friends
* Question type: Graph Theory
* 4/4 on DMOJ
* @author Tommy Pang
*/
public class CCC07S3 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static int [] friends = new int[10000], distance;
static boolean [] vis;
static int ans = 0;
public static void main(String[] args) throws IOException{
int N = Integer.parseInt(br.readLine());
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
friends[a] = b;
}
while (true) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
if (a == 0) return;
ans = 0;
BFS(a);
if (!vis[b]) {
System.out.println("No");
continue;
}
ans = distance[b]-1;
BFS(b);
if (!vis[a]) System.out.println("No");
else {
System.out.println("Yes "+ ans);
}
}
}
static void BFS(int a){
Queue<Integer> queue = new LinkedList<>();
queue.add(a);
vis = new boolean[10000];
distance = new int[10000];
vis[a] = true;
distance[a] = 0;
while (!queue.isEmpty()){
int cur = queue.poll();
if (!vis[friends[cur]]){
queue.add(friends[cur]);
distance[friends[cur]] = distance[cur]+1;
vis[friends[cur]] = true;
}
}
}
}