Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
- class c_graf
- {
- private:
- bool adiacenta [ 101 ] [ 101 ];
- int marime = 0;
- std::vector < bool > vizit;
- /* pentru citire */
- std::ifstream ifs;
- std::ofstream ofs;
- bool folos_consola = false;
- /* pentru in latime */
- int coada [ 101 ], index_coada, index_parcurgere;
- public:
- /* stergem constructorii de baza */
- c_graf ( ) = delete;
- c_graf ( const c_graf& ) = delete;
- /* singurul constructor folosit */
- c_graf ( const int& marime_graf , std::string fisier_in = " " , std::string fisier_out = " " ) {
- if ( fisier_in == " " || fisier_out == " " ) {
- this->folos_consola = true;
- return;
- }
- this->folos_consola = false;
- this->ifs.open ( fisier_in );
- this->ofs.open ( fisier_out );
- if ( marime_graf == -1 ) this->citeste ( this->marime );
- else this->marime = marime_graf;
- this->index_coada = 0;
- this->index_parcurgere = 0;
- this->vizit.reserve ( this->marime );
- }
- /* functii utile */
- /* general afisare */
- void afiseaza ( std::string s ) {
- if ( this->folos_consola ) std::cout << s;
- else ofs << s;
- }
- void citeste ( int & v ) {
- if ( this->folos_consola ) std::cin >> v;
- else ifs >> v;
- }
- /* afisare -- nu merge in codeblocks pentru ca c++11 e ceva feature nemaiauzit si compilatorul nu recunoaste functia */
- void afis_coada ( ) {
- for ( int i = 0 ; i < this->index_coada ; i ++ )
- this->afiseaza ( std::to_string ( this->coada [ i ] ) );
- }
- void afis_matrice_adiacenta ( ) {
- for ( int i = 0 ; i < this->marime ; i ++ ) {
- for ( int j = 0 ; j < this->marime ; j ++ )
- this->afiseaza ( this->adiacenta [ i ] [ j ] ? "1 " : "0 " );
- this->afiseaza ( "\n" );
- }
- }
- /* citire */
- void citeste_muchii ( int muchii = 0 ) {
- if ( muchii != -1 ) {
- if ( !muchii )
- this->citeste ( muchii );
- for ( int i = 0 ; i < muchii ; i ++ ) {
- int a , b;
- this->citeste ( a );
- this->citeste ( b );
- this->adiacenta [ a ] [ b ] = this->adiacenta [ b ] [ a ] = true;
- }
- return;
- }
- int a , b;
- while ( this->ifs >> a >> b )
- this->adiacenta [ a ] [ b ] = this->adiacenta [ b ] [ a ] = true;
- }
- /* parcurgere */
- void parcurgere_inaltime ( int start ) {
- this->vizit.at ( start ) = true;
- this->coada [ this->index_coada ] = start;
- this->index_coada ++;
- /* cautam urmatorul nod vecin nevizitat */
- for ( int x = 1 ; x <= this->marime ; x ++ )
- if ( this->adiacenta [ start ] [ x ] )
- if ( !this->vizit.at ( x ) )
- this->parcurgere_inaltime ( x );
- }
- void parcurgere_latime ( int start , bool incep = true ) {
- if ( incep ) {
- this->vizit.at ( start ) = true;
- this->coada [ this->index_coada ] = start;
- this->index_coada ++;
- }
- /* vedem vecini */
- for ( int i = 1 ; i <= this->marime ; i ++ ) {
- /* verificam daca nodul de start e vecin cu nodul gasit */
- if ( ! this->adiacenta [ i ] [ start ] || i == start )
- continue;
- int bak = i;
- /* verificam daca am gasit deja nodul */
- for ( int j = 0 ; j <= this->index_coada ; j ++ ) {
- if ( this->coada [ j ] == i ) {
- i ++;
- break;
- }
- }
- /* metoda cam ghetto */
- if ( bak != i ) {
- bak = i;
- continue;
- }
- /* nodul este nou, il bagam in coada */
- this->vizit.at ( i ) = true;
- this->coada [ index_coada ] = i;
- this->index_coada ++;
- }
- /* parcurgem nod nou , daca este posibil */
- this->index_parcurgere ++;
- if ( !this->coada [ this->index_parcurgere ] )
- return;
- this->parcurgere_latime ( this->coada [ this->index_parcurgere ] , false );
- }
- };
- int main ( void ) {
- c_graf* graf = new c_graf ( -1 , "componenteconexe.in" , "componenteconexe.out" );
- graf->citeste_muchii ( );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement