Cod sursa(job #2355155)

Utilizator Tudor_Trasculescu123Tudor Trasculescu Tudor_Trasculescu123 Data 25 februarie 2019 21:02:54
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include <bits/stdc++.h>

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

int n , m , x , y ,visited[100005] , maxim , k ;

struct MakePer
{
    vector <int> v ;
};
MakePer a[100005] ;

void dfs(int S)
{
    visited[S] = 1 ;

    for(vector<int>::iterator it = a[S].v.begin(); it != a[S].v.end(); it ++)
        if(visited[*it] == 0)
        {
            visited[*it] = visited[S] + 1 ;
            dfs(*it) ;
        }
}

int main()
{
    fin >> n >> m ;
    for(int i=1; i<=m; i++)
    {
        fin >> x >> y ;
        a[x].v.push_back(y) ;
        a[y].v.push_back(x) ;
    }
    for(int i=1; i<=n; i++)
        if(visited[i] == 0)
        {
           dfs(i) ;
           k ++ ;
        }
    fout << k ;

    return 0;
}