Cod sursa(job #1569237)

Utilizator qwertyuiTudor-Stefan Berbinschi qwertyui Data 15 ianuarie 2016 10:42:26
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.74 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

ifstream fin ("dfs.in");
ofstream fout ("dfs.out");

vector <int> edge[100005];

int visited[100005],n,m;

void DFS (int node)
{
    visited[node]=1;
    for (int i = 0; i < (int)edge[node].size(); ++i)
        if (!visited[edge[node][i]])
            DFS(edge[node][i]);
}

int main()
{
    int x,y,counter;
    fin >>n >>m;
    for (int i = 1; i <= m; ++i)
    {
        fin >>x >>y;
        edge[x].push_back(y);
        edge[y].push_back(x);
    }
    counter = 0;
    for (int i = 1; i <= n; ++i)
        if (!visited[i])
        {
            ++counter;
            DFS(i);
        }
    fout <<counter <<'\n';
    return 0;
}