Pagini recente » Cod sursa (job #1356189) | Cod sursa (job #2792690) | Cod sursa (job #1702758) | Cod sursa (job #489488) | Cod sursa (job #2782987)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("bfs.in");
ofstream out("bfs.out");
const int N = 100005;
const int INF = -1;
int n;
int d[N];
queue<int> q;
vector<int> a[N];
void bfs(int s)
{
for(int i=1; i<=n;i++)
d[i]=INF;
q.push(s);
d[s]=0;
while(!q.empty())
{
int x=q.front();
q.pop();
for(auto y: a[x])
{
if(d[y]==INF)
{
q.push(y);
d[y]=1+d[x];
}
}
}
}
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);
}
bfs(s);
for(int i=1; i<=n; i++)
out<<d[i]<<' ';
in.close();
out.close();
return 0;
}