Cod sursa(job #1703470)

Utilizator FlorinHajaFlorin Gabriel Haja FlorinHaja Data 16 mai 2016 23:06:26
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.62 kb
#include <fstream>
#include <vector>

using namespace std;

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

int n, m, cn, x, y, i;
bool viz[100005];
vector <int> ls[100005];

void df(int nod){
    int i;
    viz[nod] = 1;
    for (i = 0; i < ls[nod].size(); i++)
        if (viz[ls[nod][i]] == 0)
            df(ls[nod][i]);
}

int main(){
    f >> n >> m;
    while (m){
        f >> x >> y;
        ls[x].push_back(y);
        ls[y].push_back(x);
        m--;
    }
    for (i = 1; i <= n; i++)
        if (viz[i] == 0){
            df(i);
            cn++;
        }
    g << cn;
    return 0;
}