Pagini recente » Cod sursa (job #2600755) | Monitorul de evaluare | Cod sursa (job #1549265) | Cod sursa (job #1892334) | Cod sursa (job #3253913)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const string file_name = "bfs";
ifstream fin(file_name + ".in");
ofstream fout(file_name + ".out");
int main()
{
int n, m, s;
fin >> n >> m >> s;
vector<vector<int>> graph(n + 1);
for(int e = 1; e <= m; e++)
{
int x, y;
fin >> x >> y;
graph[x].push_back(y);
}
vector<int> dist(n + 1, -1);
vector<int> visited(n + 1, 0);
dist[s] = 0;
visited[s] = 1;
queue<int> q;
q.push(s);
while(!q.empty())
{
int node = q.front();
q.pop();
for(int neigh: graph[node]) {
if(!visited[neigh]) {
visited[neigh];
dist[neigh] = dist[node] + 1;
q.push(neigh);
}
}
}
for(int i = 1; i <= dist.size() - 1; i++) {
fout << dist[i] << ' ';
}
return 0;
}