Pagini recente » Cod sursa (job #672629) | Cod sursa (job #1699381) | Cod sursa (job #2502374) | Cod sursa (job #1455530) | Cod sursa (job #2487587)
#include <bits/stdc++.h>
#define NMAX 100001
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int dist[NMAX];
vector<int> graph[NMAX];
queue<int> q;
int main()
{
int n,m,s;
fin >> n >> m >> s;
while(m--)
{
int x, y;
fin >> x >> y;
graph[x].push_back(y);
}
for(int i = 1; i <= n; i++) dist[i] = -1;
q.push(s);
dist[s] = 0;
while(!q.empty())
{
int node = q.front();
q.pop();
for(auto x : graph[node])
{
if(dist[x] == -1)
{
dist[x] = dist[node]+1;
q.push(x);
}
}
}
for(int i = 1; i <= n; i++) fout << dist[i] << ' ';
fin.close();
fout.close();
return 0;
}