pat-1032

题目

Sharing
https://pintia.cn/problem-sets/994805342720868352/problems/994805460652113920

分析

通过第一个首地址走完,并标记走过的
再从第二个首地址开始走,只要遇到标记就说明这个是suffix

代码


#include<cstdio>
using namespace std;

const int maxn=1e5+10;
int st1,st2;
int N;

struct {
char date;
int next;
bool flag;
}node[maxn];


int main(){
scanf("%d%d%d",&st1,&st2,&N);
int adress,next;
char date;
while(N--){
scanf("%d %c %d",&adress,&date,&next);
node[adress]={date,next,false};
}
for(int i=st1;i!=-1;i=node[i].next){
node[i].flag=true;
}
for(int i=st2;i!=-1;i=node[i].next){
if(node[i].flag){
printf("%05d",i);
return 0;
}
}
cout<<-1;
return 0;
}