Cod sursa(job #3213483)

Utilizator antonio.grGrigorascu Andrei Antonio antonio.gr Data 13 martie 2024 10:27:40
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.84 kb
#include<iostream>
#include<vector>
#include<fstream>
#include<stack>

const int limita = 100005;

std::ifstream fin("input.txt");
std::ofstream fout("output.txt");
int conexe = 0;

bool visited[limita];
std::vector<int> graph[limita];

void DFS(int k){
    visited[k] = true;
    for(unsigned int i =0; i< graph[k].size();i++){
        int vecin = graph[k][i];
        if(!visited[vecin]){
            DFS(vecin);
        }
    }
}



int main(){

    int N, M;
    fin>>N>>M;
    for(int i=0; i<M; i++){
        int x, y;
        fin>>x>>y;
        graph[x].push_back(y);
        graph[y].push_back(x);

    }

    DFS(1);

    for(int i = 1; i<N; i++){
        if(!visited[i]){
            conexe += 1;
            DFS(i);
        }
    }

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