Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5. #include <algorithm>
  6.  
  7. class c_graf
  8. {
  9. private:
  10. bool adiacenta [ 101 ] [ 101 ];
  11. int marime = 0;
  12. std::vector < bool > vizit;
  13.  
  14. /* pentru citire */
  15. std::ifstream ifs;
  16. std::ofstream ofs;
  17. bool folos_consola = false;
  18.  
  19. /* pentru in latime */
  20. int coada [ 101 ], index_coada, index_parcurgere;
  21. public:
  22. /* stergem constructorii de baza */
  23. c_graf ( ) = delete;
  24. c_graf ( const c_graf& ) = delete;
  25.  
  26. /* singurul constructor folosit */
  27. c_graf ( const int& marime_graf , std::string fisier_in = " " , std::string fisier_out = " " ) {
  28. if ( fisier_in == " " || fisier_out == " " ) {
  29. this->folos_consola = true;
  30. return;
  31. }
  32.  
  33. this->folos_consola = false;
  34.  
  35. this->ifs.open ( fisier_in );
  36. this->ofs.open ( fisier_out );
  37.  
  38. if ( marime_graf == -1 ) this->citeste ( this->marime );
  39. else this->marime = marime_graf;
  40.  
  41. this->index_coada = 0;
  42. this->index_parcurgere = 0;
  43. this->vizit.reserve ( this->marime );
  44. }
  45.  
  46. /* functii utile */
  47. /* general afisare */
  48. void afiseaza ( std::string s ) {
  49. if ( this->folos_consola ) std::cout << s;
  50. else ofs << s;
  51. }
  52.  
  53. void citeste ( int & v ) {
  54. if ( this->folos_consola ) std::cin >> v;
  55. else ifs >> v;
  56. }
  57.  
  58. /* afisare -- nu merge in codeblocks pentru ca c++11 e ceva feature nemaiauzit si compilatorul nu recunoaste functia */
  59. void afis_coada ( ) {
  60. for ( int i = 0 ; i < this->index_coada ; i ++ )
  61. this->afiseaza ( std::to_string ( this->coada [ i ] ) );
  62. }
  63.  
  64. void afis_matrice_adiacenta ( ) {
  65. for ( int i = 0 ; i < this->marime ; i ++ ) {
  66. for ( int j = 0 ; j < this->marime ; j ++ )
  67. this->afiseaza ( this->adiacenta [ i ] [ j ] ? "1 " : "0 " );
  68. this->afiseaza ( "\n" );
  69. }
  70. }
  71.  
  72. /* citire */
  73. void citeste_muchii ( int muchii = 0 ) {
  74. if ( muchii != -1 ) {
  75. if ( !muchii )
  76. this->citeste ( muchii );
  77.  
  78. for ( int i = 0 ; i < muchii ; i ++ ) {
  79. int a , b;
  80. this->citeste ( a );
  81. this->citeste ( b );
  82. this->adiacenta [ a ] [ b ] = this->adiacenta [ b ] [ a ] = true;
  83. }
  84. return;
  85. }
  86.  
  87. int a , b;
  88.  
  89. while ( this->ifs >> a >> b )
  90. this->adiacenta [ a ] [ b ] = this->adiacenta [ b ] [ a ] = true;
  91. }
  92.  
  93. /* parcurgere */
  94. void parcurgere_inaltime ( int start ) {
  95. this->vizit.at ( start ) = true;
  96. this->coada [ this->index_coada ] = start;
  97. this->index_coada ++;
  98.  
  99. /* cautam urmatorul nod vecin nevizitat */
  100. for ( int x = 1 ; x <= this->marime ; x ++ )
  101. if ( this->adiacenta [ start ] [ x ] )
  102. if ( !this->vizit.at ( x ) )
  103. this->parcurgere_inaltime ( x );
  104. }
  105.  
  106. void parcurgere_latime ( int start , bool incep = true ) {
  107. if ( incep ) {
  108. this->vizit.at ( start ) = true;
  109. this->coada [ this->index_coada ] = start;
  110. this->index_coada ++;
  111. }
  112.  
  113. /* vedem vecini */
  114. for ( int i = 1 ; i <= this->marime ; i ++ ) {
  115. /* verificam daca nodul de start e vecin cu nodul gasit */
  116. if ( ! this->adiacenta [ i ] [ start ] || i == start )
  117. continue;
  118.  
  119. int bak = i;
  120.  
  121. /* verificam daca am gasit deja nodul */
  122. for ( int j = 0 ; j <= this->index_coada ; j ++ ) {
  123. if ( this->coada [ j ] == i ) {
  124. i ++;
  125. break;
  126. }
  127. }
  128.  
  129. /* metoda cam ghetto */
  130. if ( bak != i ) {
  131. bak = i;
  132. continue;
  133. }
  134.  
  135. /* nodul este nou, il bagam in coada */
  136. this->vizit.at ( i ) = true;
  137. this->coada [ index_coada ] = i;
  138. this->index_coada ++;
  139. }
  140.  
  141. /* parcurgem nod nou , daca este posibil */
  142. this->index_parcurgere ++;
  143.  
  144. if ( !this->coada [ this->index_parcurgere ] )
  145. return;
  146.  
  147. this->parcurgere_latime ( this->coada [ this->index_parcurgere ] , false );
  148. }
  149. };
  150.  
  151. int main ( void ) {
  152. c_graf* graf = new c_graf ( -1 , "componenteconexe.in" , "componenteconexe.out" );
  153. graf->citeste_muchii ( );
  154.  
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement