Pagini recente » Cod sursa (job #3202014) | Cod sursa (job #1857781) | Cod sursa (job #124272) | Cod sursa (job #2656496) | Cod sursa (job #3182713)
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
vector<int> graph[100001];
int dist[100001];
int main()
{
int n,k,s;
fin >> n >> k >> s;
while(k--)
{
int x,y;
fin >> x >> y;
graph[x].push_back(y);
}
queue<int> Q;
Q.push(s);
dist[s]=1;
while(!Q.empty())
{
int x = Q.front();
Q.pop();
for(int child : graph[x])
{
if(!dist[child])
{
dist[child]=dist[x]+1;
Q.push(child);
}
}
}
for(int i=1;i<=n;i++)
{
fout << dist[i]-1 << " ";
}
}