-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOliverAndTheGame.java
More file actions
76 lines (68 loc) · 1.93 KB
/
OliverAndTheGame.java
File metadata and controls
76 lines (68 loc) · 1.93 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/** #graph #dfs */
import java.util.ArrayList;
import java.util.Scanner;
class OliverAndTheGame {
public static void main(String[] args) {
// input
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sz = n + 1;
ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
ArrayList<Time> timeTable = new ArrayList<>();
for (int i = 0; i < sz; i++) {
graph.add(new ArrayList<>());
timeTable.add(new Time(0, 0));
}
int u, v;
for (int i = 0; i < n - 1; i++) {
u = sc.nextInt();
v = sc.nextInt();
graph.get(u).add(v);
graph.get(v).add(u);
}
// calculate start and finish time of each vertex.
Time curTime = new Time(1, 0);
dfs(graph, 1, curTime, timeTable);
// query
int q = sc.nextInt();
int direct, x, y;
for (int i = 0; i < q; i++) {
direct = sc.nextInt();
x = sc.nextInt();
y = sc.nextInt();
if (direct == 0) {
if (timeTable.get(x).start < timeTable.get(y).start
&& timeTable.get(x).finish > timeTable.get(y).finish) {
System.out.println("YES");
continue;
}
} else { // direct == 1
if (timeTable.get(x).start > timeTable.get(y).start
&& timeTable.get(x).finish < timeTable.get(y).finish) {
System.out.println("YES");
continue;
}
}
System.out.println("NO");
}
}
public static void dfs(
ArrayList<ArrayList<Integer>> graph, int source, Time curTime, ArrayList<Time> timeTable) {
timeTable.get(source).start = curTime.start;
for (int neighbour : graph.get(source)) {
if (timeTable.get(neighbour).start == 0) {
++curTime.start;
dfs(graph, neighbour, curTime, timeTable);
}
}
timeTable.get(source).finish = ++curTime.start;
}
}
class Time {
int start;
int finish;
Time(int start, int finish) {
this.start = start;
this.finish = finish;
}
}