Cod sursa(job #1559084)

Utilizator Mr.DoomRaul Ignatus Mr.Doom Data 30 decembrie 2015 00:50:15
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;

ifstream is("dfs.in");
ofstream os("dfs.out");

vector<vector<int> > G;
bitset<100001> v;
int n, m;

void Df(int x);
void Read();

int main()
{
    int cnt = 0;
    Read();
    for ( int i = 1; i <= n; ++i )
    {
        if ( !v[i] )
        {
            cnt++;
            Df(i);
        }
    }

    os << cnt;

    is.close();
    os.close();
    return 0;
}

void Read()
{
    int x, y;
    is >> n >> m;
    G = vector<vector<int> >(n + 1);
    for ( int i = 1; i <= m; ++i )
    {
        is >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
}

void Df(int x)
{
    v[x] = 1;
    for ( const auto &y : G[x] )
        if ( !v[y] )
            Df(y);
}