PAT 甲级 1032 Sharing
#include <bits/stdc++.h>
using namespace std;
const int kMAXN = 100000;
struct Node {
int address, next, flag{false};
char data;
} node[kMAXN];
int main()
{
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int head1, head2, N;
cin >> head1 >> head2 >> N;
Node tmp;
while (N--) {
cin >> tmp.address >> tmp.data >> tmp.next;
node[tmp.address] = tmp;
}
while (head1 != -1) {
node[head1].flag = true;
head1 = node[head1].next;
}
while (head2 != -1 && node[head2].flag != true) {
head2 = node[head2].next;
}
head2 == -1 ? printf("-1\n") : printf("%05d\n", head2);
}