Pagini recente » Cod sursa (job #3336670) | Cod sursa (job #2007161) | Cod sursa (job #2152192) | Borderou de evaluare (job #2158881) | Cod sursa (job #2086901)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define dm 100005
#define x first
#define y second
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int n, m;
vector <int> graf[dm];
pair<int, int> aux;
queue <int> q;
int dist[dm], k;
void bfs(int start)
{
int x;
q.push(start);
x = start;
while(!q.empty()){
x = q.front();
q.pop();
for(int i = 0; i < graf[x].size(); i++)
if(graf[x][i] != x && !dist[graf[x][i]])
{
dist[graf[x][i]] = dist[x] + 1;
q.push(graf[x][i]);
}
}
}
int main()
{
fin >> n >> m >> k;
for(int i = 1; i <= m; i++)
{
fin >> aux.x >> aux.y;
if(aux.x != aux.y)
graf[aux.x].push_back(aux.y);
}
bfs(k);
for(int i = 1; i <= n; i++)
{
if(!dist[i] && i!=k)
fout << -1 << " ";
else if (i == k)
fout << 0 << " ";
else
fout << dist[i] << " ";
}
return 0;
}