Cod sursa(job #2105496)

Utilizator calinfloreaCalin Florea calinflorea Data 13 ianuarie 2018 14:34:31
Problema Parcurgere DFS - componente conexe Scor 5
Compilator cpp Status done
Runda Arhiva educationala Marime 0.76 kb
#include <bits/stdc++.h>
#define NMax 100006
using namespace std;

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

vector <int> L[NMax];
bool viz[NMax];
int n, m, ans;
void Citire()
{
    int i, x, y;

    fin >> n >> m;

    for(i = 1; i <= m; i++)
    {
        fin >> x >> y;
        L[x].push_back(y);
        L[y].push_back(x);
    }
}

void DFS(int x)
{
    int i, j;
    viz[x] = 1;

    for(j = 0; j < L[x].size(); j++)
    {
        j = L[i][j];
        if(!viz[i])
            DFS(i);
    }
}

void Rezolva()
{
    int i;

    for(i = 1; i <= n; i++)
        if(!viz[i])
        {
            ans++;
            DFS(i);
        }
    fout << ans << "\n";
}
int main()
{
    Citire();
    Rezolva();
    return 0;
}