Untitled

mail@pastecode.io avatar
unknown
c_cpp
a month ago
1.3 kB
2
Indexable
Never
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>

class Graph
{
public:
    int n;
    std::vector< std::vector< int > > adj;

    Graph( int vertices ) : n( vertices ), adj( vertices + 1 ) {}

    void addEdge( int u, int v )
    {
        adj[ u ].push_back( v );
        adj[ v ].push_back( u );
    }
};

void readInput( const std::string& filename, std::vector< Graph >& graphs )
{
    std::ifstream inputFile( filename );
    if( !inputFile.is_open() )
    {
        std::cout << "Error opening file!" << std::endl;
        exit( EXIT_FAILURE );
    }

    int k;
    inputFile >> k;
    inputFile.ignore();

    for( int i = 0; i < k; ++i )
    {
        int n;
        inputFile >> n;
        inputFile.ignore();
        Graph g( n );
        for( int j = 0; j < n; ++j )
        {
            std::string line;
            std::getline( inputFile, line );
            std::stringstream ss( line );

            int number;
            while( ss >> number )
            {
                g.addEdge( j, number );
            }
        }
        graphs.push_back( g );
    }

    inputFile.close();
}

int main()
{
    std::vector< Graph > graphs;
    readInput( "data/test_2p_in.txt", graphs );

    return 0;
}
Leave a Comment