Pagini recente » Cod sursa (job #312998) | Cod sursa (job #1081881) | Cod sursa (job #2254971) | Cod sursa (job #2226674) | Cod sursa (job #2357615)
#include <iostream>
#include <cstring>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("bfs.in");
ofstream out("bfs.out");
const int N=1000002;
int d[N],n,m,xs;
queue <int> q;
vector <int> a[N];
void citire()
{
int x,y;
in>>n>>m>>xs;
for(int i=1;i<=m;i++)
{
in>>x>>y;
a[x].push_back(y);
}
}
void bfs(int xp)
{
memset(d,-1,sizeof(d));
q.push(xp);
d[xp]=0;
while(!q.empty())
{
int x=q.front();
q.pop();
for(auto y:a[x])
{
if(d[y]==-1)
{
q.push(y);
d[y]=1+d[x];
//pred[y]=x;
}
}
}
}
int main()
{
citire();
bfs(xs);
for(int i=1;i<=n;i++)
{
out<<d[i]<<' ';
}
return 0;
}