Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. ifstream fin("arbore.in");
  5. ofstream fout("arbore.out");
  6. int n,m,j,i,viz[101],a[101][101],c[101],x,niv[101],t[101];
  7. void citire()
  8. {int i,j;
  9. fin>>n>>m;
  10. while(fin>>i>>j)
  11. {
  12. a[i][j]=1;
  13. a[j][i]=1;
  14.  
  15.  
  16. }
  17. }
  18. void bfs(int x)
  19. {int p,u,k,i;
  20. p=u=1;
  21. c[p]=x;
  22. viz[x]=1;
  23. t[x]=0;
  24. while(p<=u)
  25. { k=c[p];
  26. p++;
  27. for(i=1;i<=n;i++)
  28. if(a[k][i]==1&&viz[i]==0)
  29. { u++;
  30. c[u]=i;
  31. viz[i]=1;
  32. t[i]=k;
  33. //niv[i]=niv[k]+1;
  34. }
  35. }
  36. }
  37. void afisare()
  38. {
  39. int i;
  40. for(i=1;i<=n;i++)
  41. fout<<t[i]<<' ';
  42.  
  43. }
  44. int main()
  45. { citire();
  46. bfs(m);
  47. afisare();
  48.  
  49. return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement