#include<iostream>#include<cstring>#include<vector>#include<stdio.h>#include<queue>#include<math.h>#include<stack>#include<algorithm>#include<map>#include<set>#define MAX 99999999typedeflonglong ll;usingnamespace std;int n,m;struct Node{int add;char data;int next;};
Node node[100001];intmain(){int s,d;scanf("%d%d%d",&s,&d,&n);for(int i =0;i<n;i++){int a,b;char x;scanf("%d %c%d",&a,&x,&b);
node[a].add = a;
node[a].data = x;
node[a].next = b;}int t =0;
set<int>s1;while(s!=-1){
s1.insert(s);
s = node[s].next;}while(d!=-1){if(s1.find(d)!=s1.end()){printf("%05d",d);
t =1;break;}
d = node[d].next;}if(!t)
cout<<-1;return0;}
代码2:
#include<iostream>#include<cstring>#include<vector>#include<stdio.h>#include<queue>#include<math.h>#include<stack>#include<algorithm>#include<map>#include<set>#define MAX 99999999usingnamespace std;typedeflonglong ll;struct node{char data;int add,next;};intmain(){
node node[100010];int x,y,n;
cin>>x>>y>>n;for(int i =0;i<n;i++){int a,b;char x;
cin>>a>>x>>b;
node[a].data = x;
node[a].next = b;}int lena =0;int lenb =0;int a = x;int b = y;while(a !=-1){
lena ++;
a = node[a].next;}while(b !=-1){
lenb ++;
b = node[b].next;}int t ;if(lena >lenb){
t = lena - lenb;while(t!=0){
x = node[x].next;t--;}}else{
t = lenb - lena;while(t!=0){
y = node[y].next;t--;}}int f =0;while(x !=-1&& y !=-1){if(x == y){
f =1;break;}
x = node[x].next;
y = node[y].next;}if(f ==1)printf("%05d",x);else
cout<<-1;return0;}