Pagini recente » Cod sursa (job #287132) | Cod sursa (job #3231994) | Cod sursa (job #2909011) | Cod sursa (job #852410) | Cod sursa (job #3218962)
#include <bits/stdc++.h>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
int n,m,k,x,y;
vector<int>dist;
vector<vector<int>>graph;
void bfs(int node)
{
queue<int>q;
q.push(node);
dist[node]=1;
while(!q.empty())
{
node=q.front();
q.pop();
for(int next:graph[node])
if(dist[next]==0)
{
dist[next]=dist[node]+1;
q.push(next);
}
}
}
int main()
{
f>>n>>m>>k;
dist.assign(n+1,0);
graph.assign(n+1,vector<int>());
for(int i=1; i<=m; i++)
{
f>>x>>y;
graph[x].push_back(y);
}
bfs(k);
for(int i=1; i<=n; i++)
g<<dist[i]-1<<' ';
return 0;
}