Cod sursa(job #3248572)

Utilizator rapatudorRapa Balan Tudor Florin rapatudor Data 12 octombrie 2024 10:39:38
Problema Parcurgere DFS - componente conexe Scor 5
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.59 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

int n, m, x, y, rez = 1;

vector<int> graph[200005];
int marked[200005];

void dfs(int k){
    int lim = graph[k].size();
    for(int i = 0; i<lim; i++){
        if(marked[ graph[k][i] ] == 0){
            marked[ graph[k][i] ] = 1;
            rez++;
            dfs(i);
        }
    }
}
int main()
{
    fin>>n>>m;
    for(int i = 0; i<m; i++){
        fin>>x>>y;
        graph[x].push_back(y);
        graph[y].push_back(x);
    }
    dfs(1);
    fout<<rez;
    return 0;
}