You are on page 1of 2

// bfs.cpp : Defines the entry point for the console application.

//breadth first

//#include "stdafx.h" #include<iostream> #include<fstream> #include<queue> #include<vector>

using namespace std; ifstream f("bfs.in"); ofstream g("bfs.out"); int i, nod, m, n, s, x, y; vector <int> A[100001], viz(100001, -1); void BFS(int nod) { queue <int> Q; Q.push(nod); viz[nod] = 0; while (Q.empty() == false) { for (int i = 0; i < A[Q.front()].size(); i++) if (viz[A[Q.front()][i]] == -1) { Q.push(A[Q.front()][i]); viz[A[Q.front()][i]] = viz[Q.front()] + 1;

} Q.pop(); } }

int main() { f >> n; f >> m; f >> s; for (i = 1; i <= m; i++) { f >> x >> y; A[x].push_back(y); } BFS(s); for (i = 1; i <= n; i++) g << viz[i] << " "; return 0; }