Cod sursa(job #1500471)

Utilizator danysaffSafta Marius Daniel danysaff Data 11 octombrie 2015 23:13:28
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.61 kb
#include <iostream>
#include<fstream>
#include<vector>
#define maxn 200000
using namespace std;
ifstream in("dfs.in");
ofstream out("dfs.out");
vector<int> v[maxn];
bool viz[maxn];
void dfs(int x)
{   int i;
    viz[x]=1;
    for(i=0; i<v[x].size(); i++)
        if(!viz[v[x][i]])
            dfs(v[x][i]);
}

int main()
{   int n,m,i, x, y, nr=1;

    in>>n>>m;
    for(i=1; i<=m; i++)
    {   in>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
    }

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

    out<<nr;
    return 0;
}