-1
#include <iostream>
using namespace std;
int counts[100001];
int linkList[100001];
int target = -1;
int main() {
int start1, start2;
int n;
for(int i=0; i<100001; i++) {
counts[i] = 0;
linkList[i] = -1;
}
cin>>start1>>start2>>n;
if(start1 == start2) {
printf("%05d\n", start1);
return 0;
}
while(n--) {
int value1, value2;
char s2;
cin>>value1>>s2>>value2;
linkList[value1] = value2;
}
int i;
for(i = start1; i!=-1; i = linkList[i]) {
counts[i] = 1;
}
for(i = start2; i!=-1 && !counts[i]; i = linkList[i]) {
counts[i] = 1;
}
if(i != -1) {
printf("%05d\n", i);
} else {
printf("-1\n");
}
return 0;
}
原文地址:http://blog.csdn.net/jason_wang1989/article/details/44040453