社区讨论
40pts玄关
P1346电车参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @m38ac2ti
- 此快照首次捕获于
- 2024/11/08 13:17 去年
- 此快照最后确认于
- 2025/11/04 15:08 4 个月前
CPP
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,x,y,k,yy,edge_sum,head[100005],railway[100005];
bool vis[100005];
queue<ll>q;
struct edge{
ll next,to;
}a[100005];
void add_edge(ll from,ll to){
edge_sum++;
a[edge_sum].next=head[from];
a[edge_sum].to=to;
head[from]=edge_sum;
}
void bfs(ll x){
q.push(x);
vis[x]=true;
while(!q.empty()){
ll t=q.front();
q.pop();
for(int i=head[t];i!=0;i=a[i].next)
{
if(vis[a[i].to])continue;
if(a[i].to==y)return ;
q.push(a[i].to);
vis[a[i].to]=true;
railway[a[i].to]=railway[t]+1;
}
}
cout<<-1;
exit(0);
}
signed main(){
cin>>n>>x>>y;
for(int i=1;i<=n;i++)
{
cin>>k;
for(int j=1;j<=k;j++)
cin>>yy,add_edge(i,yy);
}
bfs(x);
cout<<railway[y];
return 0;
}
回复
共 0 条回复,欢迎继续交流。
正在加载回复...