Pagini recente » Cod sursa (job #2280516) | Cod sursa (job #507196) | Cod sursa (job #669733) | Cod sursa (job #1886527) | Cod sursa (job #1263690)
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
char vizitat[100001];
int coada[100001],distanta[100001],start,stop;
int n,m,s;
struct nod{
int val;
struct nod *next;
};
struct nod noduri[100001];
int main()
{
freopen("bfs.in","r",stdin);
freopen("bfs.out","w",stdout);
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
scanf("%d %d %d",&n,&m,&s);
int a,b;
for(int i=1; i<=m; i++)
{
scanf("%d %d",&a,&b);
struct nod *p=(struct nod*)malloc(8);
p=&noduri[a];
while(p->next)p=p->next;
p->next=(struct nod*)malloc(8);
p=p->next;
p->next=NULL;
p->val=b;
}
start=stop=1;
coada[stop]=s;
vizitat[s]='1';
while(start<=stop)
{
struct nod *p=&noduri[coada[start]];
while(p->next)
{
p=p->next;
if(vizitat[p->val]!='1'){stop++; vizitat[p->val]='1'; coada[stop]=p->val; distanta[p->val]=distanta[coada[start]]+1;
}
}
start++;
}
for(int i=1; i<=n; i++)if(!distanta[i] && i!=s)printf("-1 "); else printf("%d ",distanta[i]);
return 0;
}