Cod sursa(job #1509756)

Utilizator AlexandruRudiAlexandru Rudi AlexandruRudi Data 24 octombrie 2015 11:58:49
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include <iostream>
#include <vector>
#include <fstream>

#define maxn 100005

using namespace std;

int n,m,x,y,sol;
vector <int> a[maxn];
int v[maxn];

void viz(int nod,bool st){
    v[nod]=1;
    for(int i=0;i<a[nod].size();i++){
        if(v[a[nod][i]]==0){
            viz(a[nod][i],false);
        }
    }
    if(st){
        sol++;
        int i=nod+1;
        while(v[i]==1 && i<=n) i++;
        if(i<=n) viz(i,true);
    }
}

int main()
{
    ifstream in("dfs.in");
    ofstream out("dfs.out");
    in >> n >> m;
    for(int i=1;i<=m;i++){
        in >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
    viz(1,true);
    out << sol;
}