Pagini recente » Cod sursa (job #1630622) | Cod sursa (job #2909362) | Cod sursa (job #110402) | Cod sursa (job #3238570) | Cod sursa (job #2031194)
#include <iostream>
#include <fstream>
#include <stdio.h>
#define NN 100005
using namespace std;
int cost[NN];
int n,m,S;
bool viz[NN];
int coada[NN];
struct nod {
int info;
nod *next;
}*L[NN];
void adaug (int x, int y )
{
//adaug pe y la lista lui x
nod *aux=new nod;
aux->info=y;
aux->next=L[x];
L[x]=aux;
}
void read(){
freopen("bfs.in","r",stdin);
scanf("%d%d%d",&n,&m,&S);
for(int k=1 ;k<=m ;++k)
{
int x,y;
scanf("%d%d",&x,&y);
adaug(x,y);
}
}
int main()
{
read();
int stanga=1;
int dreapta=0;
coada[++dreapta]=S;
viz[S]=true;
int z;
while(stanga<=dreapta)
{
nod *aux;
for(aux=L[coada[stanga]];aux; aux=aux->next)
if(!viz[aux->info])
{
coada[++dreapta]=aux->info;
viz[aux->info]=true;
cost[aux->info]=cost[coada[stanga]]+1;
}
++stanga;
}
freopen("bfs.out","w",stdout);
for(int i=1;i<=n; ++i)
if(!cost[i]&&i!=S)printf("-1 ");
else if(i==S)printf("0 ");
else printf("%d ",cost[i]);
return 0;
}