Cod sursa(job #2261024)

Utilizator BlkAlexAlex Negru BlkAlex Data 15 octombrie 2018 20:52:54
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <bits/stdc++.h>
#define MAX 100100

using namespace std;

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

int n, m, x, y, rez=0;
vector <int> v[MAX];
bool viz[MAX];

void dfs (int node){
    int i, newnode;
    viz[node]=true;
    for (i=0; i<v[node].size(); i++){
        newnode=v[node][i];
        if (viz[newnode]==false) dfs(newnode);
    }
}

int main()
{
    int i, j;
    f>>n>>m;
    for(i=1; i<=m; i++){
        f>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    for (i=1; i<=n; i++){
        if(viz[i]==false){
            rez++;
            dfs(i);
        }
    }
    g<<rez;
    return 0;
}