Cod sursa(job #2020879)

Utilizator UWantMyNameGheorghe Vlad Camil UWantMyName Data 11 septembrie 2017 21:53:20
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
#include <bits/stdc++.h>
#define in "dfs.in"
#define out "dfs.out"
using namespace std;
ifstream fin(in);
ofstream fout(out);

int n,m;
vector <int> L[100003];
int viz[100003],p;

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 k)
{
    viz[k] = 1;
    for (auto i : L[k])
        if (viz[i] == 0) DFS(i);
}

void Rezolvare()
{
    int i;

    for (i = 1; i <= n; i++)
        if (viz[i] == 0)
        {
            p++;
            DFS(i);
        }

    fout << p << "\n";
}

int main()
{
    Citire();
    Rezolvare();

    fin.close();
    fout.close();
    return 0;
}