Advertisement
Guest User

arbore

a guest
Jan 21st, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. ifstream fin("graf.in");
  6.  
  7. void Citire(int &n, int m[][100])
  8. {
  9.     fin>>n;
  10.     while(!fin.eof())
  11.     {
  12.         int x,y;
  13.         fin>>x>>y;
  14.         m[x][y]=1;
  15.         m[y][x]=1;
  16.     }
  17.     fin.close();
  18. }
  19.  
  20. void DFS(int m[][100], int *viz, int n, int nod)
  21. {
  22.     viz[nod]=1;
  23.     for(int i=1; i<=n; i++)
  24.         if(m[nod][i] == 1 && viz[i] == 0)
  25.             DFS(m, viz, n, i);
  26. }
  27.  
  28. int Matrice(int n, int m[][100])
  29. {
  30.     int s=0;
  31.     for(int i=1; i<=n; i++)
  32.         for(int j=1; j<=n; j++)
  33.             s+=m[i][j];
  34.     return s;
  35. }
  36.  
  37. int main()
  38. {
  39.     int n, m[100][100], viz[100]={0};
  40.     Citire(n, m);
  41.     viz[1]=1;
  42.     DFS(m, viz, n, 1);
  43.     int ok=1;
  44.     for(int i=1; i<=n; i++)
  45.         if(viz[i] == 0)
  46.           {
  47.               cout<<"Nu este conex => nu este arbore";
  48.               ok=0;
  49.           }
  50.     if(ok == 1)
  51.         if(Matrice(n, m) == 2*(n-1))
  52.             cout<<"Este arbore";
  53.         else
  54.             cout<<"Nu are n-1 muchii => nu este arbore";
  55.  
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement