Cod sursa(job #3241272)

Utilizator Tudor.1234Holota Tudor Matei Tudor.1234 Data 28 august 2024 14:21:06
Problema BFS - Parcurgere in latime Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.42 kb
#include "bits/stdc++.h"
std :: vector < std :: set < int > > adj;
std :: vector < int > ans;
std :: bitset < 100005 > vis;
int n, m, target;
inline static void AddEdge(int x, int y){
            adj[x].insert(y);
}
inline static void BFS(int target){
             std :: queue < int > q;
             ans[target] = 0;
             q.push(target);
             while(q.empty() == false){
                      int node = q.front();
                      q.pop();
                      for(auto i : adj[node]){
                                if(ans[i] == -1){
                                        vis[i] = true;
                                        ans[i] = ans[node] + 1;
                                        q.push(i);
                                }
                      }
             }
}
inline static void Solve(){
            std :: cin >> n >> m >> target;
            adj.resize(n + 1);
            ans.resize(n + 1, - 1);
            while(m -- ){
                  int x, y;
                  std :: cin >> x >> y;
                  AddEdge(x, y);
            }
             BFS(target);
            for(int i = 1; i < ans.size(); i++){
                      std :: cout << ans[i] << ' ';
             }
}
signed main(){
      freopen("bfs.in", "r", stdin);
      freopen("bfs.out", "w", stdout);
      std :: ios_base :: sync_with_stdio(false);
      std :: cin.tie(0);
      std :: cout.tie(0);
      Solve();
      return 0;
}