Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. int n,m,x,a[100][100],viz[100],c[100];
  5. void citire()
  6. {
  7. ifstream f ("componenteconexe.in");
  8. f>>n>>m;
  9. for(int q=1;q<=m;q++)
  10. {
  11. int i,j;
  12. f>>i>>j;
  13. a[i][j]=a[j][i]=1;
  14. }
  15. }
  16. void BF(int x)
  17. {
  18. int p,u,v;
  19. p=u=1;
  20. c[p]=x;
  21. cout<<c[p]<<" ";
  22. viz[x]=1;
  23. while(p<=u)
  24. {
  25. v=c[p];
  26. for(int i=1;i<=n;i++)
  27. if(a[i][v]==1 && viz[i]==0)
  28. {
  29. viz[i]=1;
  30. u++;
  31. c[u]=i;
  32. cout<<c[u]<<" ";
  33. }
  34. p++;
  35. }
  36. cout<<endl;
  37. }
  38. int main()
  39. {
  40. citire();
  41. for(int i=1;i<=n;i++)
  42. if(viz[i]==0)
  43. BF(i);
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement