Pagini recente » Cod sursa (job #741877) | Cod sursa (job #2270649)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
unsigned N, M;
vector<unsigned> G[100003];
unsigned vz[100003], sol[100003];
queue<unsigned> q;
void BFS(unsigned node)
{
unsigned nod, sz, i;
vz[node]=1;
q.push(node);
while(q.empty()==0)
{
nod=q.front();
sz=G[nod].size();
for(i=0; i<sz; i++)
if(vz[G[nod][i]]==0)
{
q.push(G[nod][i]);
sol[G[nod][i]]=sol[nod]+1;
vz[G[nod][i]]=1;
}
q.pop();
}
}
int main()
{
unsigned i, x, y, s;
ifstream f("bfs.in");
ofstream fout("bfs.out");
f>>N>>M>>s;
for(i=0; i<M; i++)
{
f>>x>>y;
G[x].push_back(y);
}
BFS(s);
for(i=1; i<=N; i++)
if(vz[i]==0)
fout<<-1<<" ";
else
fout<<sol[i]<<" ";
}