Pagini recente » Cod sursa (job #537170) | Cod sursa (job #2771448) | Cod sursa (job #2315122) | Cod sursa (job #360804) | Cod sursa (job #1324034)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
vector <int> v[100001];
queue <int> q;
int cost[100001];
int s, n, m, x ,y;
ifstream in("bfs.in");
ofstream out("bfs.out");
void bfs()
{
cost[s]=0;
q.push(s);
while(!q.empty())
{
int nod;
nod = q.front();
for(int k=0; k<v[nod].size(); k++)
{
if(cost[v[nod][k]]==-1)
{
cost[v[nod][k]]=cost[nod]+1;
q.push(v[nod][k]);
}
}
q.pop();
}
}
int main()
{
in>>n>>m>>s;
for(int i = 0; i<m; i++)
{
in>>x>>y;
v[x].push_back(y);
}
for(int i = 1; i<=n; i++)
cost[i]=-1;
bfs();
for(int i = 1; i<=n; i++)
out<<cost[i]<<" ";
return 0;
}