Pagini recente » Cod sursa (job #3121565) | Cod sursa (job #2298377)
#include <bits/stdc++.h>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
vector <int>v[100005];
queue<int>q;
int n,m,s,x,y,d[100005],viz[100005];
void bfs(int x)
{
int i,j;
q.push(x);
for(i=1;i<=n;i++)d[i]=-1;
d[x]=0;
viz[x]=1;
while(!q.empty())
{
int nod=q.front();
q.pop();
for(i=0;i<v[nod].size();i++)
{
if(viz[v[nod][i]]==0)
{
q.push(v[nod][i]);
d[v[nod][i]]=d[nod]+1;
viz[v[nod][i]]=1;
}
}
}
}
int i;
int main()
{
f>>n>>m>>s;
for(i=1;i<=m;i++)
{
f>>x>>y;
v[x].push_back(y);
}
bfs(s);
for(i=1;i<=n;i++)g<<d[i]<<" ";
return 0;
}