Cod sursa(job #1693060)

Utilizator Mr.RobotElliot Alderson Mr.Robot Data 22 aprilie 2016 12:30:16
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

ifstream f("dfs.in");
ofstream g("dfs.out");

int viz[100001], n, m, x, y;
vector<int> liste[100001];


void dfs(int x)
{
    viz[x] = 1;
    for( vector<int>::iterator it=liste[x].begin(); it!=liste[x].end(); it++ )
        if( viz[*it] == 0 )
            dfs(*it);
}

int main()
{

    f >> n >> m;

    for( int i=1; i<=m; i++ )
    {
        f >> x >> y;
        liste[x].push_back(y);
        liste[y].push_back(x);
    }

    int nr = 0;

    for( int i=1; i<=n; i++ )
    {
        if(viz[i] == 0)
        {
            nr++;
            dfs(i);
        }
    }

    g << nr;

    return 0;
}