Cod sursa(job #1469112)

Utilizator tudormaximTudor Maxim tudormaxim Data 7 august 2015 16:32:34
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin ("dfs.in");
ofstream fout ("dfs.out");
const int nmax = 100005;
vector <int> graf[nmax];
int n, m, nr;
bool viz[nmax];
void dfs(int x)
{
    viz[x]=true;
    for(int i=0; i<graf[x].size(); i++)
        if(viz[graf[x][i]]==false) dfs(graf[x][i]);
}
int main()
{
    int x, y, i;
    fin >> n >> m;
    for(i=1; i<=m; i++)
    {
        fin >> x >> y;
        graf[x].push_back(y);
        graf[y].push_back(x);
    }
    for(i=1; i<=n; i++)
        if(viz[i]==false)
        {
            dfs(i);
            nr++;
        }
    fout << nr;
    fin.close();
    fout.close();
    return 0;
}