Pagini recente » Cod sursa (job #851972) | Cod sursa (job #1038958) | Cod sursa (job #272523) | Cod sursa (job #727960) | Cod sursa (job #3234718)
#include <bits/stdc++.h>
using namespace std;
ifstream in("bfs.in");
ofstream out("bfs.out");
int n, m, sursa;
const int nmax = 1e6;
vector <int> graph[nmax+1];
int dist[nmax+1];
void bfs(int sursa)
{
queue <int> q;
q.push(sursa);
dist[sursa] = 1;
while(!q.empty())
{
int nod = q.front();
q.pop();
for(auto i:graph[nod])
{
if(dist[i] == 0)
{
dist[i] = dist[nod] + 1;
q.push(i);
}
}
}
}
int main()
{
int x, y;
in>>n>>m>>sursa;
for(int i = 1; i <= m; i++)
{
in>>x>>y;
graph[x].push_back(y);
}
bfs(sursa);
for(int i = 1; i <= n; i++)
{
out<<dist[i] - 1<<" ";
}
}