Cod sursa(job #2655926)

Utilizator RNedelcuNedelcu Radu RNedelcu Data 6 octombrie 2020 09:02:51
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.61 kb
#include <bits/stdc++.h>
using namespace std;
#define MAX 100000
ifstream in("dfs.in");
ofstream out("dfs.out");
int N,M,sol=0;
vector<int> A[MAX];
bool viz[MAX];
void DFS(int nod)
{
    viz[nod] = true;
    for(auto temp:A[nod])
        if(!viz[temp])
            DFS(temp);
}
int main()
{
    in>>N>>M;
    int x,y;
    for(int i=0; i<N; i++)
    {

        in>>x>>y;
        A[x].push_back(y);
    }
    memset(viz,0, sizeof(viz));
    for(int i=0; i<N; i++)
    {
        if(!viz[i])
        {
            sol++;
            DFS(i);
        }
    }
    out<<sol;
    return 0;
}