Pagini recente » Monitorul de evaluare | Monitorul de evaluare | Cod sursa (job #2962071) | Cod sursa (job #2298706) | Cod sursa (job #2149131)
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
const int NMAX = 100005;
vector <int> A[NMAX];
int cost[NMAX];
int n,m,start;
void Read();
void BFS(int start);
int main()
{
Read();
memset(cost, -1, sizeof cost);
BFS(start);
for(int i = 1; i <= n; i++)
fout << cost[i] << " ";
return 0;
}
void Read()
{
fin >> n >> m >> start;
for(int i = 1; i <= m; i++)
{
int x,y;
fin >> x >> y;
A[x].push_back(y);
}
}
void BFS(int start)
{
queue <int> coada;
cost[start] = 0;
coada.push(start);
while(!coada.empty())
{
int aux = coada.front();
for(vector<int>::iterator it = A[aux].begin(); it != A[aux].end(); it++)
if(cost[*it] == -1)
{
coada.push(*it);
cost[*it] = cost[aux] + 1;
}
coada.pop();
}
}