Pagini recente » Cod sursa (job #926778) | Cod sursa (job #2849623) | Cod sursa (job #821505) | Cod sursa (job #2606351) | Cod sursa (job #1917017)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int n, m ,s;
vector<int> adjList[100007];
int dist[100007];
void read()
{
int x,y;
fin >> n >> m >> s;
for(int i = 1; i <= m; i++)
{
fin >> x >> y;
adjList[x].push_back(y);
}
for(int i = 1; i <= n; i++)
{
dist[i] = -1;
}
}
void BFS()
{
queue<int> q;
dist[s] = 0;
q.push(s);
while(!q.empty())
{
int v = q.front();
q.pop();
int g = adjList[v].size();
for(int i = 0; i < g;i++)
{
if(dist[adjList[v][i]] == -1)
{
dist[adjList[v][i]] = dist[v] + 1;
q.push(adjList[v][i]);
}
}
}
}
int main()
{
read();
BFS();
for(int i = 1;i <= n; i++)
{
fout << dist[i] << " ";
}
return 0;
}