Advertisement
Radurco

Grafuri neordonate cred

Oct 13th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. ifstream f("matrice.txt");
  5. int n,i,j,a[20][20],v[20],coada[20],i_c,sf_c;
  6. void citire()
  7. {
  8.     f>>n;
  9.     while(f>>i>>j)
  10.     {
  11.         a[i][j]=1;
  12.         a[j][i]=1;
  13.     }
  14. }
  15. void afisare()
  16. {
  17.     for(int i=1; i<=n; i++)
  18.     {
  19.         for (int j=1; j<=n; j++)
  20.         {
  21.             cout<<a[i][j]<<" ";
  22.         }
  23.         cout<<endl;
  24.     }
  25. }
  26. void adancime(int nod)
  27. {
  28.     cout<<nod<<" ";
  29.     v[nod]=1;
  30.     for(int i=1; i<=n; i++)
  31.     {
  32.         if(a[nod][i]==1 && v[i]==0)
  33.         {
  34.             adancime(i);
  35.         }
  36.     }
  37. }
  38. void latime(int nod)
  39. {
  40.     coada[i_c]=nod;
  41.     v[nod]=1;
  42.     while(i_c<=sf_c)
  43.     {
  44.         i=1;
  45.         while(i<=n)
  46.         {
  47.             if(a[coada[i_c]][i]&&v[i]==0)
  48.             {
  49.                 sf_c++;
  50.                 coada[sf_c]=i;
  51.                 v[i]=1;
  52.             }
  53.             i++;
  54.  
  55.         }
  56.         cout<<coada[i_c]<<" ";
  57.         i_c++;
  58.     }
  59. }
  60. void conexe()
  61. {
  62.     for(int i=1; i<=n; i++)
  63.     {
  64.         if(v[i]==0)
  65.         {
  66.             adancime(i);
  67.             cout<<endl;
  68.         }
  69.     }
  70. }
  71. int main()
  72. {
  73.     citire();
  74.     afisare();
  75.     conexe();
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement