Pagini recente » Cod sursa (job #2363357) | Cod sursa (job #1099892) | Cod sursa (job #1062562) | Cod sursa (job #1635537) | Cod sursa (job #2706745)
#include <iostream>
#include<fstream>
#include<queue>
#include<vector>
using namespace std;
int n;
int d[100001];
ifstream in("bfs.in");
ofstream out("bfs.out");
queue<int> q;
vector <int> a[100001];
int main()
{
int 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++)
{
d[i]=-1;
}
q.push(s);
d[s]=0;
while(!q.empty())
{
int x=q.front();
q.pop();
for(auto y:a[x])
{
if(d[y]==-1)
{
q.push(y);
d[y]=1+d[x];
}
}
}
for (int i = 1; i <= n; i++)
{
out << d[i] << " ";
}
out.close();
return 0;
}