Pagini recente » Cod sursa (job #2282408) | Rating Danut Avadanei (ziende) | Cod sursa (job #558235) | Istoria paginii runda/catdebunesti | Cod sursa (job #1691099)
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <fstream>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
int n,m,i,j,nr;
vector <vector <int> > graph(100000);
vector <bool> visited (100000,false);
vector <int> cost (100000,-1);
void bfs(int vertex)
{ if(vertex<0||vertex>n-1)
return;
queue <int> q;
int element;
q.push(vertex);
visited[vertex]=true;
cost[vertex]=0;
while(!q.empty())
{ element=q.front();
for(i=0;i<graph[element].size();i++)
if(!visited[graph[element][i]])
{ q.push(graph[element][i]);
//cout<<graph[element][i]+1<<" ";
cost[graph[element][i]]=cost[element]+1;
visited[graph[element][i]]=true;
}
q.pop();
}
}
int main()
{ int x,y,s;
f>>n>>m>>s;
for(i=0;i<m;i++)
{ f>>x>>y;
x--; y--;
graph[x].push_back(y);
}
bfs(s-1);
for(i=0;i<n;i++)
g<<cost[i]<<" ";
return 0;
}