Cod sursa(job #3214847)

Utilizator SerbanCaroleSerban Carole SerbanCarole Data 14 martie 2024 15:02:54
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
using pii = pair<int,int>;
const int nmax = 1e5 + 1;
ifstream cin("bfs.in");
ofstream cout("bfs.out");
vector <int> g[nmax];
int n , m , s , x , y , dp[nmax];
signed main()
{
    queue<int>q;
    cin >> n >> m >> s;
    for(int i = 1 ; i <= m ; ++i)
    {
        cin  >> x >> y;
        g[x].push_back(y);
    }
    q.push(s);
    dp[s] = 1;
    while(!q.empty())
    {
        x = q.front(); q.pop();
        for(auto it : g[x])
        {
            if(!dp[it])
            {
                dp[it] = dp[x] + 1;
                q.push(it);
            }
        }
    }
    for(int i = 1 ; i <= n ; ++i)
    {
        cout << dp[i] - 1 << ' ';
    }
    return 0;
}