Pagini recente » Cod sursa (job #2923295) | Cod sursa (job #1323847) | Cod sursa (job #2026756) | Cod sursa (job #1591319) | Cod sursa (job #2514526)
#include <fstream>
#include <bitset>
#include <queue>
#define Mil 1000000
#define Nod first
#define Dist second
using namespace std;
ifstream cin("bfs.in");
ofstream cout("bfs.out");
int n,m,x;
int vf[Mil+1],urm[Mil+1],last[100001],nr;
int dist[100001];
bitset <100001> viz;
void adauga(int from,int to)
{
vf[++nr]=to;
urm[nr]=last[from];
last[from]=nr;
}
void bfs(int nod)
{
viz[nod]=1;
queue <pair<int,int>> coada;
coada.push({nod,0});
while(!coada.empty())
{
int nod=coada.front().Nod,distNod=coada.front().Dist;
coada.pop();
dist[nod]=distNod;
for(int k=last[nod];k;k=urm[k])
if(!viz[ vf[k] ])
{
viz[ vf[k] ]=1;
coada.push({vf[k],distNod+1});
}
}
}
int main()
{
cin>>n>>m>>x;
for(int i,j,k=1;k<=m;k++)
{
cin>>i>>j;
adauga(i,j);
}
bfs(x);
for(int i=1;i<=n;i++)
if(dist[i]==0 && i!=x)
dist[i]=-1;
for(int i=1;i<=n;i++)
cout<<dist[i]<<' ';
return 0;
}