Pagini recente » Cod sursa (job #2645917) | Cod sursa (job #1646465) | Cod sursa (job #2557356) | Cod sursa (job #2978474) | Cod sursa (job #3147841)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream cin("bfs.in");
ofstream cout("bfsout");
int n,m,s;
struct per
{
vector <int> dir;
}v[100001];
vector <int> d;
vector <bool> visited;
int abx(int x)
{
queue <int > q;
q.push(x);
d[x]=1;
while(!q.empty())
{
int cur=q.front();
q.pop();
for(int i=1;i<=v[cur].dir[0];i++)
{
if(!visited[v[cur].dir[i]])
{
visited[v[cur].dir[i]]=1;
q.push(v[cur].dir[i]);
d[v[cur].dir[i]]=d[cur]+1;
}
}
}
return 0;
}
int main()
{
cin>>n>>m>>s;
for(int i=1;i<=n;i++)
{
v[i].dir.push_back(0);
visited.push_back(0);
d.push_back(0);
}
visited.push_back(0);
d.push_back(0);
for(int i=1;i<=m;i++)
{
int x,y;
cin>>x>>y;
if(x!=y)
{
v[x].dir[0]++;
v[x].dir.push_back(y);
}
}
visited[s]=1;
abx(s);
for(int i=1;i<=n;i++)
{
if(d[i]==0)
{
cout<<-1<<" ";
continue;
}
cout<<d[i]-1<<" ";
}
return 0;
}