Cod sursa(job #929750)

Utilizator paunmatei7FMI Paun Matei paunmatei7 Data 27 martie 2013 11:01:56
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.87 kb
#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
using namespace std;
queue <int> q;
vector <int> v[100001];
int viz[100001] , n , m , pozi , x , y;
void bfs(int x)
{
    memset(viz , -1 , sizeof(viz));
    viz[x]=0;
    q.push(x);
    while(! q.empty())
    {
        int temp=q.front();
        for(int i=0 ; i<v[temp].size() ; ++i)
            if(viz[v[temp][i]] == -1)
            {
                viz[v[temp][i]]=viz[temp]+1;
                q.push(v[temp][i]);
            }
        q.pop();
    }
}
int main()
{
    freopen("bfs.in" , "r" , stdin);
    freopen("bfs.out" , "w" , stdout);
    scanf("%d %d %d" , &n , &m , &pozi);
    for ( ; m>0 ; --m)
    {
        scanf("%d %d" , &x , &y);
        v[x].push_back(y);
    }
    bfs(pozi);
    for(int i=1 ; i<=n ; ++i)
        printf("%d " , viz[i]);
    printf("\n");
}