Pagini recente » Cod sursa (job #2519051) | Cod sursa (job #1606458) | Cod sursa (job #2484382) | Cod sursa (job #2319803) | Cod sursa (job #2783599)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("bfs.in");
ofstream out("bfs.out");
const int N=1e5+1;
int cost[N];
vector<int> a[N];
queue<int> q;
int main()
{
int n,m,s;
in>>n>>m>>s;
for(int i=1; i<=m; i++)
{
int x,y;
in>>x>>y;
a[x].push_back(y);
}
for(int i=1; i<=n; i++)
{
cost[i]=N;
}
cost[s]=0;
q.push(s);
while(!q.empty())
{
int x=q.front();
q.pop();
for(auto y: a[x])
{
if(cost[y]==N)
{
cost[y]=cost[x]+1;
q.push(y);
}
}
}
for(int i=1; i<=n; i++)
{
if(cost[i]==N) out<<-1<<" ";
else out<<cost[i]<<" ";
}
return 0;
}