PAT甲题题解-1032. Sharing (25)-链表水题

时间:2023-03-09 19:04:10
PAT甲题题解-1032. Sharing (25)-链表水题
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cmath>
using namespace std;
/*
链表题

*/
int n; struct Word{
int addr;
char ch;
int next=-;
}word[];
int vis[];
int main()
{
int first1,first2;
int adr,nxt;
char ch;
scanf("%d %d %d",&first1,&first2,&n);
for(int i=;i<n;i++){
scanf("%d %c %d",&adr,&ch,&nxt);
word[adr].addr=adr;
word[adr].ch=ch;
word[adr].next=nxt;
}
memset(vis,,sizeof(vis));
while(first1!=-){
vis[first1]=;
first1=word[first1].next;
}
bool flag=false;
while(first2!=-){
if(vis[first2]){
flag=true;
break;
}
first2=word[first2].next;
}
if(flag)
printf("%05d\n",first2);
else
printf("-1\n");
return ;
}