Cod sursa(job #688696)

Utilizator ZexonAvramita Teodor Zexon Data 23 februarie 2012 19:17:57
Problema BFS - Parcurgere in latime Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.77 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;

ifstream in ("bfs.in");
ofstream out ("bfs.out");

int n, m, s, v[100001];
vector <int> a[100001];
queue <int> q;

void citire () {
    in >> n >> m >> s;
    int x,y;
    for (int i = 1; i <= m; ++i) {
        in >> x >> y;
        a[x].push_back(y);
    }
}


int main()
{
    int x,y;
    citire();
    for (int i = 0;i<=100001;i++)
        v[i]=-1;
    q.push(s);
    v[s]=0;
    while(!q.empty())
    {
        x=q.front();
        q.pop();
        for(size_t i=0; i<a[x].size();i++)
        {
            y=a[x][i];
            if(v[y]==-1)
            {
                q.push(y);
                v[y]=1+v[x];
            }
        }
    }
    return 0;
}