Cod sursa(job #3243752)

Utilizator FlaviuuuFlaviu Flaviuuu Data 20 septembrie 2024 19:25:44
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.15 kb
#include <fstream>
#include <vector>
#include <map>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
using namespace std;
ifstream cin("bfs.in");
ofstream cout("bfs.out");
#define ll long long
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
int n, m, s, xx, y;
vector<vector<int>> v;
int fr[100001];
void parc()
{
    queue<pair<int, int>> q;
    q.push({s, 1});
    while(!q.empty())
    {
        pair<int, int> t = q.front();
        q.pop();
        for(auto &nod:v[t.first])
        {
            if(fr[nod] == 0)
            {
                q.push({nod, t.second + 1});
                fr[nod] = t.second + 1;
            }
        }
    }
    for(int i = 1; i <= n; i++)
    {
        if(i == s)
            cout<<"0"<<" ";
        else
            cout<<fr[i] - 1<<" ";
    }
}
int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    ///ll t; cin>>t; while(t--) solve(), cout<<'\n';
    cin>>n>>m>>s;
    v.resize(n + 5);
    for(int i = 1; i <= m; i++)
    {
        cin>>xx>>y;
        v[xx].push_back(y);
    }
    parc();
    return 0;
}