#include<cstdio>
#include<iostream>
using namespace std;
const int maxn=1000001;
struct Node
{
char c;
int next;
}node[maxn];
bool hashtable[maxn]={false};
int main()
{
int a,b,n;
cin>>a>>b>>n;
for (int i=0;i<n;i++)
{
int t1,t2;
char t;
scanf("%d %c %d",&t1,&t,&t2);
node[t1].c=t1;
node[t1].next=t2;
}
int i=-1;
for(i=a;i!=-1;)
{
hashtable[i]=true;
i=node[i].next;
}
for(i=b;i!=-1;)
{
if(hashtable[i]==true)
break;
i=node[i].next;
}
if(i==-1)
{
printf("-1");
}
else
{
printf("%05d",i);
}
}
A1032 Sharing (25) 静态链表
最新推荐文章于 2024-11-16 16:44:44 发布
本文介绍了一个使用C++实现的图遍历算法,通过建立节点结构体和哈希表来检测两个不同起点的路径是否有交集。具体包括读取输入数据,构建图的节点连接,并检查从两个不同节点出发的路径是否相遇。

310

被折叠的 条评论
为什么被折叠?



