#include<cstdio>
using namespace std;
struct Node{
int next;
char data;
bool flag;
}node[100000];
int main(){
int s1, s2,a, b, num;
scanf("%d%d%d", &s1, &s2, &num);
for(int i = 0; i < num; i++){
char c;
scanf("%d %c %d", &a, &c, &b);
node[a] = {b, c, false};
}
for(int i = s1; i != -1; i = node[i].next){
node[i].flag = true;
}
for(int i = s2; i != -1; i = node[i].next){
if(node[i].flag == true){
printf("%05d", i);
return 0;
}
}
printf("-1");
return 0;
}
原文:https://www.cnblogs.com/tsruixi/p/12254254.html