- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- struct Node{
- int next;
- char ch;
- };
- Node node[100005];
- int vis[100005];
- int main(){
- int start1,start2,N,a,b;
- char c;
- scanf("%d%d%d",&start1,&start2,&N);
- memset(vis,0,sizeof(vis));
- for(int i=0;i<N;i++){
- scanf("%d %c %d",&a,&c,&b);
- node[a].ch=c;
- node[a].next=b;
- }
- while(start1!=-1){
- vis[start1]=1;
- start1=node[start1].next;
- }
- bool flag=false;
- int ans;
- while(start2!=-1){
- if(vis[start2]==1){
- flag=true;
- ans=start2;
- break;
- }
- start2=node[start2].next;
- }
- if(flag)
- printf("%05d\n",ans);
- else
- printf("-1\n");
- //system("pause");
- return 0;
- }
08-30
08-30