Pagini recente » Cod sursa (job #1272684) | Cod sursa (job #1524743) | Cod sursa (job #2740365) | Cod sursa (job #2952068) | Cod sursa (job #2032952)
#include <fstream>
#include <vector>
#include <queue>
#define N 100005
using namespace std;
ifstream fin ("bfs.in");
ofstream fout("bfs.out");
vector <int> v[N];
queue <int> q;
int dist[N];
bool f[N];
void BFS(int x)
{
vector <int>::iterator it;
f[x] = 1;
dist[x] = 0;
q.push(x);
while(!q.empty())
{
for(it = v[q.front()].begin(); it != v[q.front()].end(); ++it)
if(!f[*it])
{
f[*it] = 1;
dist[*it] = dist[q.front()] + 1;
q.push(*it);
}
q.pop();
}
}
int main()
{
int n, m, s, x, y;
fin >> n >> m >> s;
while(m--)
{
fin >> x >> y;
v[x].push_back(y);
}
fin.close();
for(int i = 1; i <= n; ++i) dist[i] = -1;
BFS(s);
for(int i = 1; i <= n; ++i) fout << dist[i] << " ";
fout.close();
return 0;
}