Pagini recente » Cod sursa (job #3151058) | Cod sursa (job #1824891) | Cod sursa (job #1992298) | Cod sursa (job #2355808) | Cod sursa (job #467632)
Cod sursa(job #467632)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
const char iname[] = "bfs.in";
const char oname[] = "bfs.out";
const int nmax = 100010;
ifstream fin(iname);
ofstream fout(oname);
vector<int> Node[nmax];
int N, M, i, j, Initial, x, y;
int Size[nmax], Cost[nmax];
int Queue[nmax];
void BFS(int nod)
{
memset(Cost, -1, sizeof(Cost));
int pas = 1;
Queue[pas] = nod;
Cost[nod] = 0;
for(i = 1; i <= pas; i ++)
for(j = 0; j < Node[Queue[i]].size(); j ++)
{
if(Cost[Node[Queue[i]][j]] == - 1)
{
Queue[++pas] = Node[Queue[i]][j];
Cost[Queue[pas]] = Cost[Queue[i]] + 1;
}
}
}
int main()
{
fin >> N >> M >> Initial;
for(i = 1; i <= M; i ++)
{
fin >> x >> y;
Node[x].push_back(y);
}
for(i = 1; i <= N; i ++)
Size[i] = Node[i].size();
BFS(Initial);
for(i = 1; i <= N; i ++)
fout << Cost[i] << " ";
return 0;
}