-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy paththe_great_game.cpp
60 lines (47 loc) · 1.11 KB
/
the_great_game.cpp
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
#include <iostream>
#include <vector>
#include <cmath>
#include <limits>
using namespace std;
int solve() {
int n; std::cin >> n;
int m; std::cin >> m;
int r; std::cin >> r; r = r - 1;
int b; std::cin >> b; b = b - 1;
vector<vector<int>> neighbors(n, vector<int>());
int u, v;
for (int i = 0; i < m; ++i) {
cin >> u;
cin >> v;
// u,v are in range [1...n], transform to [0...n-1]
neighbors[u - 1].push_back(v - 1);
}
vector<int> minSteps(n, numeric_limits<int>::max());
vector<int> maxSteps(n, -1);
minSteps[n - 1] = 0;
maxSteps[n - 1] = 0;
for (int u = n - 2; u >= 0; --u) {
for (int v : neighbors[u]) {
minSteps[u] = min(maxSteps[v] + 1, minSteps[u]);
maxSteps[u] = max(minSteps[v] + 1, maxSteps[u]);
}
}
if (minSteps[r] < minSteps[b]) {
return 0;
}
if (minSteps[r] > minSteps[b]) {
return 1;
}
if (minSteps[r] % 2 == 1) {
return 0;
}
return 1;
}
int main() {
ios_base::sync_with_stdio(false);
int t; std::cin >> t;
for (int i = 0; i < t; ++i) {
int solution = solve();
std::cout << solution << std::endl;
}
}