Cod sursa(job #3256134)

Utilizator Andrei24543Andrei Hulubei Andrei24543 Data 13 noiembrie 2024 16:40:17
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.57 kb
#include <bits/stdc++.h>
using namespace std;

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

vector<int> L[100005];
int a[100005] , n , m;

void DFS(int k)
{
    a[k] = 1;
    for(int i : L[k])
        if(a[i] == 0) DFS(i);
}

int main()
{
    int i , j , k , nrcc = 0;
    fin >> n >> m;
    for(k = 1;k <= m;k++)
    {
        fin >> i >> j;
        L[i].push_back(j);
        L[j].push_back(i);
    }
    for(i = 1;i <= n;i++)
        if(a[i] == 0)
        {
            nrcc++;
            DFS(i);
        }
    fout << nrcc;
    return 0;
}