Cod sursa(job #1188684)

Utilizator EhtRalpmetFMI Ardei Claudiu-Alexandru EhtRalpmet Data 20 mai 2014 11:49:23
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include<fstream>
//#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
ifstream  cin("bfs.in");
ofstream cout("bfs.out");
  
queue<pair<int,int> > q;
int i,j,k,n,m,a,b,nr,s,infi=1000000,val[100001];
vector<int> v[100001];
int bl[100001];
pair<int,int> x;
  
int main(){
    cin>>n>>m>>s;
    for(i=1;i<=m;i++){
        cin>>a>>b;
        v[a].push_back(b);
    }
      
    for(j=1;j<=n;j++){
        val[j]=-1;
    }
    q.push(make_pair(s,0));
    while(!q.empty()){
        x=q.front();
        q.pop();
        if(bl[x.first]==0){
            val[x.first]=x.second;
            bl[x.first]=1;
        }else{
            continue;
        }
        for(k=0;k<v[x.first].size();k++){
            if(bl[v[x.first][k]]==0){
                q.push(make_pair(v[x.first][k],x.second+1));
            }
        }
    }
    for(i=1;i<=n;i++){
        cout<<val[i]<<' ';
    }
      
    return 0;
}