Pagini recente » Cod sursa (job #802597) | Cod sursa (job #2850405) | Cod sursa (job #780718) | Cod sursa (job #592587) | Cod sursa (job #2000578)
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
ifstream in("bfs.in");
ofstream out("bfs.out");
vector <int> a[100005];
queue <int> c;
int d[100005];
int main()
{
int n, m, k;
in >> n >> m >> k;
for(int i = 1; i <= m; ++i)
{
int x, y;
in >> x >> y;
a[x].push_back(y);
}
d[k] = 1;
c.push(k);
while(!c.empty())
{
int x = c.front();
for(int i = 0; i < a[x].size(); ++i)
if(d[a[x][i]] == 0)
{
d[a[x][i]] = d[x] + 1;
c.push(a[x][i]);
}
c.pop();
}
for(int i = 1; i <= n; ++i)
out << d[i] - 1 << ' ';
return 0;
}