Pagini recente » Cod sursa (job #3297442) | Cod sursa (job #2788387) | Cod sursa (job #2655480) | Diferente pentru problema/harta intre reviziile 4 si 3 | Cod sursa (job #1759245)
#include <iostream>
#include <vector>
#include <fstream>
#include <queue>
using namespace std;
ifstream in("bfs.in");
ofstream out("bfs.out");
vector <int> graf[100001];
queue <int> Q;
int sol[100001];
int used[100001];
void bfs(int n,int s)
{
while(Q.empty()==0)
{
int x=Q.front();
cout<<x<<" ";
while(graf[x].empty()==0)
{
if(used[graf[x].back()]==0)
{
Q.push(graf[x].back());
used[graf[x].back()]=1;
sol[graf[x].back()]=sol[x]+1;
}
graf[x].pop_back();
}
Q.pop();
}
}
int main()
{
int n,m,s;
in>>n>>m>>s;
for(int i=1; i<=m; i++)
{
int x,y;
in>>x>>y;
graf[x].push_back(y);
}
Q.push(s);
used[s]=1;
sol[s]=1;
bfs(n,s);
for(int i=1;i<=n;i++)
{
out<<sol[i]-1<<" ";
}
}