Pagini recente » Cod sursa (job #2210711) | Cod sursa (job #356391) | Cod sursa (job #2845226) | Cod sursa (job #2967666) | Cod sursa (job #2098122)
#include <iostream>
#include <vector>
#include <fstream>
#define nmax 100010
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int n,m,L,start;
int Stiva[nmax],ans[nmax],G[nmax];
struct node
{
int x;
node*urm;
};
node *p[nmax];
void BFS(int nod)
{
for(int i =1 ; i <= n ; i++)
ans[i]=-1;
L=1;
Stiva[L]=nod;
ans[nod]=0;
node *q;
int w;
for(int i =1; i <= L; i ++)
for(int q=p[Stiva[i]]; q!=NULL ; g=g->urm)
{
w=q->x;
if(ans[Stiva[i][w]]==-1)
{
Stiva[++L]=w;
ans[Stiva[L]]=ans[Stiva[i]]+1;
}
}
}
void adauga(int x, nod*& pp)
{
nod*q=new node;
q->x=x;
q->urm=pp;
pp=q;
}
int main()
{
int x,y;
fin>>n>>m>>start;
for(int i = 1; i <= m ; i ++)
{
fin>>x>>y;
adauga(y,p[x]);
}
BFS(start);
for(int i =1; i <= n ; i++)
fout<<ans[i]<<" ";
return 0;
}