SHARE
TWEET

muie badii

a guest Jan 14th, 2020 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <fstream>
  2.  
  3. using namespace std;
  4.  
  5. bool a[105][105];
  6. int n, viz[105];
  7. ifstream fin("componenteconexe.in");
  8. ofstream fout("componenteconexe.out");
  9.  
  10. void citire()
  11. {
  12.     int x,y;
  13.     fin>>n;
  14.  
  15.     while(fin>>x>>y)
  16.         a[x][y]=a[y][x]=1;
  17.  
  18.  
  19. }
  20. void DFS(int x,int k)
  21. {
  22.     viz[x]=k;
  23.     for(int i=1; i<=n ; i++)
  24.         if (a[i][x]&&!viz[i])
  25.             DFS(i,k);
  26. }
  27. void afisare(int k)
  28. {
  29.     fout<<k<<'\n';
  30.     for(int i=1; i<=k ; i++)
  31.     {
  32.         for(int j=1; j<=n; j++)
  33.             if (viz[j]==i)
  34.                 fout<<j<<" ";
  35.         fout<<'\n';
  36.     }
  37. }
  38.  
  39. int main()
  40. {
  41.     int k=0;
  42.     citire();
  43.     for(int i=1; i<=n; i++)
  44.         if(!viz[i])
  45.         {
  46.             k++;
  47.             DFS(i,k);
  48.         }
  49.     afisare(k);
  50.  
  51.     return 0;
  52. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top