Pagini recente » Statistici Ahmet Gedemenli (gedemenli) | Cod sursa (job #2298117) | Cod sursa (job #2793019) | Istoria paginii runda/tl2 | Cod sursa (job #2290265)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
#define nmax 1000005
vector<int> V[nmax];
int n, m, s, viz[nmax];
void citire()
{
ifstream f ("bfs.in");
f>>n>>m>>s;
for(int i=0; i<m; i++)
{
int x, y;
f>>x>>y;
V[x].push_back(y);
}
}
void bfs()
{
queue<int> q;
q.push(s);
viz[s]=1;
while(!q.empty())
{
for(auto x:V[q.front()])
if(!viz[x])
{
q.push(x);
viz[x]=viz[q.front()]+1;
}
q.pop();
}
}
void afisare()
{
ofstream g("bfs.out");
for(int i=1; i<=n; i++)
g<<viz[i]-1<<' ';
}
int main()
{
citire();
bfs();
afisare();
return 0;
}