Cod sursa(job #1184581)

Utilizator EhtRalpmetFMI Ardei Claudiu-Alexandru EhtRalpmet Data 13 mai 2014 12:00:28
Problema BFS - Parcurgere in latime Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.87 kb
#include<fstream>
#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;
vector<int> v[100001];
int val[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);
    }
    bool bl=0;
    
    for(j=1;j<=n;j++){
        val[j]=-1;
    }
    q.push(make_pair(s,0));
    while(!q.empty()){
        x=q.front();
        q.pop();
        if(val[x.first]==-1){
            val[x.first]=x.second;
        }
        for(k=0;k<v[x.first].size();k++){
            if(val[v[x.first][k]]==-1){
                q.push(make_pair(v[x.first][k],x.second+1));
            }
        }
    }
    for(i=1;i<=n;i++){
        cout<<val[i]<<' ';
    }
    
    return 0;
}