Pagini recente » Cod sursa (job #882059) | Cod sursa (job #1918962) | Cod sursa (job #2529055) | Cod sursa (job #1833483) | Cod sursa (job #2339510)
#include <fstream>
#include <bitset>
#include <algorithm>
#include <queue>
using namespace std;
const int NMAX = 100005;
const int INF = 2000000000;
int n, m, s;
queue <int> q;
bitset <NMAX> viz;
int dist[NMAX];
vector <int> graph[NMAX];
void BFS()
{
q.push(s);
viz[s] = 1;
while (!q.empty())
{
int x = q.front();
q.pop();
for (auto i : graph[x])
if (viz[i] == 0)
{
dist[i] = dist[x] + 1;
viz[i] = 1;
q.push(i);
}
}
}
int main()
{
ifstream fin("bfs.in");
ofstream fout("bfs.out");
fin >> n >> m >> s;
for (int i = 1;i <= m;++i)
{
int x, y;
fin >> x >> y;
graph[x].push_back(y);
}
BFS();
for (int i = 1;i <= n;++i)
if (viz[i] == 0)
fout << -1 << " ";
else
fout << dist[i] << " ";
fin.close();
fout.close();
return 0;
}