Pagini recente » Cod sursa (job #1016682) | Cod sursa (job #306452) | Cod sursa (job #1436913) | Borderou de evaluare (job #128214) | Cod sursa (job #2676077)
#include <fstream>
#include <map>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
const int MAX_NODES_COUNT = 100013;
vector<int> adjList[MAX_NODES_COUNT];
queue<int> q;
int n, m, s, x, y;
int dist[MAX_NODES_COUNT];
int main()
{
fin >> n >> m >> s;
while (m--)
{
fin >> x >> y;
adjList[x].push_back(y);
}
q.push(s);
fill(dist, dist + n + 1, -1);
dist[s] = 0;
while (!q.empty())
{
int currNode = q.front();
q.pop();
for (int node : adjList[currNode])
{
if (dist[node] == -1)
{
dist[node] = dist[currNode] + 1;
q.push(node);
}
}
}
for (int i = 1; i <= n; ++i)
{
fout << dist[i] << " ";
}
fout.flush();
}