Pagini recente » Profil giagia | Cod sursa (job #1321345) | Cod sursa (job #674998) | Cod sursa (job #3296589) | Cod sursa (job #2016800)
#include <iostream>
#include <vector>
#include <fstream>
#include <queue>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
const int NodMax = 100005;
vector < int > v[NodMax];
queue < int > Q;
int n,m,s;
int dist[NodMax];
void citire ()
{
fin>>n>>m>>s;
for (int i=1; i<=m; ++i)
{
int a,b;
fin>>a>>b;
v[a].push_back(b);
}
}
void distanta ()
{
Q.push(s);
dist[s]=1;
while (!Q.empty())
{
int nod;
nod=Q.front();
vector < int > ::iterator it;
for (it=v[nod].begin(); it!=v[nod].end(); ++it)
if (!dist[*it])
{
Q.push(*it);
dist[*it]=dist[nod]+1;
}
Q.pop();
}
}
void afishare()
{
for (int i=1;i<=n; i++)
if (!dist[i]) fout<<"-1 ";
else fout<<dist[i]-1<<" ";
}
int main()
{
// for(int i=1; i<=100; ++i)
// {
// int x;
// fin >> x;
// v[i].push_back(x);
// }
//
// vector < int > ::iterator it;
// for(it=v[i].begin(); it!=v[i].end(); ++it)
// {
// cout << *it;
// }
citire();
distanta();
afishare();
return 0;
}