Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. ifstream f("date.in");
  5. ofstream g("date.out");
  6.  
  7. vector<int>*mat;
  8. unsigned int n,m;
  9.  
  10. void Citire()
  11. {
  12.     f>>n>>m;
  13.     for(int i=1;i<=m;i++)
  14.     {
  15.         int x,y;
  16.         f>>x>>y;
  17.         mat[x].push_back(y);
  18.     }
  19. }
  20.  
  21. void Bfs(int k)
  22. {
  23.     bool *seen = new bool[n];
  24.     for(int i=0;i<=n;i++)
  25.         seen=false;
  26.        
  27.     queue<int>coada;
  28.     vector<int>::iterator index;
  29.    
  30.     coada.push(k);seen[k]=true;
  31.  
  32.     while(!coada.empty())
  33.     {
  34.         int nod_curent=coada.front();
  35.         cout<<nod_curent<<" ";
  36.         coada.pop();
  37.  
  38.         for(index=mat[nod_curent].begin();index!=mat[nod_curent].begin();index++)
  39.             if(!seen[*index])
  40.             {
  41.                 seen[*index]=true;
  42.                 coada.push(*index);
  43.             }
  44.     }
  45. }
  46.  
  47. int main()
  48. {
  49.     Citire();
  50.     Bfs(1);
  51.  
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement