Pagini recente » Cod sursa (job #2040254) | Cod sursa (job #2034846) | Cod sursa (job #2152844) | Cod sursa (job #410874) | Cod sursa (job #3165864)
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <fstream>
#include <map>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
vector<int> graf[100001];
int n, m, s;
map<int, bool> visited;
queue<pair<int, int>> q;
int bfs(int nod)
{
q.push({s, 0});
visited[s] = true;
int cNode, pas;
while (!q.empty())
{
cNode = q.front().first;
pas = q.front().second + 1;
if (cNode == nod) return q.front().second;
for (int i = 0; i < graf[cNode].size(); i++)
{
if (!visited.count(graf[cNode][i]))
{
visited[graf[cNode][i]] = true;
q.push({graf[cNode][i], pas});
}
}
q.pop();
}
return -1;
}
int main()
{
fin >> n >> m >> s;
int x, y;
for (int i = 0; i < m; i++)
{
fin >> x >> y;
graf[x].push_back(y);
}
queue<pair<int, int>> emptyQ;
map<int, bool> emptyM;
for (int i = 1; i <= n; i++)
{
fout << bfs(i) << ' ';
q = emptyQ;
visited = emptyM;
}
return 0;
}