Pagini recente » Cod sursa (job #860857) | Cod sursa (job #2028759) | Cod sursa (job #966563) | Cod sursa (job #1526054) | Cod sursa (job #2186346)
#include <iostream>
#include <fstream>
#include <vector>
#include <list>
#include <cstring>
#include <algorithm>
#define MAXN 100005
#define MAX 10000000
using namespace std;
ifstream in("bfs.in");
ofstream out("bfs.out");
vector<int>v[MAXN];
bool viz[MAXN];
list<int>stiva;
int n,m,sursa,dp[MAXN];
void bfs(int sursa){
memset(dp,MAX,sizeof(dp));
dp[sursa] = 0;
viz[sursa] = true;
stiva.push_front(sursa);
while(!stiva.empty()){
int nod = stiva.front();
stiva.pop_front();
int curent;
for(int i = 0; i < v[nod].size(); i++){
curent = v[nod][i];
if(!viz[curent]){
stiva.push_back(curent);
dp[curent] = min(dp[curent],dp[nod]+1);
viz[curent] = true;
}
}
}
}
int main()
{
in>>n>>m>>sursa;
int x,y;
for(int i = 1; i <= m; i++){
in>>x>>y;
v[x].push_back(y);
}
bfs(sursa);
for(int i = 1; i <= n; i++)
if(dp[i] == MAX)
dp[i] = -1;
for(int i = 1; i <= n; i++)
out<<dp[i]<<" ";
return 0;
}