Pagini recente » Borderou de evaluare (job #2584377) | Borderou de evaluare (job #488979) | Borderou de evaluare (job #1277106) | Borderou de evaluare (job #534061) | Cod sursa (job #668062)
Cod sursa(job #668062)
#include<iostream>
#include<fstream>
#include<vector>
#include<queue>
#define DN 100005
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int viz[DN], m, n, s;
queue <int> c;
vector <int> gr[DN];
int main()
{
int i, a,b;
fin>>n>>m>>s;
for(i=1;i<=m;i++)
{
fin>>a>>b;
gr[a].push_back(b);
}
for(i=1;i<=n;i++)
viz[i]=-1;
viz[s]=0;
for(c.push(s);c.size();c.pop())
{
int y=c.front();
for(i=0;i<gr[y].size();i++)
{
int fiu=gr[y][i];
if(-1==viz[fiu])
{
viz[fiu]=viz[y]+1;
c.push(fiu);
}
}
}
for(i=1;i<=n;i++)
fout<<viz[i]<<" ";
}