Cod sursa(job #1363292)

Utilizator viuscenkoViktor Iuscenko viuscenko Data 26 februarie 2015 21:02:04
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.52 kb
#include <bits/stdc++.h>

using namespace std;

#define     mp              make_pair
#define     fs              first
#define     sc              second
#define     pob             pop_back
#define     pub             push_back
#define     eps             1E-7
#define     sz(a)           a.size()
#define     count_one       __builtin_popcount;
#define     count_onell     __builtin_popcountll;
#define     fastIO          ios_base::sync_with_stdio(false)
#define     PI              (acos(-1.0))
#define     linf            (1LL<<62)//>4e18
#define     inf             (0x7f7f7f7f)//>2e9

#define DEBUG 1
#ifdef DEBUG
#define D(x) x
#else
#define D(x)
#endif

#define MAXN 100000
#define MAXM 1000000

FILE *in = fopen("bfs.in", "r");
FILE *out = fopen("bfs.out", "w");

int n, m, s;
int rez[MAXN + 1];
vector<int> vec[MAXN+1];
queue<int> Q;

void bfs(int nod) {
    Q.push(nod);
    while(!Q.empty()) {
        nod = Q.front(); Q.pop();
        for(vector<int>::iterator it = vec[nod].begin(); it != vec[nod].end(); it++) {
            if(rez[*it] == -1) {
                Q.push(*it);
                rez[*it] = rez[nod] + 1;
            }
        }
    }
}

int main()
{
    int x, y;

    fscanf(in, "%d%d%d", &n, &m, &s);

    while(m--) {
        fscanf(in, "%d%d", &x, &y);
        vec[x].pub(y);
    }

    for(int i = 1; i <= n; ++i)
        rez[i] = -1;

    rez[s] = 0;

    bfs(s);

    for(int i = 1; i <= n; ++i)
        fprintf(out, "%d ", rez[i]);

    return 0;
}