-
Notifications
You must be signed in to change notification settings - Fork 0
/
E.cpp
68 lines (60 loc) · 1.65 KB
/
E.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
61
62
63
64
65
66
67
68
#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <sstream>
using namespace std;
int simulate(const vector<int>& x, const vector<int>& y, const vector<int>& t, int A, int B, char direction) {
int step = 0;
int current_A = A;
int current_B = B;
set<pair<int, int>> visited;
while (visited.find({current_A, current_B}) == visited.end()) {
if (t[current_A] != t[current_B]) {
return step;
}
visited.insert({current_A, current_B});
int next_A, next_B;
if (direction == 'X') {
next_A = x[current_A];
next_B = x[current_B];
} else {
next_A = y[current_A];
next_B = y[current_B];
}
current_A = next_A;
current_B = next_B;
step++;
}
return -1; // "GG"
}
int main() {
int T;
cin >> T;
cin.ignore(); // 忽略换行符
while (T--) {
string line;
getline(cin, line);
stringstream ss(line);
int n, A, B;
ss >> n >> A >> B;
vector<int> x(n), y(n), t(n);
for (int i = 0; i < n; ++i) {
getline(cin, line);
stringstream ss(line);
ss >> x[i] >> y[i] >> t[i];
}
int step_X = simulate(x, y, t, A, B, 'X');
int step_Y = simulate(x, y, t, A, B, 'Y');
if (step_X != -1 && step_Y != -1) {
cout << min(step_X, step_Y) << endl;
} else if (step_X != -1) {
cout << step_X << endl;
} else if (step_Y != -1) {
cout << step_Y << endl;
} else {
cout << "GG" << endl;
}
}
return 0;
}