Cod sursa(job #1778461)

Utilizator al_k_ponyClaudiu Babin al_k_pony Data 13 octombrie 2016 20:16:30
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define end; "\n"
#define maxn 100005
using namespace std;
vector <int>A[maxn];
int n,m,s,L,S[maxn],Cost[maxn];

void BFS(int nod)
{
    memset(Cost, -1, sizeof(Cost));
    L = 1;
    Cost[nod] = 0;
    S[L] = nod;
    for(int i = 1;i<=L;i++)
        for(int j = 0;j < A[S[i]].size();j++)
            if(Cost[A[S[i]][j]] == -1)
                {
                    S[++L] = A[S[i]][j];
                    Cost[S[L]] = Cost[S[i]] + 1;
                }
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    //
    ifstream fin("bfs.in");
    ofstream fout("bfs.out");
    fin >> n >> m >> s;
    int x,y;
    for(int i = 1;i<=m;i++)
    {
        fin >> x >> y;
        A[x].push_back(y);
    }
    BFS(s);
    for(int i = 1;i<=n;i++) fout << Cost[i] << " ";
    fin.close();
    fout.close();
    return 0;
}