Pagini recente » Cod sursa (job #1229730) | Cod sursa (job #1039930) | Cod sursa (job #244919) | Cod sursa (job #595417) | Cod sursa (job #2170626)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
const int M = 10e6;
const int N = 10e5;
ifstream fcin("bfs.in");
ofstream fcout("bfs.out");
int main()
{
int n, m, s;
fcin >> n >> m >> s;
vector<int> V[M];
int x, y;
for (int i = 0; i < m; ++i)
{
fcin >> x >> y;
V[x].push_back(y);
}
bool visited[N];
for (int i = 1; i <= n; ++i)
visited[i] = false;
visited[s] = true;
int tav[M];
for (int i = 1; i <= n; ++i)
tav[i] = -1;
tav[s] = 0;
queue<int> q;
q.push(s);
while (!q.empty())
{
int ss = q.front();
q.pop();
for (unsigned int i = 0; i < V[ss].size(); ++i)
if (!visited[V[ss][i]])
{
visited[V[ss][i]] = true;
q.push(V[ss][i]);
tav[V[ss][i]] = tav[ss] + 1;
}
}
for (int i = 1; i <= n; ++i)
fcout << tav[i] << ' ';
}