代码如下:
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 using namespace std; 8 int main(){ 9 int *m = new int[100001];10 int *exist = new int[100001];11 memset(m,-1,sizeof(m));12 memset(exist,-1,sizeof(exist));13 int sum;14 int a,b;15 scanf("%d %d %d",&a,&b,&sum);16 m[a] = -1;17 m[b] = -1;18 for(int i = 0; i < sum; i++){19 char tem[10];20 int pre,next;21 scanf("%d %s %d",&pre,tem,&next);22 m[pre] = next;23 }24 int head = a;25 while(head != -1){26 //把第一个链表存进哈希表exist中27 exist[head] = 1;28 head = m[head];29 }30 head = b;31 while(head != -1){32 if(exist[head] == 1){33 break;34 }35 head = m[head];36 }37 if(head == -1) printf("%d\n",head);38 else printf("%05d\n",head);39 return 0;40 }