Advertisement
hegemon88676

mall (nod in care se ajunge din orice alt nod)

May 29th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. int a[100][100], n, m, d[100], c[100];
  6. bool viz[100];
  7. ifstream f("date.in");
  8.  
  9. void citire()
  10. {
  11.     int i, j, k;
  12.     f>>n>>m;
  13.     for(k=1; k<=m; k++)
  14.     {
  15.         f>>i>>j;
  16.         a[i][j]=1;
  17.     }
  18. }
  19.  
  20. void lee(int start)
  21. {
  22.     int p, u, i, x;
  23.     for(i=1; i<=n; i++)
  24.     {
  25.         c[i]=0;
  26.         d[i]=0;
  27.         viz[i]=false;
  28.     }
  29.     p=u=1;
  30.     c[1]=start;
  31.     viz[start]=true;
  32.     d[start]=0;
  33.     while(p<=u)
  34.     {
  35.         x=c[p];
  36.         for(i=1; i<=n; i++)
  37.             if(a[x][i]==1 && viz[i]==false)
  38.             {
  39.                 viz[i]=true;
  40.                 u++;
  41.                 c[u]=i;
  42.                 d[i]=d[x]+1;
  43.             }
  44.         p++;
  45.     }
  46. }
  47.  
  48. int main()
  49. {
  50.     int i, j, ok,k=0;
  51.  
  52.     citire();
  53.     cout<<"Matricea de adiacenta:";
  54.     for(i=1; i<=n; i++)
  55.     {
  56.         cout<<endl;
  57.         for(j=1; j<=n; j++)
  58.             cout<<a[i][j]<<" ";
  59.     }
  60.     cout<<endl;
  61.     for(i=1; i<=n; i++)
  62.     {
  63.         ok=1;
  64.         for(j=1; j<=n; j++)
  65.         {
  66.             if(j!=i)
  67.             {
  68.                 lee(j);
  69.                 if(d[i]==0)
  70.                     ok=0;
  71.             }
  72.         }
  73.         if(ok==1)
  74.         {
  75.             k=1;
  76.             cout<<endl<<i<<endl;
  77.         }
  78.     }
  79.     if(!k)
  80.         cout<<"\nNU EXISTA";
  81. }
  82. /*
  83. 5 6
  84. 1 4
  85. 2 1
  86. 4 2
  87. 4 3
  88. 5 4
  89. 5 1
  90.  
  91. 5 5
  92. 3 1
  93. 4 1
  94. 4 2
  95. 5 1
  96. 5 3
  97. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement