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