Cod sursa(job #2275921)

Utilizator driver71528@gmail.comTerec Andrei-Sorin [email protected] Data 3 noiembrie 2018 19:16:46
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp-32 Status done
Runda Arhiva educationala Marime 0.58 kb
#include <iostream>
#include <vector>

#define MAXn 100000
#define MAXm 200000
using namespace std;

vector<int> v[MAXn+1];
bool viz[MAXn+1];
int n,m;

void DFS(int i)
{
    viz[i]=true;
    for(int j=0;j<v[i].size();j++)
        if(!viz[v[i][j]])
            DFS(v[i][j]);
}


int main()
{
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int a,b;
        cin>>a>>b;
        v[a].push_back(b);
    }
    int rez=0;
    for(int i=1;i<=n;i++)
        if(!viz[i])
        {
            rez++;
            DFS(i);
        }
    cout<<rez;

    return 0;
}