Pagini recente » Cod sursa (job #394584) | Cod sursa (job #1309625) | Cod sursa (job #1197592) | Cod sursa (job #1529621) | Cod sursa (job #2639540)
#include <bits/stdc++.h>
using namespace std;
ifstream f ("bfs.in");
ofstream g ("bfs.out");
vector <int> graf[100005];
bool visited[100005];
int distances[100005];
queue <int> q;
void bfs(int node)
{
visited[node] = true;
q.push(node);
while(!q.empty())
{
int x=q.front();
q.pop();
for(auto y : graf[x])
if(!visited[y])
{
visited[y]=true;
distances[y]=distances[x]+1;
q.push(y);
}
}
}
int main()
{
int n, m, s;
f >> n >> m >>s;
while(m--)
{
int x, y;
f >> x >> y;
graf[x].push_back(y);
}
bfs(s);
for(int i=1; i<=n; ++i)
if(!visited[i])
g<<-1<<" ";
else
g<<distances[i]<<" ";
return 0;
}