Pagini recente » Cod sursa (job #2127719) | Cod sursa (job #612164) | Cod sursa (job #3192203) | Cod sursa (job #1641038) | Cod sursa (job #2232709)
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
ifstream fin ("bfs.in");
ofstream fout ("bfs.out");
queue<int> q;
int cost[100000];
struct graf
{
int y;
graf* next;
}*a[100001];
int main()
{
int n,m,s;
fin>>n>>m>>s;
for (int i=0; i<m; i++)
{
int x,y;
fin>>x>>y;
graf* nod=new graf;
nod->y=y;
nod->next=a[x];
a[x]=nod;
}
for (int i=1; i<=n; i++)
cost[i]=-1;
q.push(s);
cost[s]=0;
while(!q.empty())
{
int nod=q.front();
q.pop();
graf *i=a[nod];
while (i!=NULL)
{
if (cost[i->y]==-1)
{
q.push(i->y);
cost[i->y]=cost[nod]+1;
}
i=i->next;
}
}
for (int i=1; i<=n; i++)
fout<<cost[i]<<' ';
return 0;
}